Add partial index on members to optimize highest access level query
What does this MR do?
Add partial index on memebers (user_id, access_level) WHERE requested_at IS NULL
to speed up the query generated by User#current_highest_access_level
(called from UpdateHighestRoleWorker
).
See #327593 (closed).
Execution plans
- Before - https://console.postgres.ai/gitlab/gitlab-production-tunnel/sessions/3513/commands/11623
- After - https://console.postgres.ai/gitlab/gitlab-production-tunnel/sessions/3513/commands/11630
We see massive drop in shared buffers used - ~7.40 GiB vs ~32.00 KiB.
Index creation took 37.567 s
on postgres.ai.
Database migration
Up
$ bundle exec rails db:migrate:up VERSION=20210415074645
== 20210415074645 IndexMembersOnUserIdAccessLevelRequestedAtIsNull: migrating =
-- transaction_open?()
-> 0.0000s
-- index_exists?(:members, [:user_id, :access_level], {:where=>"requested_at IS NULL", :name=>"index_members_on_user_id_and_access_level_requested_at_is_null", :algorithm=>:concurrently})
-> 0.0048s
-- execute("SET statement_timeout TO 0")
-> 0.0005s
-- add_index(:members, [:user_id, :access_level], {:where=>"requested_at IS NULL", :name=>"index_members_on_user_id_and_access_level_requested_at_is_null", :algorithm=>:concurrently})
-> 0.0037s
-- execute("RESET ALL")
-> 0.0005s
== 20210415074645 IndexMembersOnUserIdAccessLevelRequestedAtIsNull: migrated (0.0105s)
Down
$ bundle exec rails db:migrate:down VERSION=20210415074645
== 20210415074645 IndexMembersOnUserIdAccessLevelRequestedAtIsNull: reverting =
-- transaction_open?()
-> 0.0000s
-- indexes(:members)
-> 0.0053s
-- execute("SET statement_timeout TO 0")
-> 0.0005s
-- remove_index(:members, {:algorithm=>:concurrently, :name=>"index_members_on_user_id_and_access_level_requested_at_is_null"})
-> 0.0045s
-- execute("RESET ALL")
-> 0.0006s
== 20210415074645 IndexMembersOnUserIdAccessLevelRequestedAtIsNull: reverted (0.0119s)
Screenshots (strongly suggested)
Does this MR meet the acceptance criteria?
Conformity
-
📋 Does this MR need a changelog?-
I have included a changelog entry. -
I have not included a changelog entry because _____.
-
-
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
-
Label as security and @ mention @gitlab-com/gl-security/appsec
-
The MR includes necessary changes to maintain consistency between UI, API, email, or other methods -
Security reports checked/validated by a reviewer from the AppSec team
Related to #327593 (closed)
Edited by Krasimir Angelov