From be472673313680d62ff9d3b4bb81976e714dd8cd Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Mon, 3 Jun 2019 13:53:48 +0200 Subject: Add activerecord-explain-analyze gem This gem allows you to get the `EXPLAIN ANALYZE` query plan, directly from the Rails console. The gem is installed with `require: false`, but if it was loaded on launch, this would be it's memory load: ``` TOP: 145.3086 MiB rails/all: 22.4844 MiB ... activerecord-explain-analyze: 2.9648 MiB active_record/connection_adapters/postgresql_adapter: 2.9648 MiB pg: 2.9648 MiB pg_ext: 2.9648 MiB ... ``` --- doc/development/understanding_explain_plans.md | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'doc/development/understanding_explain_plans.md') diff --git a/doc/development/understanding_explain_plans.md b/doc/development/understanding_explain_plans.md index 0abd86d4b4c..2ef8b3148e4 100644 --- a/doc/development/understanding_explain_plans.md +++ b/doc/development/understanding_explain_plans.md @@ -654,6 +654,35 @@ and related tools such as: - - +## Producing query plans + +There are a few ways to get the output of a query plan. Of course you +can directly run the `EXPLAIN` query in the `psql` console, or you can +follow one of the other options below. + +### Rails console + +Using the [`activerecord-explain-analyze`](https://github.com/6/activerecord-explain-analyze) +you can directly generate the query plan from the Rails console: + +```ruby +pry(main)> require 'activerecord-explain-analyze' +=> true +pry(main)> Project.where('build_timeout > ?', 3600).explain(analyze: true) + Project Load (1.9ms) SELECT "projects".* FROM "projects" WHERE (build_timeout > 3600) + ↳ (pry):12 +=> EXPLAIN for: SELECT "projects".* FROM "projects" WHERE (build_timeout > 3600) +Seq Scan on public.projects (cost=0.00..2.17 rows=1 width=742) (actual time=0.040..0.041 rows=0 loops=1) + Output: id, name, path, description, created_at, updated_at, creator_id, namespace_id, ... + Filter: (projects.build_timeout > 3600) + Rows Removed by Filter: 14 + Buffers: shared hit=2 +Planning time: 0.411 ms +Execution time: 0.113 ms +``` + +### Chatops + GitLab employees can also use our chatops solution, available in Slack using the `/chatops` slash command. You can use chatops to get a query plan by running the following: -- cgit v1.2.1