activerecord-analyze 0.2.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 226e12a1f8d3b7be66168f555950dff9fb7f1ab8
4
- data.tar.gz: 53feb1e87fb720e691586f13cf01963d2eec8ac6
2
+ SHA256:
3
+ metadata.gz: ef55b677ccdab275d0b76db680c55e8c8534b7e03245dc76f622eca81cb3b244
4
+ data.tar.gz: 3ee9e2dc7ce0251cab913d79ee3c5b5760b571d27bdf98b132cb8d8751c7c7da
5
5
  SHA512:
6
- metadata.gz: dde5e026fc7e5e26ca627157078666dc62dbd15bbae2ed517a6060129a1bcb13d344da26de108cf1344068d6ba4ce37892e4ca98062121c517d068a063ffba47
7
- data.tar.gz: effb4506e5be439b594f536a4bb30e57133e28445d63b78abef153c3f9386a8e5a669e58e9f6a238caad8b73eb3dd6697c953d3cfd88d7e689aa303496c37f61
6
+ metadata.gz: 5a1057165667e384853cc9fc83e98b4c98377c2af41057cbe52427cd7a25b8fff99f50321435e4bab1645be97e2d989e026fe3ba85d0d825e53992d1d373ac3b
7
+ data.tar.gz: 43019fbfa8dff77b956e62eba3750f63b4925eb179876348750414a77c91c0406892a12a3eb719d51e100b8e00e7274a7b41db0054c06d9a061a8887b4be807a
@@ -0,0 +1,30 @@
1
+ version: 2
2
+ jobs:
3
+ test:
4
+ docker:
5
+ - image: circleci/ruby:2.6.5
6
+ environment:
7
+ DATABASE_URL: postgresql://postgres:secret@localhost:5432/activerecord-analyze-test
8
+ - image: circleci/postgres:11.5
9
+ environment:
10
+ POSTGRES_USER: postgres
11
+ POSTGRES_DB: activerecord-analyze-test
12
+ POSTGRES_PASSWORD: secret
13
+ parallelism: 1
14
+ steps:
15
+ - checkout
16
+ - run: gem update --system
17
+ - run: gem install bundler
18
+ - run: bundle install --path vendor/bundle
19
+ - run: sudo apt-get update
20
+ - run: sudo apt install postgresql-client-11
21
+ - run: dockerize -wait tcp://localhost:5432 -timeout 1m
22
+ - run:
23
+ name: Run specs
24
+ command: |
25
+ bundle exec rspec spec/
26
+ workflows:
27
+ version: 2
28
+ test:
29
+ jobs:
30
+ - test
data/README.md CHANGED
@@ -1,2 +1,114 @@
1
- # ActiveRecord Analyze
1
+ # ActiveRecord Analyze [![Gem Version](https://badge.fury.io/rb/activerecord-analyze.svg)](https://badge.fury.io/rb/ruby-pg-extras) [![CircleCI](https://circleci.com/gh/pawurb/activerecord-analyze.svg?style=svg)](https://circleci.com/gh/pawurb/activerecord-analyze)
2
2
 
