01c6323d23
Instead of applying WHERE on a UNION, apply the WHERE on each of the seperate SELECT statements, and do UNION on that. Local tests with about 2_000_000 projects: - 1_500_000 private projects - 40_000 internal projects - 400_000 public projects For the API endpoint `/api/v4/projects?visibility=private` the slowest query was: ```sql SELECT "projects".* FROM "projects" WHERE ... ``` The original query took 1073.8ms. The query refactored to UNION of SELECT/WHERE took 2.3ms. The original query was: ```sql SELECT "projects".* FROM "projects" WHERE "projects"."pending_delete" = $1 AND (projects.id IN (SELECT "projects"."id" FROM "projects" INNER JOIN "project_authorizations" ON "projects"."id" = "project_authorizations"."project_id" WHERE "projects"."pending_delete" = 'f' AND "project_authorizations"."user_id" = 23 UNION SELECT "projects"."id" FROM "projects" WHERE "projects"."visibility_level" IN (20, 10))) AND "projects"."visibility_level" = $2 AND "projects"."archived" = $3 ORDER BY "projects"."created_at" DESC LIMIT 20 OFFSET 0 [["pending_delete", "f"], ["visibility_level", 0], ["archived", "f"]] ``` The refactored query: ```sql SELECT "projects".* FROM "projects" WHERE "projects"."pending_delete" = $1 AND (projects.id IN (SELECT "projects"."id" FROM "projects" INNER JOIN "project_authorizations" ON "projects"."id" = "project_authorizations"."project_id" WHERE "projects"."pending_delete" = 'f' AND "project_authorizations"."user_id" = 23 AND "projects"."visibility_level" = 0 AND "projects"."archived" = 'f' UNION SELECT "projects"."id" FROM "projects" WHERE "projects"."visibility_level" IN (20, 10) AND "projects"."visibility_level" = 0 AND "projects"."archived" = 'f')) ORDER BY "projects"."created_at" DESC LIMIT 20 OFFSET 0 [["pending_delete", "f"]] ``` |
||
---|---|---|
.. | ||
.gitkeep | ||
12614-fix-long-message-from-mr.yml | ||
17489-hide-code-from-guests.yml | ||
18927-reorder-issue-action-buttons.yml | ||
19107-404-when-creating-new-milestone-or-issue-for-project-that-has-issues-disabled.yml | ||
20517-delete-projects-issuescontroller-redirect_old.yml | ||
22619-add-an-email-address-to-unsubscribe-list-header-in-email | ||
23036-replace-all-spinach-tests-with-rspec-feature-tests.yml | ||
24373-warning-message-go-away.yml | ||
25373-jira-links.yml | ||
26325-system-hooks.yml | ||
26908-make-timelogs-use-foreign-keys | ||
27439-memory-usage-info.yml | ||
29852-latex-formatting.yml | ||
30410-revert-9347-and-10079.yml | ||
30827-changes-to-audit-log.yml | ||
30917-wiki-is-not-searchable-with-guest-permissions.yml | ||
30949-empty-states.yml | ||
31384-new-issue-button-on-no-results-page-after-search-doesn-t-go-to-correct-form.yml | ||
31448-jira-urls.yml | ||
31474-issue-boards-sidebar-milestone-dropdown-should-not-be-multi-select.yml | ||
31483-ordered-task-list.yml | ||
31510-mask-password-field-edit.yml | ||
31554-update-rufus-scheduler-and-sidekiq.yml | ||
31556-ci-coverage-paralel-rspec.yml | ||
31616-add-uptime-of-gitlab-instance-in-admin-area.yml | ||
31625-tag-editor-loses-all-inputs-when-you-try-to-add-a-tag-that-already-exists.yml | ||
31781-print-rendered-files-not-possible.yml | ||
31849-pipeline-show-view-realtime.yml | ||
31902-namespace-recent-searches-to-project.yml | ||
31998-pipelines-empty-state.yml | ||
32086-atwho-is-still-enabled-for-personal-snippet-comments-form.yml | ||
32219-speed-up-yarn-install-in-ci-by-utilizing-inter-pipeline-cache.yml | ||
32340-correct-jobs-api-documentation | ||
32395-duplicate-string-in-https-docs-gitlab-com-ce-administration-environment_variables-html.yml | ||
32418-make-link-to-self-less-obvious.yml | ||
32570-project-activity-tab-border.yml | ||
32598-avoid-resource-intensive-login-checks-if-password-is-not-provided-for-git-http.yml | ||
32682-skipped-ci-icon.yml | ||
32715-fix-note-padding.yml | ||
32790-pipeline_schedules-pages-throwing-error-500.yml | ||
32799-remove-no_turbolink-attribute-from-haml.yml | ||
32807-company-icon.yml | ||
32851-postgres-min-version.yml | ||
33032-invalid-you-directly-addressed-yourself-todo-when-using-unsubscribe.yml | ||
33048-markdown-rendering-of-md-files-has-ceased-to-display-latex-equations.yml | ||
adam-influxdb-hostname.yml | ||
add-index-for-auto_canceled_by_id-mysql.yml | ||
add-mock-deployment-and-monitoring-service-for-development.yaml | ||
add-unicode-trace-feature-test.yml | ||
add_ability_to_cancel_attaching_file_and_redesign_attaching_files_ui.yml | ||
allow_numeric_values_in_gitlab_ci_yml.yml | ||
bvl-rename-build-events-to-job-events.yml | ||
ce-32623-browser-tooltip-commits-branch-list.yml | ||
ci-build-pipeline-header-vue.yml | ||
counters_cache_invalidation.yml | ||
dm-async-tree-readme.yml | ||
dm-auxiliary-viewers.yml | ||
dm-consistent-commit-sha-style.yml | ||
dm-consistent-last-push-event.yml | ||
dm-copy-as-gfm-without-empty-elements.yml | ||
dm-copy-gfm-when-parts-of-other-elements-are-selected.yml | ||
dm-dependency-linker-gemfile.yml | ||
dm-more-dependency-linkers.yml | ||
dm-outdated-system-note.yml | ||
dm-paste-code-inside-gfm-code.yml | ||
dm-tree-last-commit.yml | ||
document-foreign-keys.yml | ||
dturner-username.yml | ||
dz-project-list-cache-key.yml | ||
dz-rename-pipelines-settings-tab.yml | ||
enable-auto-cancelling-by-default.yml | ||
feature-print-go-version-in-env-info.yml | ||
feature-rss-scoped-token.yml | ||
fix-counter-cache-for-acts-as-taggable.yml | ||
fix-gb-exclude-manual-actions-from-cancelable-jobs.yml | ||
fix-github-import.yml | ||
fix-terminals-support-for-kubernetes-service.yml | ||
gitaly-local-branches.yml | ||
gitaly-opt-out.yml | ||
issuable-form-create-label-sub-groups.yml | ||
issue-templates-summary-lines.yml | ||
issue_19262.yml | ||
issue_27168_2.yml | ||
issue_32225_2.yml | ||
mrchrisw-catch-openssl.yml | ||
omega-submodules.yml | ||
prevent-project-transfer.yml | ||
protected-branches-no-one-merge.yml | ||
remove-old-isobject.yml | ||
repository-name-emojis | ||
rework-authorizations-performance.yml | ||
search-restrict-projects-to-group.yml | ||
sh-fix-container-registry-s3-redirects.yml | ||
sh-fix-submodules-trailing-spaces.yml | ||
tc-cache-trackable-attributes.yml | ||
tc-clean-pending-delete-projects.yml | ||
tc-improve-project-api-perf.yml | ||
up-arrow-focus-discussion-comment.yml | ||
update-admin-health-page.yml | ||
use_relative_path_for_project_avatars.yml | ||
wait-for-ajax-handling-all-js-requests.yml | ||
winh-pipeline-author-link.yml | ||
zj-clean-up-ci-variables-table.yml | ||
zj-fix-pipeline-etag.yml | ||
zj-pipeline-schedule-owner.yml | ||
zj-sort-env-folders.yml |