Reduce queries on Controller Groups::LabelsController#index [RUN AS-IF-FOSS] [RUN ALL RSPEC]
requested to merge 21034-controller-groups-labelscontroller-index-executes-more-than-100-sql-queries into master
What does this MR do?
Related to #21034 (closed) Group::Labels controller
- Fix found bullet n+1 query by preloading (:group)
- Use BatchLoader to preload the subscription for each label (each subscription is called once for each label loaded).
- Refactor
toggle_subscription_label_path
to use the new lazy method and not make another db call
Screenshots (strongly suggested)
Database
Group Labels for GitLab.org: https://gitlab.com/groups/gitlab-org/-/labels
Explain plan: https://explain.depesz.com/s/YeOw
Index Scan using index_subscriptions_on_subscribable_and_user_id_and_project_id on public.subscriptions (cost=0.42..42.25 rows=1 width=41) (actual time=7.093..39.648 rows=3 loops=1)
Index Cond: ((subscriptions.subscribable_id = ANY ('{3079031,3704995,11832434,11832683,11832684,14931416,16876290,14540790,10438591,11434168,15904538,1890178,16648665,16198756,11832677,11832671,3121561,18275109,10421239,18275109,18275109}'::integer[])) AND ((subscriptions.subscribable_type)::text = 'Label'::text) AND (subscriptions.user_id = 5708766) AND (subscriptions.project_id IS NULL))
Buffers: shared hit=37 read=26
I/O Timings: read=39.039
SQL before
individual SQL generated for each Label (2 times per label)
SELECT
"subscriptions".*
FROM
"subscriptions"
WHERE
"subscriptions"."subscribable_id" = 11832681
AND "subscriptions"."subscribable_type" = 'Label'
AND "subscriptions"."user_id" = 5708766
AND "subscriptions"."project_id" IS NULL
LIMIT 1
SQL after
SQL generated once for all labels
SELECT
"subscriptions".*
FROM
"subscriptions"
WHERE
"subscriptions"."subscribable_id" IN (3079031, 3704995, 11832434, 11832683, 11832684, 14931416, 16876290, 14540790, 10438591, 11434168, 15904538, 1890178, 16648665, 16198756, 11832677, 11832671, 3121561, 18275109, 10421239, 18275109, 18275109)
AND "subscriptions"."subscribable_type" IN ('Label')
AND "subscriptions"."project_id" IS NULL
AND "subscriptions"."user_id" = 5708766
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
Edited by Terri Chu