3
+ This gem adds an `analyze` method to Active Record query objects. It executes `EXPLAIN ANALYZE` on a query SQL.
4
+
5
+ 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).
6
+
7
+ 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).
8
+
9
+ ![PG Query visualizer plan](https://raw.githubusercontent.com/pawurb/activerecord-analyze/master/query-plan.png)
10
+
11
+ ## Installation
12
+
13
+ In your Gemfile:
14
+
15
+ ```ruby
16
+
17
+ gem 'activerecord-analyze'
18
+
19
+ ```
20
+
21
+ ## Options
22
+
23
+ The `analyze` method supports the following EXPLAIN query options ([PostgreSQL docs reference](https://www.postgresql.org/docs/12/sql-explain.html)):
24
+
25
+ ```
26
+ VERBOSE [ boolean ]
27
+ COSTS [ boolean ]
28
+ SETTINGS [ boolean ]
29
+ BUFFERS [ boolean ]
30
+ TIMING [ boolean ]
31
+ SUMMARY [ boolean ]
32
+ FORMAT { TEXT | XML | JSON | YAML }
33
+ ```
34
+
35
+ You can execute it like that:
36
+
37
+ ```ruby
38
+
39
+ User.all.analyze(
40
+ format: :json,
41
+ verbose: true,
42
+ costs: true,
43
+ settings: true,
44
+ buffers: true,
45
+ timing: true,
46
+ summary: true
47
+ )
48
+
49
+ # EXPLAIN (FORMAT JSON, ANALYZE, VERBOSE, COSTS, SETTINGS, BUFFERS, TIMING, SUMMARY)
50
+ # SELECT "users".* FROM "users"
51
+ # [
52
+ # {
53
+ # "Plan": {
54
+ # "Node Type": "Seq Scan",
55
+ # "Parallel Aware": false,
56
+ # "Relation Name": "users",
57
+ # "Schema": "public",
58
+ # "Alias": "users",
59
+ # "Startup Cost": 0.00,
60
+ # "Total Cost": 11.56,
61
+ # "Plan Rows": 520,
62
+ # "Plan Width": 127,
63
+ # "Actual Startup Time": 0.006,
64
+ # "Actual Total Time": 0.007,
65
+ # "Actual Rows": 2,
66
+ # "Actual Loops": 1,
67
+ # "Output": ["id", "team_id", "email"],
68
+ # "Shared Hit Blocks": 1,
69
+ # "Shared Read Blocks": 0,
70
+ # "Shared Dirtied Blocks": 0,
71
+ # "Shared Written Blocks": 0,
72
+ # "Local Hit Blocks": 0,
73
+ # "Local Read Blocks": 0,
74
+ # "Local Dirtied Blocks": 0,
75
+ # "Local Written Blocks": 0,
76
+ # "Temp Read Blocks": 0,
77
+ # "Temp Written Blocks": 0,
78
+ # "I/O Read Time": 0.000,
79
+ # "I/O Write Time": 0.000
80
+ # },
81
+ # "Settings": {
82
+ # "cpu_index_tuple_cost": "0.001",
83
+ # "cpu_operator_cost": "0.0005",
84
+ # "cpu_tuple_cost": "0.003",
85
+ # "effective_cache_size": "10800000kB",
86
+ # "max_parallel_workers_per_gather": "1",
87
+ # "random_page_cost": "2",
88
+ # "work_mem": "100MB"
89
+ # },
90
+ # "Planning Time": 0.033,
91
+ # "Triggers": [
92
+ # ],
93
+ # "Execution Time": 0.018
94
+ # }
95
+ # ]
96
+
97
+ ```
98
+
99
+ Optionally you can disable running the `ANALYZE` query and only generate the plan:
100
+
101
+ ```ruby
102
+
103
+ User.all.analyze(analyze: false)
104
+
105
+ # EXPLAIN ANALYZE for: SELECT "users".* FROM "users"
106
+ # QUERY PLAN
107
+ # ----------------------------------------------------------
108
+ # Seq Scan on users (cost=0.00..15.20 rows=520 width=127)
109
+
110
+ ```
111
+
112
+ ### Disclaimer
113
+
114
+ It is a bit experimental and can break with new Rails release.
@@ -15,5 +15,8 @@ Gem::Specification.new do |gem|
15
15
  gem.test_files = gem.files.grep(%r{^(spec)/})
16
16
  gem.require_paths = ["lib"]
17
17
  gem.license = "MIT"
18
- gem.add_dependency "rails", "~> 5.1.0"
18
+ gem.add_dependency "rails", ">= 5.1.0"
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "pg"
21
+ gem.add_development_dependency "rspec"
19
22
  end
@@ -0,0 +1,11 @@
1
+ version: '3'
2
+
3
+ services:
4
+ postgres:
5
+ image: postgres:11.5-alpine
6
+ environment:
7
+ POSTGRES_USER: postgres
8
+ POSTGRES_DB: ruby-pg-extras-test
9
+ POSTGRES_PASSWORD: secret
10
+ ports:
11
+ - '5432:5432'
@@ -2,9 +2,60 @@ module ActiveRecord
2
2
  module ConnectionAdapters
3
3
  module PostgreSQL
4
4
  module DatabaseStatements
5
- def analyze(arel, binds = [])
6
- sql = "EXPLAIN ANALYZE #{to_sql(arel, binds)}"
7
- PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN ANALYZE", binds))
5
+ def analyze(arel, binds = [], opts = {})
6
+ format_sql = if fmt = opts[:format].presence
7
+ case fmt
8
+ when :json
9
+ "FORMAT JSON,"
10
+ when :hash
11
+ "FORMAT JSON,"
12
+ when :yaml
13
+ "FORMAT YAML,"
14
+ when :text
15
+ "FORMAT TEXT,"
16
+ when :xml
17
+ "FORMAT XML,"
18
+ end
19
+ end
20
+
21
+ verbose_sql = if opts[:verbose] == true
22
+ ", VERBOSE"
23
+ end
24
+
25
+ costs_sql = if opts[:costs] == true
26
+ ", COSTS"
27
+ end
28
+
29
+ settings_sql = if opts[:settings] == true
30
+ ", SETTINGS"
31
+ end
32
+
33
+ buffers_sql = if opts[:buffers] == true
34
+ ", BUFFERS"
35
+ end
36
+
37
+ timing_sql = if opts[:timing] == true
38
+ ", TIMING"
39
+ end
40
+
41
+ summary_sql = if opts[:summary] == true
42
+ ", SUMMARY"
43
+ end
44
+
45
+ analyze_sql = if opts[:analyze] == false
46
+ ""
47
+ else
48
+ "ANALYZE"
49
+ end
50
+
51
+ opts_sql = "(#{format_sql} #{analyze_sql}#{verbose_sql}#{costs_sql}#{settings_sql}#{buffers_sql}#{timing_sql}#{summary_sql})"
52
+ .strip.gsub(/\s+/, " ")
53
+ .gsub(/\(\s?\s?\s?,/, "(")
54
+ .gsub(/\s,\s/, " ")
55
+ .gsub(/\(\s?\)/, "")
56
+
57
+ sql = "EXPLAIN #{opts_sql} #{to_sql(arel, binds)}"
58
+ PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN #{opts_sql}".strip, binds))
8
59
  end
9
60
  end
10
61
  end
@@ -13,23 +64,42 @@ end
13
64
 
14
65
  module ActiveRecord
15
66
  class Relation
16
- def analyze
17
- exec_analyze(collecting_queries_for_explain { exec_queries })
67
+ def analyze(opts = {})
68
+ res = exec_analyze(collecting_queries_for_explain { exec_queries }, opts)
69
+ if [:json, :hash].include?(opts[:format])
70
+ start = res.index("[")
71
+ finish = res.rindex("]")
72
+ raw_json = res.slice(start, finish - start + 1)
73
+
74
+ if opts[:format] == :json
75
+ JSON.parse(raw_json).to_json
76
+ elsif opts[:format] == :hash
77
+ JSON.parse(raw_json)
78
+ end
79
+ else
80
+ res
81
+ end
18
82
  end
19
83
  end
20
84
  end
21
85
 
22
86
  module ActiveRecord
23
87
  module Explain
24
- def exec_analyze(queries) # :nodoc:
88
+ def exec_analyze(queries, opts = {}) # :nodoc:
25
89
  str = queries.map do |sql, binds|
26
- msg = "EXPLAIN ANALYZE for: #{sql}".dup
90
+ analyze_msg = if opts[:analyze] == false
91
+ ""
92
+ else
93
+ " ANALYZE"
94
+ end
95
+
96
+ msg = "EXPLAIN#{analyze_msg} for: #{sql}".dup
27
97
  unless binds.empty?
28
98
  msg << " "
29
99
  msg << binds.map { |attr| render_bind(attr) }.inspect
30
100
  end
31
101
  msg << "\n"
32
- msg << connection.analyze(sql, binds)
102
+ msg << connection.analyze(sql, binds, opts)
33
103
  end.join("\n")
34
104
 
35
105
  # Overriding inspect to be more human readable, especially in the console.
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordAnalyze
2
- VERSION = "0.2.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/query-plan.png ADDED
Binary file
data/spec/main_spec.rb ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require "migrations/create_users_migration.rb"
5
+
6
+ class User < ActiveRecord::Base; end
7
+
8
+ describe "ActiveRecord analyze" do
9
+ before(:all) do
10
+ ActiveRecord::Base.establish_connection(
11
+ ENV.fetch("DATABASE_URL")
12
+ )
13
+
14
+ @schema_migration = ActiveRecord::Base.connection.schema_migration
15
+ ActiveRecord::Migrator.new(:up, [CreateUsers.new], @schema_migration).migrate
16
+ end
17
+
18
+ describe "no opts" do
19
+ it "works" do
20
+ expect do
21
+ User.all.analyze
22
+ end.not_to raise_error
23
+ end
24
+ end
25
+
26
+ describe "format json" do
27
+ it "works" do
28
+ result = User.all.analyze(format: :json)
29
+ expect(JSON.parse(result)[0].keys.sort).to eq [
30
+ "Execution Time", "Plan", "Planning Time", "Triggers"
31
+ ]
32
+ end
33
+ end
34
+
35
+ describe "format hash" do
36
+ it "works" do
37
+ result = User.all.analyze(format: :hash)
38
+ expect(result[0].keys.sort).to eq [
39
+ "Execution Time", "Plan", "Planning Time", "Triggers"
40
+ ]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,10 @@
1
+ class CreateUsers < ActiveRecord::Migration::Current
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :name
5
+ t.string :email
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require "active_record/railtie"
5
+ require 'bundler/setup'
6
+ require_relative '../lib/activerecord-analyze'
7
+
8
+ ENV["DATABASE_URL"] ||= "postgresql://postgres:secret@localhost:5432/activerecord-analyze-test"
metadata CHANGED
@@ -1,29 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-analyze
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-11 00:00:00.000000000 Z
11
+ date: 2021-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 5.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 5.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
27
69
  description: ' Gem adds an "analyze" method to all Active Record query objects. Compatible
28
70
  with PostgreSQL database. '
29
71
  email:
@@ -32,20 +74,26 @@ executables: []
32
74
  extensions: []
33
75
  extra_rdoc_files: []
34
76
  files:
77
+ - ".circleci/config.yml"
35
78
  - ".gitignore"
36
79
  - Gemfile
37
80
  - LICENSE.txt
38
81
  - README.md
39
82
  - Rakefile
40
83
  - activerecord-analyze.gemspec
84
+ - docker-compose.yml.sample
41
85
  - lib/activerecord-analyze.rb
42
86
  - lib/activerecord-analyze/main.rb
43
87
  - lib/activerecord-analyze/version.rb
88
+ - query-plan.png
89
+ - spec/main_spec.rb
90
+ - spec/migrations/create_users_migration.rb
91
+ - spec/spec_helper.rb
44
92
  homepage: http://github.com/pawurb/activerecord-analyze
45
93
  licenses:
46
94
  - MIT
47
95
  metadata: {}
48
- post_install_message:
96
+ post_install_message:
49
97
  rdoc_options: []
50
98
  require_paths:
51
99
  - lib
@@ -60,9 +108,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
108
  - !ruby/object:Gem::Version
61
109
  version: '0'
62
110
  requirements: []
63
- rubyforge_project:
64
- rubygems_version: 2.6.14
65
- signing_key:
111
+ rubygems_version: 3.1.4
112
+ signing_key:
66
113
  specification_version: 4
67
114
  summary: Add EXPLAIN ANALYZE to Active Record query objects
68
- test_files: []
115
+ test_files:
116
+ - spec/main_spec.rb
117
+ - spec/migrations/create_users_migration.rb
118
+ - spec/spec_helper.rb