Catches N+1 queries while your tests run and while you use the app in dev.
It reports a query that ran at least :threshold times, two by default, from the same call stack in one unit of work.
Each test, request, LiveView event and Oban job is a unit.
Only reads count, never inserts, updates or deletes.
Turn on stack traces for the repo in config/dev.exs and config/test.exs so findings point at your code.
config :my_app, MyApp.Repo, stacktrace: trueAttach it in test/test_helper.exs and scan every test from your case templates.
EctoDoctor.RepeatedQueries.attach(repo: MyApp.Repo)
# in the setup of DataCase and ConnCase
setup tags do
:ok = EctoDoctor.RepeatedQueries.scan_test(tags)
MyApp.DataCase.setup_sandbox(tags)
:ok
endA test tagged @tag repeated_queries: false isn't scanned.
mix ecto_doctor.install does all of this for you.
Adopting it in an existing app
Until the baseline file exists findings only print.
Run ECTO_DOCTOR_RECORD=1 mix test once to record the N+1 queries you have today.
From then on only new ones fail a test.
Options
:repo- Required. Names the repo to watch or a list of repos.:threshold(pos_integer/0) - Sets how many identical queries make an N+1. The default value is2.:mode- Chooses whether a test with a new N+1 fails or only prints it. The default value is:raise.:baseline- Leaves out the N+1 queries listed in this file. The default value is".ecto_doctor_baseline.exs".:apps(list ofatom/0) - Picks the apps whose code counts in call stacks. Defaults to the repos' apps.:allow(list ofString.t/0) - Ignores N+1 queries whose call stack has a file path containing one of these. The default value is[].:focus(list ofString.t/0) - Reports only N+1 queries whose call stack has a file path containing one of these. The default value is[].:ignore_queries(list ofString.t/0) - Ignores queries whose SQL contains one of these. The default value is[].:ignore_frames(list ofatom/0) - Leaves these modules out of the compared call stacks, such as wrappers around Repo. The default value is[].:min_duration_ms(non_neg_integer/0) - Reports only N+1 queries that took at least this long in total. The default value is0.:sample_rate- Sets the share of requests and jobs to scan, from 0 to 1. Tests are always scanned. The default value is1.0.:log(boolean/0) - Prints each N+1 to standard error. The default value istrue.:github_annotations(boolean/0) - Prints GitHub Actions annotations. On by default inside GitHub Actions.:json_report- Appends each N+1 to this file as a line of JSON. The default value isnil.
Telemetry
Each finding emits [:ecto_doctor, :repeated_query] with :count and :duration_ms.
Its metadata holds :sql, :frames, :fix, :key and :label.
Summary
Functions
Starts watching the repo's queries.
Stops watching.
Runs the function in a scan of its own and returns its N+1 queries without reporting them.
Leaves the code in the function out of the scan it runs in and returns what the function returns.
Scans the code in the function and reports its N+1 queries without raising.
Scans one ExUnit test and fails it on a new N+1 query.
Functions
@spec attach(keyword()) :: :ok
Starts watching the repo's queries.
Call it once in test/test_helper.exs or when your app starts in dev.
@spec detach() :: :ok
Stops watching.
@spec findings((-> term())) :: [EctoDoctor.RepeatedQueries.Finding.t()]
Runs the function in a scan of its own and returns its N+1 queries without reporting them.
The assertions in EctoDoctor.RepeatedQueries.Assertions use it.
@spec pause((-> result)) :: result when result: term()
Leaves the code in the function out of the scan it runs in and returns what the function returns.
Scans the code in the function and reports its N+1 queries without raising.
The label shows in each finding. It returns what the function returns.
EctoDoctor.RepeatedQueries.scan("nightly import", &Importer.run_nightly/0)
@spec scan_test(map()) :: :ok
Scans one ExUnit test and fails it on a new N+1 query.
setup {EctoDoctor.RepeatedQueries, :scan_test}