activerecord-explain-analyze 0.1.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 +7 -0
- data/.circleci/config.yml +13 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/README.md +71 -0
- data/Rakefile +6 -0
- data/activerecord-explain-analyze.gemspec +34 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/activerecord-explain-analyze.rb +9 -0
- data/lib/activerecord-explain-analyze/postgresql_adapter.rb +33 -0
- data/lib/activerecord-explain-analyze/relation.rb +38 -0
- data/lib/activerecord-explain-analyze/version.rb +3 -0
- metadata +185 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '0780914e2c587bc84e3f3ba2d3129d35a1cbba01'
|
4
|
+
data.tar.gz: 68e16b34cab2d641a45316a78ae85fc0760b4aa2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ac0b7247cdd9d4e51aa14b912b41f9faf96155a7b2cce96fc81bce1a00008ca81e99d8bdbf0b1597983eca95082aa5db525775fae60ef89053b39c5c32d678c
|
7
|
+
data.tar.gz: b03ade0199465f8088fa833a2534f3d02bbf206b8ec768c6dcdbafd619cbbe18dfe42763d9313a47d3352efd63a39394ed8c75b5ec43b00333abfd5761fa4751
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
2
|
+
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
|
3
|
+
version: 2
|
4
|
+
jobs:
|
5
|
+
build:
|
6
|
+
docker:
|
7
|
+
- image: circleci/ruby:2.4.2
|
8
|
+
working_directory: ~/repo
|
9
|
+
steps:
|
10
|
+
- checkout
|
11
|
+
- run: bundle install --jobs=4 --retry=3 --path vendor/bundle
|
12
|
+
- run: bundle exec bundle-audit update && bundle exec bundle-audit check
|
13
|
+
- run: bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# activerecord-explain-analyze [](https://circleci.com/gh/6/activerecord-explain-analyze)
|
2
|
+
|
3
|
+
Extends ActiveRecord#explain with support for EXPLAIN ANALYZE and output formats of JSON, XML, and YAML.
|
4
|
+
|
5
|
+
It currently supports ActiveRecord 4 and 5, and PostgreSQL only.
|
6
|
+
|
7
|
+
### Examples:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
Wallet.where(base_currency: "USD").explain(analyze: true)
|
11
|
+
```
|
12
|
+
Results in:
|
13
|
+
|
14
|
+
```sql
|
15
|
+
EXPLAIN for: SELECT "wallets".* FROM "wallets" WHERE "wallets"."deleted_at" IS NULL AND "wallets"."base_currency" = $1
|
16
|
+
Bitmap Heap Scan on public.wallets (cost=4.16..9.50 rows=1 width=164) (actual time=0.008..0.012 rows=30 loops=1)
|
17
|
+
Output: id, canonical_id, client_id, wallet_type, base_currency, created_at, updated_at, deleted_at
|
18
|
+
Recheck Cond: (wallets.deleted_at IS NULL)
|
19
|
+
Filter: ((wallets.base_currency)::text = 'USD'::text)
|
20
|
+
Heap Blocks: exact=1
|
21
|
+
Buffers: shared hit=2
|
22
|
+
-> Bitmap Index Scan on index_wallets_on_deleted_at (cost=0.00..4.16 rows=2 width=0) (actual time=0.003..0.003 rows=32 loops=1)
|
23
|
+
Index Cond: (wallets.deleted_at IS NULL)
|
24
|
+
Buffers: shared hit=1
|
25
|
+
Planning time: 0.041 ms
|
26
|
+
Execution time: 0.026 ms
|
27
|
+
```
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Wallet.where(base_currency: "USD").explain(analyze: true, format: :json)
|
31
|
+
```
|
32
|
+
|
33
|
+
Results in:
|
34
|
+
|
35
|
+
```json
|
36
|
+
[
|
37
|
+
{
|
38
|
+
"Plan": {
|
39
|
+
"Node Type": "Bitmap Heap Scan",
|
40
|
+
"Parallel Aware": false,
|
41
|
+
"Relation Name": "wallets",
|
42
|
+
"Schema": "public",
|
43
|
+
"Alias": "wallets",
|
44
|
+
"Startup Cost": 4.16,
|
45
|
+
"Total Cost": 9.50,
|
46
|
+
"Plan Rows": 1,
|
47
|
+
"Plan Width": 164,
|
48
|
+
"Actual Startup Time": 0.008,
|
49
|
+
"Actual Total Time": 0.013,
|
50
|
+
"Actual Rows": 30,
|
51
|
+
...
|
52
|
+
```
|
53
|
+
|
54
|
+
You can then paste this JSON output into [PEV](http://tatiyants.com/pev/) or similar tools to get a visualization of the EXPLAIN query output:
|
55
|
+
|
56
|
+
<img width="673" alt="screen shot 2017-10-27 at 4 24 38 pm" src="https://user-images.githubusercontent.com/158675/32123765-6b4938ae-bb33-11e7-80b6-7d9ceac013e2.png">
|
57
|
+
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
|
61
|
+
Add this line to your application's Gemfile and run `bundle` to install:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
gem 'activerecord-explain-analyze'
|
65
|
+
```
|
66
|
+
|
67
|
+
## Development
|
68
|
+
|
69
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
70
|
+
|
71
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "activerecord-explain-analyze/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-explain-analyze"
|
8
|
+
spec.version = ActiveRecordExplainAnalyze::VERSION
|
9
|
+
spec.authors = ["Peter Graham"]
|
10
|
+
spec.email = ["peterghm@gmail.com"]
|
11
|
+
spec.licenses = ["MIT"]
|
12
|
+
|
13
|
+
spec.summary = %q{ActiveRecord#explain with support for EXPLAIN ANALYZE and a variety of output formats}
|
14
|
+
spec.description = %q{Extends ActiveRecord#explain with support for EXPLAIN ANALYZE and output formats of JSON, XML, and YAML.}
|
15
|
+
spec.homepage = "https://github.com/6/activerecord-explain-analyze"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "activerecord", ">= 4"
|
25
|
+
spec.add_dependency "pg"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
spec.add_development_dependency "pry"
|
31
|
+
spec.add_development_dependency "rspec-collection_matchers"
|
32
|
+
spec.add_development_dependency "rspec-its"
|
33
|
+
spec.add_development_dependency "bundler-audit"
|
34
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "activerecord/explain/analyze"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_record/connection_adapters/postgresql_adapter"
|
3
|
+
|
4
|
+
require "activerecord-explain-analyze/version"
|
5
|
+
require "activerecord-explain-analyze/relation"
|
6
|
+
require "activerecord-explain-analyze/postgresql_adapter"
|
7
|
+
|
8
|
+
ActiveRecord::Relation.send(:prepend, ActiveRecordExplainAnalyze::Relation)
|
9
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, ActiveRecordExplainAnalyze::PostgreSQLAdapter)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ActiveRecordExplainAnalyze
|
2
|
+
module PostgreSQLAdapter
|
3
|
+
def explain_with_options(arel, binds = [], analyze, format)
|
4
|
+
options = []
|
5
|
+
options.concat(["ANALYZE", "COSTS", "VERBOSE", "BUFFERS"]) if analyze
|
6
|
+
options << "FORMAT #{format}" unless format == "TEXT"
|
7
|
+
options_sql = options.size > 0 ? "(#{options.join(', ')})" : ""
|
8
|
+
|
9
|
+
sql = "EXPLAIN #{options_sql} #{to_sql(arel, binds)}"
|
10
|
+
result = exec_query(sql, "EXPLAIN", binds)
|
11
|
+
|
12
|
+
if format == "TEXT" && explain_pretty_printer
|
13
|
+
explain_pretty_printer.new.pp(result)
|
14
|
+
else
|
15
|
+
result.rows.map(&:first).join("\n")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def explain_pretty_printer
|
22
|
+
if defined?(ExplainPrettyPrinter)
|
23
|
+
# Rails 4:
|
24
|
+
ExplainPrettyPrinter
|
25
|
+
elsif defined?(PostgreSQL::ExplainPrettyPrinter)
|
26
|
+
# Rails 5:
|
27
|
+
PostgreSQL::ExplainPrettyPrinter
|
28
|
+
else
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module ActiveRecordExplainAnalyze
|
2
|
+
module Relation
|
3
|
+
EXPLAIN_FORMATS = [
|
4
|
+
"JSON",
|
5
|
+
"TEXT",
|
6
|
+
"XML",
|
7
|
+
"YAML",
|
8
|
+
].freeze
|
9
|
+
|
10
|
+
def explain(analyze: false, format: :text)
|
11
|
+
format = format.to_s.upcase
|
12
|
+
unless EXPLAIN_FORMATS.include?(format)
|
13
|
+
raise ArgumentError, "format must be one of: #{EXPLAIN_FORMATS.join(', ')}"
|
14
|
+
end
|
15
|
+
|
16
|
+
queries = collecting_queries_for_explain { exec_queries }
|
17
|
+
if analyze || format != "TEXT"
|
18
|
+
exec_explain_with_options(queries, analyze: analyze, format: format)
|
19
|
+
else
|
20
|
+
exec_explain(queries)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def exec_explain_with_options(queries, analyze:, format:)
|
25
|
+
str = queries.map do |sql, binds|
|
26
|
+
msg = "EXPLAIN for: #{sql}\n".dup
|
27
|
+
msg << connection.explain_with_options(sql, binds, analyze, format)
|
28
|
+
end.join("\n")
|
29
|
+
|
30
|
+
# Overriding inspect to be more human readable, especially in the console.
|
31
|
+
def str.inspect
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
str
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-explain-analyze
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Graham
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.15'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.15'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-collection_matchers
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec-its
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: bundler-audit
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: Extends ActiveRecord#explain with support for EXPLAIN ANALYZE and output
|
140
|
+
formats of JSON, XML, and YAML.
|
141
|
+
email:
|
142
|
+
- peterghm@gmail.com
|
143
|
+
executables: []
|
144
|
+
extensions: []
|
145
|
+
extra_rdoc_files: []
|
146
|
+
files:
|
147
|
+
- ".circleci/config.yml"
|
148
|
+
- ".gitignore"
|
149
|
+
- ".rspec"
|
150
|
+
- Gemfile
|
151
|
+
- README.md
|
152
|
+
- Rakefile
|
153
|
+
- activerecord-explain-analyze.gemspec
|
154
|
+
- bin/console
|
155
|
+
- bin/setup
|
156
|
+
- lib/activerecord-explain-analyze.rb
|
157
|
+
- lib/activerecord-explain-analyze/postgresql_adapter.rb
|
158
|
+
- lib/activerecord-explain-analyze/relation.rb
|
159
|
+
- lib/activerecord-explain-analyze/version.rb
|
160
|
+
homepage: https://github.com/6/activerecord-explain-analyze
|
161
|
+
licenses:
|
162
|
+
- MIT
|
163
|
+
metadata: {}
|
164
|
+
post_install_message:
|
165
|
+
rdoc_options: []
|
166
|
+
require_paths:
|
167
|
+
- lib
|
168
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
requirements: []
|
179
|
+
rubyforge_project:
|
180
|
+
rubygems_version: 2.6.13
|
181
|
+
signing_key:
|
182
|
+
specification_version: 4
|
183
|
+
summary: ActiveRecord#explain with support for EXPLAIN ANALYZE and a variety of output
|
184
|
+
formats
|
185
|
+
test_files: []
|