- Notifications
You must be signed in to change notification settings - Fork0
A child-eating monster; a library to help you find and eliminate N+1 queries.
License
J3RN/saturn
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A child-eating monster; a library to help you find and eliminate N+1 queries.
In order for Saturn to collect query data, it first must beinstalled. Once that is completed, you may use the following functions:
By default, Saturn starts disabled (i.e. not collecting query data) to save memory. When you'd like to start collecting query data, run:
Saturn.enable()
You can also configure Saturn to be enabled at start-up using theenable
key of its configuration:
config:saturn,enable:true
To stop collecting query data, run:
Saturn.disable()
Once you have collected some data, you can query the most-made queries:
iex>Saturn.report()Query:"SELECT DISTINCT o0.\"queue\" FROM\"public\".\"oban_jobs\" AS o0 WHERE (o0.\"state\" = 'available') AND (NOT (o0.\"queue\" IS NULL))"Count:100Stacktrace:lib/ecto/repo/supervisor.ex:162: Ecto.Repo.Supervisor.tuplet/2lib/my_app/repo.ex:2: MyApp.Repo.all/2lib/oban/plugins/stager.ex:131: Oban.Plugins.Stager.notify_queues/1lib/oban/plugins/stager.ex:98: Oban.Plugins.Stager.-check_leadership_and_stage/1-fun-0-/1lib/ecto/adapters/sql.ex:1202: Ecto.Adapters.SQL.-checkout_or_transaction/4-fun-0-/3...#=> :ok
InvokingSaturn.report()
asSaturn.report(:count)
is equivalent; the argument defaults to:count
.
Saturn also supports querying by time:
iex>Saturn.report(:time)Query:"SELECT DISTINCT o0.\"queue\" FROM\"public\".\"oban_jobs\" AS o0 WHERE (o0.\"state\" = 'available') AND (NOT (o0.\"queue\" IS NULL))"Time:157msStacktrace:lib/ecto/repo/supervisor.ex:162: Ecto.Repo.Supervisor.tuplet/2lib/my_app/repo.ex:2: MyApp.Repo.all/2lib/oban/plugins/stager.ex:131: Oban.Plugins.Stager.notify_queues/1lib/oban/plugins/stager.ex:98: Oban.Plugins.Stager.-check_leadership_and_stage/1-fun-0-/1lib/ecto/adapters/sql.ex:1202: Ecto.Adapters.SQL.-checkout_or_transaction/4-fun-0-/3...#=> :ok
Lastly, Saturn supports a prof-style output (a laeprof
,fprof
, etc) to help you identify cost centers:
iex>Saturn.report(:prof)Source Count%CountTime%TimeSaturn.fake/1266246.9195 SELECT*FROM users;266246.9195Saturn.foobar/213312.354Saturn.Foobar.do_thing/313312.354SELECT*FROMusersWHERE id=5;13312.354
If you want to clear all recorded queries, invokeSaturn.clear/0
:
Saturn.clear()#=> :ok
The package can be installed by addingsaturn
to your list of dependencies inmix.exs
:
defdepsdo[{:saturn,git:"git@github.com:instinctscience/saturn.git"}]end
Next, your application needs to be configured to send Ecto telemetry events to Saturn:
# lib/myapp_web/telemetry.ex:ok=:telemetry.attach("saturn-aggregator",[:myapp,:repo,:query],&Saturn.handle_query/4,nil)
Remember to change:myapp
to the actual name of your app!
Finally, in order for Saturn to collect and report stacktraces, Ecto needs to be configured to include stacktraces in its telemetry events:
# config/dev.exsconfig:myapp,:repo,username:"postgres",password:"postgres",# etcstacktrace:true