Skip database analyzers for cached queries by default
What does this MR do and why?
Cached database queries are skipped by default, even if they require
tracking. Analyzers can override this behavior in skip_cached?
method.
References
Slow cached queries were found by @ahegyi randomly. Flamegraph showed that analyzers were on hot path of cached queries:
How to set up and validate locally
- Run specs
🤞 - Run profiler on this branch
- Run profiler on
master - Observe that analyzers are no longer contributing to the runtime. Overall runtime dropped from 20s -> 12s.
Profile
- Add spec
spec/models/foo_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe "Foo" do
let(:classes) do
[
Project,
User,
Issue,
MergeRequest,
UserDetail,
Route,
Namespace
]
end
before do
ActiveRecord::Base.connection_pool.enable_query_cache!
p warmup: ActiveRecord::Base.connection_pool.query_cache_enabled
classes.each(&:first)
end
it "profiles", :stackprof do
p profile: ActiveRecord::Base.connection_pool.query_cache_enabled
1000.times { classes.each(&:first) }
end
end
- Run spec
bin/rspec spec/models/foo_spec.rb - Review flamegraph via speedscope (
npm install -g speedscope):speedscope tmp/stackprof_spec_models_foo_spec_5.json.gz
| This branch | master |
|---|---|
Finished in 11.05 seconds (files took 15.64 seconds to load) |
Finished in 19.15 seconds (files took 15.77 seconds to load) |
| stackprof_spec_models_foo_spec_5.json.gz | stackprof_spec_models_foo_spec_5.json.gz |
|
|
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Edited by Peter Leitzen

