GraphQL - Preload parent in group epics query
What does this MR do?
Related to #210553 (closed)
Fixes N+1 queries for the parent field in group epics by using the Lookahead feature to preload the field.
After this change, we can see the number of queries against the epics
table reduced:
Query
{
group(fullPath: "gitlab-org") {
epics {
edges {
node {
parent {
title
}
}
}
}
}
}
Before preloading field
Started POST "/api/graphql" for 127.0.0.1 at 2020-09-24 15:13:45 +0100
Processing by GraphqlController#execute as */*
Parameters: {"query"=>"query {\n group(fullPath: \"gitlab-org\") {\n epics {\n edges {\n node {\n iid\n parent {\n title\n }\n }\n }\n }\n }\n}", "variables"=>"[FILTERED]", "graphql"=>{"query"=>"query {\n group(fullPath: \"gitlab-org\") {\n epics {\n edges {\n node {\n iid\n parent {\n title\n }\n }\n }\n }\n }\n}", "variables"=>"[FILTERED]"}}
[1m[36mUser Load (0.7ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
↳ lib/gitlab/graphql/loaders/full_path_model_loader.rb:18:in `block in find'
[1m[36mLicense Load (0.3ms)[0m [1m[34mSELECT "licenses".* FROM "licenses" ORDER BY "licenses"."id" DESC LIMIT $1[0m [["LIMIT", 100]]
↳ ee/app/models/license.rb:258:in `load_license'
[1m[36mIpRestriction Load (0.3ms)[0m [1m[34mSELECT "ip_restrictions".* FROM "ip_restrictions" WHERE "ip_restrictions"."group_id" = $1[0m [["group_id", 22]]
↳ ee/lib/gitlab/ip_restriction/enforcer.rb:31:in `allows_address?'
[1m[36mGroup Load (0.6ms)[0m [1m[34mSELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = $1 AND "namespaces"."id" = $2 LIMIT $3[0m [["type", "Group"], ["id", 22], ["LIMIT", 1]]
↳ ee/app/finders/epics_finder.rb:142:in `block in group'
[1m[36mEpic Load (1.9ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."group_id" IN (WITH RECURSIVE "base_and_descendants" AS ((SELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = 'Group' AND "namespaces"."id" = 22)
UNION
(SELECT "namespaces".* FROM "namespaces", "base_and_descendants" WHERE "namespaces"."type" = 'Group' AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT "namespaces"."id" FROM "base_and_descendants" AS "namespaces") ORDER BY "epics"."id" DESC LIMIT $1[0m [["LIMIT", 2000]]
↳ lib/gitlab/graphql/pagination/keyset/connection.rb:100:in `nodes'
[1m[36mGroup Load (0.5ms)[0m [1m[34mSELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = $1 AND "namespaces"."id" = $2[0m [["type", "Group"], ["id", 22]]
↳ lib/gitlab/graphql/pagination/keyset/connection.rb:100:in `nodes'
[1m[36mEpic Load (0.3ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" = $1[0m [["id", 80]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.4ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" = $1[0m [["id", 0]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.4ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" = $1[0m [["id", 78]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.5ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2)[0m [["id", 0], ["id", 76]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.4ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2)[0m [["id", 0], ["id", 74]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.4ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2)[0m [["id", 0], ["id", 70]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.5ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" = $1[0m [["id", 71]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.4ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2)[0m [["id", 0], ["id", 64]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.4ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2, $3)[0m [["id", 64], ["id", 0], ["id", 53]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.5ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2, $3)[0m [["id", 53], ["id", 0], ["id", 47]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.5ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2, $3)[0m [["id", 47], ["id", 0], ["id", 41]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
[1m[36mEpic Load (0.5ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2, $3)[0m [["id", 41], ["id", 0], ["id", 1]]
↳ lib/gitlab/graphql/loaders/batch_model_loader.rb:21:in `block (2 levels) in find'
Completed 200 OK in 2690ms (Views: 1.0ms | ActiveRecord: 58.3ms | Elasticsearch: 0.0ms | Allocations: 2202355)
After preloading field
Started POST "/api/graphql" for 127.0.0.1 at 2020-09-24 15:15:39 +0100
Processing by GraphqlController#execute as */*
Parameters: {"query"=>"query {\n group(fullPath: \"gitlab-org\") {\n epics {\n edges {\n node {\n iid\n parent {\n title\n }\n }\n }\n }\n }\n}", "variables"=>"[FILTERED]", "graphql"=>{"query"=>"query {\n group(fullPath: \"gitlab-org\") {\n epics {\n edges {\n node {\n iid\n parent {\n title\n }\n }\n }\n }\n }\n}", "variables"=>"[FILTERED]"}}
↳ lib/gitlab/graphql/loaders/full_path_model_loader.rb:18:in `block in find'
[1m[36mLicense Load (0.3ms)[0m [1m[34mSELECT "licenses".* FROM "licenses" ORDER BY "licenses"."id" DESC LIMIT $1[0m [["LIMIT", 100]]
↳ ee/app/models/license.rb:258:in `load_license'
[1m[36mIpRestriction Load (0.6ms)[0m [1m[34mSELECT "ip_restrictions".* FROM "ip_restrictions" WHERE "ip_restrictions"."group_id" = $1[0m [["group_id", 22]]
↳ ee/lib/gitlab/ip_restriction/enforcer.rb:31:in `allows_address?'
[1m[36mGroup Load (0.6ms)[0m [1m[34mSELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = $1 AND "namespaces"."id" = $2 LIMIT $3[0m [["type", "Group"], ["id", 22], ["LIMIT", 1]]
↳ ee/app/finders/epics_finder.rb:142:in `block in group'
[1m[36mEpic Load (1.7ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."group_id" IN (WITH RECURSIVE "base_and_descendants" AS ((SELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = 'Group' AND "namespaces"."id" = 22)
UNION
(SELECT "namespaces".* FROM "namespaces", "base_and_descendants" WHERE "namespaces"."type" = 'Group' AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT "namespaces"."id" FROM "base_and_descendants" AS "namespaces") ORDER BY "epics"."id" DESC LIMIT $1[0m [["LIMIT", 2000]]
↳ lib/gitlab/graphql/pagination/keyset/connection.rb:100:in `nodes'
[1m[36mGroup Load (0.4ms)[0m [1m[34mSELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = $1 AND "namespaces"."id" = $2[0m [["type", "Group"], ["id", 22]]
↳ lib/gitlab/graphql/pagination/keyset/connection.rb:100:in `nodes'
[1m[36mEpic Load (0.5ms)[0m [1m[34mSELECT "epics".* FROM "epics" WHERE "epics"."id" IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)[0m [["id", 80], ["id", 78], ["id", 76], ["id", 74], ["id", 70], ["id", 71], ["id", 64], ["id", 53], ["id", 47], ["id", 41], ["id", 1]]
↳ lib/gitlab/graphql/pagination/keyset/connection.rb:100:in `nodes'
Completed 200 OK in 2765ms (Views: 1.0ms | ActiveRecord: 52.6ms | Elasticsearch: 0.0ms | Allocations: 2150721)
Screenshots
Does this MR meet the acceptance criteria?
Conformity
-
Changelog entry -
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 Eugenia Grieff