Skip to content

Preload Ci::Runner tags to prevent N+1 queries [RUN ALL RSPEC] [RUN AS-IF-FOSS]

What does this MR do?

Contributes to #21107 (closed)

Preloads Ci::Runner tags on "Settings > CI/CI" page.

In a nutshell, the queries look:

Before

> Ci::Runner.all.map { |r| r.tags }

  Ci::Runner Load (0.8ms)  SELECT "ci_runners".* FROM "ci_runners" 
  ActsAsTaggableOn::Tag Load (0.4ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 1 AND "taggings"."taggable_type" = 'Ci::Runner' AND "taggings"."context" = 'tags' 
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 2 AND "taggings"."taggable_type" = 'Ci::Runner' AND "taggings"."context" = 'tags' 
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 3 AND "taggings"."taggable_type" = 'Ci::Runner' AND "taggings"."context" = 'tags' 
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 4 AND "taggings"."taggable_type" = 'Ci::Runner' AND "taggings"."context" = 'tags' 
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 5 AND "taggings"."taggable_type" = 'Ci::Runner' AND "taggings"."context" = 'tags' 
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 6 AND "taggings"."taggable_type" = 'Ci::Runner' AND "taggings"."context" = 'tags' 

After

> Ci::Runner.all.with_tags.map { |r| r.tags }

  Ci::Runner Load (0.3ms)  SELECT "ci_runners".* FROM "ci_runners" 
  ActsAsTaggableOn::Tagging Load (0.4ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_type" = 'Ci::Runner' AND "taggings"."context" = 'tags' AND "taggings"."taggable_id" IN (1, 2, 3, 4, 5, 6)
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" IN (5, 4, 3, 2, 1) 

Screenshots (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

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 Vitali Tatarintev

Merge request reports