activerecord-analyze 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4a7ef7ec9a322a55d31e636725eaa77273059926ebb4a744697b23d612a59b4
4
- data.tar.gz: 93dbe428ea7bfda307d5362c3ee2623dc5d1aa3a93ada7fe5449affb03eb3c30
3
+ metadata.gz: 1f3e8c5511121ff7c9a7e2aab56d1638e1f42286477bfb97a72ae6d284f719b1
4
+ data.tar.gz: 0a6eba3afdf108a616ad837d9520148a2a387ab0ab3441afbbe2e9851e974e1e
5
5
  SHA512:
6
- metadata.gz: a100348c560f7c04733e2d0390a298fe5c8818d06a7c095e5fa4a15099af2bbd8b4263dc2c9ad68315f17905569eb4874d634ae48e8cc4732a5984b06272f4a6
7
- data.tar.gz: a786120ba3bb4bc3ffe4c8a4a36b7b4e972c9618da55b96e8e3b1494734ef76a5ce8424f8de736d5510026642f0f79b6ed91d8574bce0d5248b549b730b32b27
6
+ metadata.gz: 94223f7a2f15534a7b1d648cb5a019f4aeab0efef044a104ff70e4ee876cdc332de67dfdc86fb325aac5b847a515ae6b02846fed2a19c1b342df4e43b14c7e3d
7
+ data.tar.gz: 14aa9186a5e14d00dc6b6475d9d7052e00942d82bb5deafcb14f7c170a80282216c05597e102e9ce55c934807ceee145462d6f416ece2030ae21cfbbd495f3d8
data/README.md CHANGED
@@ -2,6 +2,8 @@
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
+ 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
+
5
7
  ## Installation
6
8
 
7
9
  In your Gemfile:
@@ -14,7 +16,7 @@ gem 'activerecord-analyze'
14
16
 
15
17
  ## Options
16
18
 
17
- The `analyze` method supports the following EXPLAIN query options (PG docs reference)[https://www.postgresql.org/docs/12/sql-explain.html]:
19
+ The `analyze` method supports the following EXPLAIN query options ([PostgreSQL docs reference](https://www.postgresql.org/docs/12/sql-explain.html)):
18
20
 
19
21
  ```
20
22
  VERBOSE [ boolean ]
@@ -26,7 +28,7 @@ SUMMARY [ boolean ]
26
28
  FORMAT { TEXT | XML | JSON | YAML }
27
29
  ```
28
30
 
29
- You can pass the options like that:
31
+ You can execute it like that:
30
32
 
31
33
  ```ruby
32
34
 
@@ -41,7 +43,7 @@ User.all.analyze(
41
43
  )
42
44
 
43
45
  # EXPLAIN (FORMAT JSON, ANALYZE, VERBOSE, COSTS, SETTINGS, BUFFERS, TIMING, SUMMARY)
44
- # SELECT "users".* FROM "users"
46
+ # SELECT "users".* FROM "users"
45
47
  # [
46
48
  # {
47
49
  # "Plan": {
@@ -58,6 +60,7 @@ User.all.analyze(
58
60
  # "Actual Total Time": 0.007,
59
61
  # "Actual Rows": 2,
60
62
  # "Actual Loops": 1,
63
+ # "Output": ["id", "team_id", "email"],
61
64
  # "Shared Hit Blocks": 1,
62
65
  # "Shared Read Blocks": 0,
63
66
  # "Shared Dirtied Blocks": 0,
@@ -89,6 +92,8 @@ User.all.analyze(
89
92
 
90
93
  ```
91
94
 
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
+
92
97
  Optionally you can disable running the `ANALYZE` query and only generate the plan:
93
98
 
94
99
  ```ruby
@@ -7,6 +7,8 @@ module ActiveRecord
7
7
  case fmt
8
8
  when :json
9
9
  "FORMAT JSON,"
10
+ when :hash
11
+ "FORMAT JSON,"
10
12
  when :yaml
11
13
  "FORMAT YAML,"
12
14
  when :text
@@ -63,7 +65,18 @@ end
63
65
  module ActiveRecord
64
66
  class Relation
65
67
  def analyze(opts = {})
66
- exec_analyze(collecting_queries_for_explain { exec_queries }, opts)
68
+ res = exec_analyze(collecting_queries_for_explain { exec_queries }, opts)
69
+ if [:json, :hash].include?(opts[:format])
70
+ raw_json = "[" + res[/\[(.*?)(\(\d row)/m, 1]
71
+
72
+ if opts[:format] == :json
73
+ JSON.parse(raw_json).to_json
74
+ elsif opts[:format] == :hash
75
+ JSON.parse(raw_json)
76
+ end
77
+ else
78
+ res
79
+ end
67
80
  end
68
81
  end
69
82
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordAnalyze
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
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.1
4
+ version: 0.5.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-01-12 00:00:00.000000000 Z
11
+ date: 2021-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails