[Awesome Ruby Gem] Use activerecord-analyze gem to analyze Active Record query in the PostgreSQL database
activerecord-analyze
activerecord-analyze
adds an analyze method to Active Record query objects. It executes EXPLAIN ANALYZE
on a query SQL.
You can check out this blog post for more info on how to debug and fix slow queries in Rails apps - https://pawelurbanek.com/slow-rails-queries.
The following format options are supported :json, :hash, :yaml, :text, :xml
. Especially the :json
format is useful because it let’s you visualize a query plan using a visualizer tool - https://tatiyants.com/pev/#/plans/new.
Installation
You can install it as a gem:
1 | gem install activerecord-analyze |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usages
Options
The analyze
method supports the following EXPLAIN
query options (PostgreSQL docs reference):
1 | buffers: [ boolean ] |
You can execute it like that:
1 | puts User.all.analyze( |
Optionally you can disable running the ANALYZE
query and only generate the plan:
1 | User.all.analyze(analyze: false) |
References
[4] Postgres EXPLAIN Visualizer (pev) - https://tatiyants.com/pev/#/plans/new
[6] PostgreSQL: The world’s most advanced open source database - https://www.postgresql.org/