Pipelines Controller has 4 queries repeated O(n) times for n jobs (i.e. "n+1" queries)
Viewing the pipeline from a merge request e.g. https://gitlab.com/gitlab-org/gitlab-ce/pipelines/16118586
These four queries are repeated once for every job. They should be replaced with four queries (or fewer) which fetch all the needed information for the relevant jobs all in one result set each.
86 SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 999 AND "taggings"."taggable_type" = 'CommitStatus' AND "taggings"."context" = 'tags'
88 SELECT 1 AS one FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 999 AND "taggings"."taggable_type" = 'CommitStatus' AND "taggings"."context" = 'tags' LIMIT 1
88 SELECT "ci_job_artifacts".* FROM "ci_job_artifacts" WHERE "ci_job_artifacts"."job_id" = 999 AND "ci_job_artifacts"."file_type" = 1 LIMIT 1
88 SELECT "ci_pipelines".* FROM "ci_pipelines" WHERE "ci_pipelines"."id" = 16118586 LIMIT 1
Even though each of these queries is super-fast, ranging from under a millisecond up to about 2ms the totals for these 88 repetitions is substantial:
89.125ms SELECT "ci_job_artifacts".* FROM "ci_job_artifacts" WHERE "ci_job_artifacts"."job_id" = 47840701 AND "ci_job_artifacts"."file_type" = 1 LIMIT 1
93.573ms SELECT "ci_pipelines".* FROM "ci_pipelines" WHERE "ci_pipelines"."id" = 16118586 LIMIT 1
111.425ms SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 47840697 AND "taggings"."taggable_type" = 'CommitStatus' AND "taggings"."context" = 'tags'
187.387ms SELECT 1 AS one FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 47840714 AND "taggings"."taggable_type" = 'CommitStatus' AND "taggings"."context" = 'tags' LIMIT 1
The first two can probably be combined into a single query. The last is actually entirely identical and is just being executed repeatedly for no reason.
Edited by Gregory Stark