Skip to content

Banzai::Filter::MergeRequestReferenceFilter has three N+1 query problems

@stanhu asked a simple question yesterday: https://gitlab.slack.com/archives/C02PF508L/p1490545717949121

Did you know it takes 20 s to render our CE CHANGELOG.md? Find out why in https://redash.gitlab.com/dashboard/gitlab-profiler-statistics

That sufficed to get me interested! :)

The first thing I did was to open the SQL log and I immediately saw that there was a N+1 query problem for merge requests, but also for namespaces, and projects... Knowing that CHANGELOG.md contains a lot of merge request references, that's definitely a good track.

Then I opened the stackprof results and noticed that ~22% of the total time is spent in 21.79% (30.68%) #<Module:0x007f93795832c0>#find_object_cached [1305 calls, 1427 total]:

Screen_Shot_2017-03-27_at_11.50.39

and then we have 1231 calls to .find_by: 20.79% (99.62%) ActiveRecord::FinderMethods#find_by [1231 calls, 1235 total], bingo!

For the namespace and project N+1 query problem, I actually detected it by using the QueryRecorder.