N+1 queries in IssuesController#show due to `cross_reference_not_visible_for?` call
Following https://gitlab.com/gitlab-org/gitlab-ce/issues/37952, you can see lots of N+1 queries in the IssuesController#show
method due to a cross_reference_not_visible_for?
for every note. For example:
D, [2017-09-16T06:23:28.902460 #8944] DEBUG -- : User Load (1.9ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (444)
D, [2017-09-16T06:23:28.914665 #8944] DEBUG -- : ↳ lib/banzai/reference_parser/base_parser.rb:171:in `collection_objects_for_ids'
D, [2017-09-16T06:23:28.915170 #8944] DEBUG -- : ↳ lib/banzai/reference_parser/base_parser.rb:134:in `grouped_objects_for_nodes'
D, [2017-09-16T06:23:28.915503 #8944] DEBUG -- : ↳ lib/banzai/reference_parser/issue_parser.rb:24:in `issues_for_nodes'
D, [2017-09-16T06:23:28.921646 #8944] DEBUG -- : ↳ lib/banzai/reference_parser/issue_parser.rb:7:in `nodes_visible_to_user'
D, [2017-09-16T06:23:28.922076 #8944] DEBUG -- : ↳ lib/banzai/reference_parser/base_parser.rb:201:in `gather_references'
D, [2017-09-16T06:23:28.922412 #8944] DEBUG -- : ↳ lib/banzai/reference_parser/base_parser.rb:195:in `process'
D, [2017-09-16T06:23:28.922733 #8944] DEBUG -- : ↳ lib/banzai/reference_extractor.rb:16:in `references'
D, [2017-09-16T06:23:28.922956 #8944] DEBUG -- : ↳ lib/gitlab/reference_extractor.rb:20:in `references'
D, [2017-09-16T06:23:28.923286 #8944] DEBUG -- : ↳ lib/gitlab/reference_extractor.rb:43:in `issues'
D, [2017-09-16T06:23:28.923684 #8944] DEBUG -- : ↳ app/models/concerns/mentionable.rb:85:in `referenced_mentionables'
D, [2017-09-16T06:23:28.924035 #8944] DEBUG -- : ↳ app/models/note.rb:260:in `has_referenced_mentionables?'
D, [2017-09-16T06:23:28.924380 #8944] DEBUG -- : ↳ app/models/note.rb:253:in `cross_reference_not_visible_for?'
D, [2017-09-16T06:23:28.924721 #8944] DEBUG -- : ↳ app/controllers/projects/issues_controller.rb:92:in `block in discussions'
D, [2017-09-16T06:23:28.924980 #8944] DEBUG -- : ↳ app/controllers/projects/issues_controller.rb:92:in `discussions'
D, [2017-09-16T06:23:28.925328 #8944] DEBUG -- : ↳ app/controllers/application_controller.rb:334:in `set_locale'
http://profiler.gitlap.com/20170916/343200e6-062a-49b9-bb09-43f77dcf0787.txt.gz
prepare_notes_for_rendering
does a log of legwork to optimize this already, and we might able to optimize this call simply by changing the call as such:
diff --git a/app/controllers/projects/issues_controller.rb b/app/controllers/projects/issues_controller.rb
index 8990c919ca..42bfa4b9d4 100644
--- a/app/controllers/projects/issues_controller.rb
+++ b/app/controllers/projects/issues_controller.rb
@@ -87,9 +87,9 @@ class Projects::IssuesController < Projects::ApplicationController
.inc_relations_for_view
.includes(:noteable)
.fresh
- .reject { |n| n.cross_reference_not_visible_for?(current_user) }
- prepare_notes_for_rendering(notes)
+ notes = prepare_notes_for_rendering(notes)
+ notes = notes.reject { |n| n.cross_reference_not_visible_for?(current_user) }
discussions = Discussion.build_collection(notes, @issue)
/cc: @DouweM
Edited by Stan Hu