DEV Community

madogiwa
madogiwa

Posted on

released gem active_record_json_explain 💎✨

I released gem active_record_json_explain💎✨

https://github.com/Madogiwa0124/active_record_json_explain

This gem extends ActiveRecord::Relation#explain to make it possible to get EXPLAIN in JSON format.(Only supported MySQL and Postgresql)

Usage

Add this line to your application's Gemfile:

gem 'active_record_json_explain', require: false

Add require 'active_record json describe' to the model, and then execute explain (json: true).

require 'active_record_json_explain'

class Sample < ActiveRecord::Base
  scope :with_title, -> { where(title: 'hoge') }
end

Sample.with_title.explain(json: true)
=> EXPLAIN for: SELECT "samples".* FROM "samples" WHERE "samples"."title" = $1 [["title", "hoge"]]
                                                                                                                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[
  {
    "Plan": {
      "Node Type": "Seq Scan",
      "Parallel Aware": false,
      "Relation Name": "samples",
      "Alias": "samples",
      "Startup Cost": 0.00,
      "Total Cost": 11.62,
      "Plan Rows": 1,
      "Plan Width": 556,
      "Filter": "((title)::text = 'hoge'::text)"
    }
  }
]
(1 row)

Top comments (0)