• Stars
    star
    217
  • Rank 182,446 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 6 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add EXPLAIN ANALYZE to Rails Active Record query objects

ActiveRecord Analyze Gem Version GH Actions

This gem 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.

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.

PG Query visualizer plan

Installation

In your Gemfile:

gem 'activerecord-analyze'

Options

The analyze method supports the following EXPLAIN query options (PostgreSQL docs reference):

buffers: [ boolean ]
verbose: [ boolean ]
costs: [ boolean ]
settings: [ boolean ]
timing: [ boolean ]
summary: [ boolean ]
format: { :text | :json | :xml | :yaml | :pretty_json }

You can execute it like that:

puts User.all.analyze(
  format: :pretty_json, # :pretty_json format option generates a formatted JSON output
  verbose: true,
  costs: true,
  settings: true,
  buffers: true,
  timing: true,
  summary: true
)

# EXPLAIN (FORMAT JSON, ANALYZE, VERBOSE, COSTS, SETTINGS, BUFFERS, TIMING, SUMMARY)
# SELECT "users".* FROM "users"
# [
#   {
#     "Plan": {
#       "Node Type": "Seq Scan",
#       "Parallel Aware": false,
#       "Relation Name": "users",
#       "Schema": "public",
#       "Alias": "users",
#       "Startup Cost": 0.00,
#       "Total Cost": 11.56,
#       "Plan Rows": 520,
#       "Plan Width": 127,
#       "Actual Startup Time": 0.006,
#       "Actual Total Time": 0.007,
#       "Actual Rows": 2,
#       "Actual Loops": 1,
#       "Output": ["id", "team_id", "email"],
#       "Shared Hit Blocks": 1,
#       "Shared Read Blocks": 0,
#       "Shared Dirtied Blocks": 0,
#       "Shared Written Blocks": 0,
#       "Local Hit Blocks": 0,
#       "Local Read Blocks": 0,
#       "Local Dirtied Blocks": 0,
#       "Local Written Blocks": 0,
#       "Temp Read Blocks": 0,
#       "Temp Written Blocks": 0,
#       "I/O Read Time": 0.000,
#       "I/O Write Time": 0.000
#     },
#     "Settings": {
#       "cpu_index_tuple_cost": "0.001",
#       "cpu_operator_cost": "0.0005",
#       "cpu_tuple_cost": "0.003",
#       "effective_cache_size": "10800000kB",
#       "max_parallel_workers_per_gather": "1",
#       "random_page_cost": "2",
#       "work_mem": "100MB"
#     },
#     "Planning Time": 0.033,
#     "Triggers": [
#     ],
#     "Execution Time": 0.018
#   }
# ]

Optionally you can disable running the ANALYZE query and only generate the plan:

User.all.analyze(analyze: false)

# EXPLAIN ANALYZE for: SELECT "users".* FROM "users"
#                         QUERY PLAN
# ----------------------------------------------------------
#  Seq Scan on users  (cost=0.00..15.20 rows=520 width=127)

Analyzing raw SQL queries

You can also use a raw SQL query string to generate an EXPLAIN ANALYZE output:

query = "SELECT * FROM users WHERE email = '[email protected]'"

puts ActiveRecordAnalyze.analyze_sql(query, { format: :json })

# [
# {
#    "Plan": {
#      "Node Type": "Seq Scan",
#      "Parallel Aware": false,
#      "Relation Name": "users",
#      "Alias": "users",
#      "Startup Cost": 0.00,
#      "Total Cost": 18.75,
#      "Plan Rows": 4,
#      "Plan Width": 88,
#      "Actual Startup Time": 0.010,
#      "Actual Total Time": 0.018,
#      "Actual Rows": 0,
#      "Actual Loops": 1,
#      "Filter": "((email)::text = '[email protected]'::text)",
#      "Rows Removed by Filter": 0
#    },
#    "Planning Time": 0.052,
#    "Triggers": [
#    ],
#    "Execution Time": 0.062
# }
# ]

This feature is helpful in analyzing SQL queries extracted from the logs.

Disclaimer

It is a bit experimental and can break with new Rails release.

More Repositories

1

rails-pg-extras

Rails PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Ruby
1,128
star
2

termit

Translations with speech synthesis in your terminal as a ruby gem
Ruby
507
star
3

ecto_psql_extras

Ecto PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Elixir
359
star
4

rails-brotli-cache

Drop-in enhancement for Rails cache, offering better performance and compression with Brotli algorithm
Ruby
250
star
5

normit

Translations with speech synthesis in your terminal as a node package
JavaScript
240
star
6

smart_init

A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API
Ruby
179
star
7

ruby-pg-extras

Ruby PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Ruby
127
star
8

node-postgres-extras

NodeJS PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
JavaScript
71
star
9

univ3-revm-arbitrage

Uniswap V3 MEV arbitrage calculations with REVM
Rust
50
star
10

pg-locks-monitor

A simple tool to observe PostgreSQL database locks in Rails apps.
Ruby
50
star
11

devloop

An automated test runner for Rails that instantly executes specs based on a recent git diff output.
Ruby
41
star
12

ecto_extras

Ecto helper functions.
Elixir
38
star
13

lazyme

A simple gem to help you optimize your shell workflow
Ruby
37
star
14

python-pg-extras

Python PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Python
34
star
15

Locker

Ethereum Smart Contracts for locking Ether, ERC20 and ERC721 tokens based on time and price conditions
TypeScript
31
star
16

WaitForIt

Events and time based iOS app scenarios made easy.
Swift
26
star
17

.dotfiles

My development environment settings.
Shell
13
star
18

haskell-pg-extras

Haskell PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Haskell
9
star
19

ruby-jemalloc-node-yarn

Docker image of Ruby with Jemalloc Node 16 LTS and Yarn
Dockerfile
9
star
20

railsSearchKit

This Chrome extension provides easy access to the search bars every Rails developer needs.
JavaScript
8
star
21

mev-gas-optimization

Optimize MEV Arbitrage Smart Contract with Yul and Huff
Rust
7
star
22

dont_you_count

Disable count queries for selected Active Admin tables.
Ruby
7
star
23

pi-hole-docker-compose

pi-hole-docker-compose
7
star
24

rust-pg-extras

Rust PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Rust
6
star
25

activerecord-implicit-order

Ruby
5
star
26

delegate_it

A drop in replacement for ActiveSupport delegate method in non Rails projects.
Ruby
4
star
27

active-admin-tips

Active Admin tips and performance optimizations in action
Ruby
4
star
28

Siorbackend

Ruby
1
star
29

FRP_introduction

Comparison between observer and reactive approach to login form validations.
JavaScript
1
star
30

focus.apki.io

Landing page for Focus app
HTML
1
star
31

abstract_base

Abstract Class pattern Ruby gem
Ruby
1
star