activerecord-analyze 0.5.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +30 -0
- data/README.md +14 -12
- data/activerecord-analyze.gemspec +3 -0
- data/docker-compose.yml.sample +11 -0
- data/lib/activerecord-analyze/main.rb +8 -2
- data/lib/activerecord-analyze/version.rb +1 -1
- data/query-plan.png +0 -0
- data/spec/main_spec.rb +66 -0
- data/spec/migrations/create_users_migration.rb +10 -0
- data/spec/spec_helper.rb +8 -0
- metadata +54 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 054f9f2dad0f0e6a87b82d1722a95889688d3a3b712a4077c0d5892d693ef52f
|
4
|
+
data.tar.gz: c08fe69a2a34e6f9b8b51ab40afa24810262e681a9f01109786c8fdcd5bf0285
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6a5ad936771e8a0d6d1fa73c0ac68b2146b7f4d7e6e3c9667def0f7c5450b7ab7109885db4f6590217227e84d821f4d3f976c235dd1f02a6d52d5335db45b04
|
7
|
+
data.tar.gz: c2e987200963f0acf13eb734a6ae1bdd0212bcc68bae3aad1274fe8c0cd27f0cdd28d714ade1d1f15e76dc6b7d7d31fc83340ad80c541874e445ac4807776934
|
@@ -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,9 +1,13 @@
|
|
1
|
-
# ActiveRecord Analyze
|
1
|
+
# ActiveRecord Analyze [![Gem Version](https://badge.fury.io/rb/activerecord-analyze.svg)](https://badge.fury.io/rb/activerecord-analyze) [![CircleCI](https://circleci.com/gh/pawurb/activerecord-analyze.svg?style=svg)](https://circleci.com/gh/pawurb/activerecord-analyze)
|
2
2
|
|
3
3
|
This gem adds an `analyze` method to Active Record query objects. It executes `EXPLAIN ANALYZE` on a query SQL.
|
4
4
|
|
5
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
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
|
+
|
7
11
|
## Installation
|
8
12
|
|
9
13
|
In your Gemfile:
|
@@ -19,21 +23,21 @@ gem 'activerecord-analyze'
|
|
19
23
|
The `analyze` method supports the following EXPLAIN query options ([PostgreSQL docs reference](https://www.postgresql.org/docs/12/sql-explain.html)):
|
20
24
|
|
21
25
|
```
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
buffers: [ boolean ]
|
27
|
+
verbose: [ boolean ]
|
28
|
+
costs: [ boolean ]
|
29
|
+
settings: [ boolean ]
|
30
|
+
timing: [ boolean ]
|
31
|
+
summary: [ boolean ]
|
32
|
+
format: { :text | :json | :xml | :yaml | :pretty_json }
|
29
33
|
```
|
30
34
|
|
31
35
|
You can execute it like that:
|
32
36
|
|
33
37
|
```ruby
|
34
38
|
|
35
|
-
User.all.analyze(
|
36
|
-
format: :
|
39
|
+
puts User.all.analyze(
|
40
|
+
format: :pretty_json, # :pretty_json format option generates a formatted JSON output
|
37
41
|
verbose: true,
|
38
42
|
costs: true,
|
39
43
|
settings: true,
|
@@ -92,8 +96,6 @@ User.all.analyze(
|
|
92
96
|
|
93
97
|
```
|
94
98
|
|
95
|
-
The following `format` options are supported `:json, :hash, :yaml, :text, :xml`. Especially the `:json` format is useful because it allows you to visualize a query plan using [a visualizer tool](https://tatiyants.com/pev/#/plans/new).
|
96
|
-
|
97
99
|
Optionally you can disable running the `ANALYZE` query and only generate the plan:
|
98
100
|
|
99
101
|
```ruby
|
@@ -9,6 +9,8 @@ module ActiveRecord
|
|
9
9
|
"FORMAT JSON,"
|
10
10
|
when :hash
|
11
11
|
"FORMAT JSON,"
|
12
|
+
when :pretty_json
|
13
|
+
"FORMAT JSON,"
|
12
14
|
when :yaml
|
13
15
|
"FORMAT YAML,"
|
14
16
|
when :text
|
@@ -66,13 +68,17 @@ module ActiveRecord
|
|
66
68
|
class Relation
|
67
69
|
def analyze(opts = {})
|
68
70
|
res = exec_analyze(collecting_queries_for_explain { exec_queries }, opts)
|
69
|
-
if [:json, :hash].include?(opts[:format])
|
70
|
-
|
71
|
+
if [:json, :hash, :pretty_json].include?(opts[:format])
|
72
|
+
start = res.index("[\n")
|
73
|
+
finish = res.rindex("]")
|
74
|
+
raw_json = res.slice(start, finish - start + 1)
|
71
75
|
|
72
76
|
if opts[:format] == :json
|
73
77
|
JSON.parse(raw_json).to_json
|
74
78
|
elsif opts[:format] == :hash
|
75
79
|
JSON.parse(raw_json)
|
80
|
+
elsif opts[:format] == :pretty_json
|
81
|
+
JSON.pretty_generate(JSON.parse(raw_json))
|
76
82
|
end
|
77
83
|
else
|
78
84
|
res
|
data/query-plan.png
ADDED
Binary file
|
data/spec/main_spec.rb
ADDED
@@ -0,0 +1,66 @@
|
|
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
|
+
|
44
|
+
describe "format pretty" do
|
45
|
+
it "works" do
|
46
|
+
result = User.all.analyze(format: :pretty_json)
|
47
|
+
expect(JSON.parse(result)[0].keys.sort).to eq [
|
48
|
+
"Execution Time", "Plan", "Planning Time", "Triggers"
|
49
|
+
]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "supports options" do
|
54
|
+
it "works" do
|
55
|
+
result = User.all.limit(10).where.not(email: nil).analyze(
|
56
|
+
format: :hash,
|
57
|
+
costs: true,
|
58
|
+
timing: true,
|
59
|
+
summary: true
|
60
|
+
)
|
61
|
+
expect(result[0].keys.sort).to eq [
|
62
|
+
"Execution Time", "Plan", "Planning Time", "Triggers"
|
63
|
+
]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-analyze
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,6 +24,48 @@ dependencies:
|
|
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,15 +74,21 @@ 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
|
@@ -64,4 +112,7 @@ rubygems_version: 3.1.4
|
|
64
112
|
signing_key:
|
65
113
|
specification_version: 4
|
66
114
|
summary: Add EXPLAIN ANALYZE to Active Record query objects
|
67
|
-
test_files:
|
115
|
+
test_files:
|
116
|
+
- spec/main_spec.rb
|
117
|
+
- spec/migrations/create_users_migration.rb
|
118
|
+
- spec/spec_helper.rb
|