diff options
author | Stan Hu <stanhu@gmail.com> | 2019-01-20 09:10:20 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-01-20 09:17:29 +0300 |
commit | f9e217872d26c161bc64dc7bb9456b1b22bc3259 (patch) | |
tree | 9f1dacc02ca147eced7309c5bb3ada5fe2f65974 /lib | |
parent | d4d4ebadfb373518013382560b1f505eb6217f13 (diff) |
Eliminate N+1 queries in /api/groups/:id
In https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/15475/diffs, a
significant amount of work went into eliminating N+1 queries in the
/api/groups/:id/projects endpoint. We can reuse the
`Entities::Project.prepare_relation` call on the projects.
In a group with 2,573 projects on GitLab.com, this change significantly
improves performance:
* 18019 SQL queries down to 21
* Time spent in DB: 70 s down to 384 ms
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/49845
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/entities.rb | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 97b421e8651..e0a48908122 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -344,19 +344,23 @@ module API class GroupDetail < Group expose :projects, using: Entities::Project do |group, options| - GroupProjectsFinder.new( + projects = GroupProjectsFinder.new( group: group, current_user: options[:current_user], options: { only_owned: true } ).execute + + Entities::Project.prepare_relation(projects) end expose :shared_projects, using: Entities::Project do |group, options| - GroupProjectsFinder.new( + projects = GroupProjectsFinder.new( group: group, current_user: options[:current_user], options: { only_shared: true } ).execute + + Entities::Project.prepare_relation(projects) end end |