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

  1. Run specs 🤞
  2. Run profiler on this branch
  3. Run profiler on master
  4. Observe that analyzers are no longer contributing to the runtime. Overall runtime dropped from 20s -> 12s.

Profile

  1. 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
  1. Run spec bin/rspec spec/models/foo_spec.rb
  2. 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
Screenshot_From_2025-11-07_15-43-05 Screenshot_From_2025-11-07_15-44-50

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

Merge request reports

Loading