• Stars
    star
    209
  • Rank 181,574 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 6 years ago
  • Updated 2 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,072
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
351
star
4

rails-brotli-cache

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

normit

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

smart_init

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

ruby-pg-extras

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

node-postgres-extras

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

ecto_extras

Ecto helper functions.
Elixir
36
star
10

devloop

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

lazyme

A simple gem to help you optimize your shell workflow
Ruby
35
star
12

python-pg-extras

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

Locker

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

WaitForIt

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

.dotfiles

My development environment settings.
Shell
13
star
16

haskell-pg-extras

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

ruby-jemalloc-node-yarn

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

railsSearchKit

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

dont_you_count

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

pi-hole-docker-compose

pi-hole-docker-compose
6
star
21

activerecord-implicit-order

Ruby
5
star
22

delegate_it

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

active-admin-tips

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

rust-pg-extras

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

Siorbackend

Ruby
1
star
26

abstract_base

Abstract Class pattern Ruby gem
Ruby
1
star
27

FRP_introduction

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

focus.apki.io

Landing page for Focus app
HTML
1
star