summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToon Claes <toon@gitlab.com>2019-06-03 13:53:48 +0200
committerToon Claes <toon@gitlab.com>2019-06-03 13:56:09 +0200
commitf11fb92ded35feab4d54c1c5e0972d410ec9ca5a (patch)
tree002b09e54a8ad11a91bd2e5764f2dd3fe2af1a6b
parent205f0d0cfe7ff47ead965ff120599dc3f2453974 (diff)
downloadgitlab-ce-tc-db-explain-analyze.tar.gz
Add activerecord-explain-analyze gemtc-db-explain-analyze
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 ... ```
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock4
-rw-r--r--doc/development/understanding_explain_plans.md29
3 files changed, 34 insertions, 0 deletions
diff --git a/Gemfile b/Gemfile
index 66348414506..55e1bd58a38 100644
--- a/Gemfile
+++ b/Gemfile
@@ -403,6 +403,7 @@ gem 'ruby-prof', '~> 0.17.0'
gem 'rbtrace', '~> 0.4', require: false
gem 'memory_profiler', '~> 0.9', require: false
gem 'benchmark-memory', '~> 0.1', require: false
+gem 'activerecord-explain-analyze', '~> 0.1', require: false
# OAuth
gem 'oauth2', '~> 1.4'
diff --git a/Gemfile.lock b/Gemfile.lock
index b869c9923a1..3b37cec3b7f 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -38,6 +38,9 @@ GEM
activemodel (= 5.1.7)
activesupport (= 5.1.7)
arel (~> 8.0)
+ activerecord-explain-analyze (0.1.0)
+ activerecord (>= 4)
+ pg
activerecord_sane_schema_dumper (1.0)
rails (>= 5, < 6)
activesupport (5.1.7)
@@ -1012,6 +1015,7 @@ DEPENDENCIES
RedCloth (~> 4.3.2)
ace-rails-ap (~> 4.1.0)
acme-client (~> 2.0.2)
+ activerecord-explain-analyze (~> 0.1)
activerecord_sane_schema_dumper (= 1.0)
acts-as-taggable-on (~> 6.0)
addressable (~> 2.5.2)
diff --git a/doc/development/understanding_explain_plans.md b/doc/development/understanding_explain_plans.md
index 01a0044f096..a676d6f5fb8 100644
--- a/doc/development/understanding_explain_plans.md
+++ b/doc/development/understanding_explain_plans.md
@@ -653,6 +653,35 @@ and related tools such as:
- <https://explain.depesz.com/>
- <http://tatiyants.com/postgres-query-plan-visualization/>
+## 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: