quick_count 0.1.1 → 0.1.2.pre.1

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: b96ac3ad5c0ecf19aaa83bdda2805c30221b26ae36d19a614aaa637cf2a52dbc
4
- data.tar.gz: f42909f6a0a932e98329d4e4676cdc085db7aceac847fcd035746cbfd652439b
3
+ metadata.gz: 065e672ffe033dc29178202180bd92aa49392f2f4e7adac035df5fa3610f9f11
4
+ data.tar.gz: e6899dfe52b32fefc5ca800491ddc367736662da824e57c8c4dd7d2eb9282e64
5
5
  SHA512:
6
- metadata.gz: 30d43a8f2dca676c28c68b8f94fb699f09c9af067f27f8cfe7a9332c6ed322e10ac745130e9e0f21b74157fb9dcdee4b696115b1483f1054eb4e60d992bcd42d
7
- data.tar.gz: 0aae2ca316cb80fe2bab834bc2ed95769897db442ab186931d17fa1de2a01d923834eca1beb37518d913409a679177adf949f6a95f2b6cae4d3465046f981cc9
6
+ metadata.gz: 68e5ba2385e63a7871cbbf1fb5b00106ec8e7b39dd4d3523504caeb06b1183d81d86e1e4cae4a2ce923bc680a3e0bc0759d0d602ff36c32de96f6d4773c85f46
7
+ data.tar.gz: f3027961d2bbcfc2ab37ebbe5be729603dbd585f7511fb741f26f32213c27cfa7cdc64d8816469fde2f69686ae6a4ce94a7513aa0d57055ed4e60d14a51d2f94
@@ -1,11 +1,12 @@
1
1
  require 'active_support/concern'
2
+ require 'pry'
2
3
 
3
4
  module CountEstimate
4
5
  module ActiveRecord
5
6
 
6
7
  def count_estimate
7
- my_statement = ::ActiveRecord::Base.connection.quote(to_sql)
8
- result = ::ActiveRecord::Base.connection.execute("SELECT count_estimate(#{my_statement})")
8
+ my_statement = connection.quote(to_sql)
9
+ result = connection.execute("SELECT count_estimate(#{my_statement})")
9
10
  result[0]["count_estimate"].to_i
10
11
  end
11
12
 
data/lib/quick_count.rb CHANGED
@@ -27,49 +27,48 @@ module QuickCount
27
27
  private
28
28
 
29
29
  def self.quick_count_sql(threshold: 500000, schema: 'public')
30
- <<-SQL
30
+ <<~SQL
31
31
  CREATE OR REPLACE FUNCTION #{schema}.quick_count(table_name text, threshold bigint default #{threshold}) RETURNS bigint AS
32
32
  $func$
33
33
  DECLARE count bigint;
34
34
  BEGIN
35
- EXECUTE 'SELECT
36
- CASE
37
- WHEN SUM(estimate)::integer < '|| threshold ||' THEN
38
- (SELECT COUNT(*) FROM "'|| table_name ||'")
39
- ELSE
40
- SUM(estimate)::integer
41
- END AS count
42
- FROM (
43
- SELECT
44
- ((SUM(child.reltuples::float)/greatest(SUM(child.relpages::float),1))) * (SUM(pg_relation_size(child.oid))::float / (current_setting(''block_size'')::float)) AS estimate
45
- FROM pg_inherits
46
- JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
47
- JOIN pg_class child ON pg_inherits.inhrelid = child.oid
48
- WHERE parent.relname = '''|| table_name ||'''
49
- UNION SELECT (reltuples::float/greatest(relpages::float, 1)) * (pg_relation_size(pg_class.oid)::float / (current_setting(''block_size'')::float)) AS estimate FROM pg_class where relname='''|| table_name ||'''
50
- ) AS tables' INTO count;
51
- RETURN count;
35
+ EXECUTE 'SELECT
36
+ CASE
37
+ WHEN SUM(estimate)::integer < '|| threshold ||' THEN
38
+ (SELECT COUNT(*) FROM "'|| table_name ||'")
39
+ ELSE
40
+ SUM(estimate)::integer
41
+ END AS count
42
+ FROM (
43
+ SELECT
44
+ ((SUM(child.reltuples::float)/greatest(SUM(child.relpages::float),1))) * (SUM(pg_relation_size(child.oid))::float / (current_setting(''block_size'')::float)) AS estimate
45
+ FROM pg_inherits
46
+ JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
47
+ JOIN pg_class child ON pg_inherits.inhrelid = child.oid
48
+ WHERE parent.relname = '''|| table_name ||'''
49
+ UNION SELECT (reltuples::float/greatest(relpages::float, 1)) * (pg_relation_size(pg_class.oid)::float / (current_setting(''block_size'')::float)) AS estimate FROM pg_class where relname='''|| table_name ||'''
50
+ ) AS tables' INTO count;
51
+ RETURN count;
52
52
  END
53
53
  $func$ LANGUAGE plpgsql;
54
54
  SQL
55
55
  end
56
56
 
57
57
  def self.count_estimate_sql(schema: 'public')
58
- <<-SQL
59
- CREATE OR REPLACE FUNCTION #{schema}.count_estimate(query text) RETURNS integer AS
60
- $func$
61
- DECLARE
62
- rec record;
63
- rows integer;
64
- BEGIN
65
- FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP
66
- rows := substring(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)');
67
- EXIT WHEN rows IS NOT NULL;
68
- END LOOP;
69
-
70
- RETURN rows;
71
- END
72
- $func$ LANGUAGE plpgsql;
58
+ <<~SQL
59
+ CREATE OR REPLACE FUNCTION #{schema}.count_estimate(query text) RETURNS integer AS
60
+ $func$
61
+ DECLARE
62
+ rec record;
63
+ rows integer;
64
+ BEGIN
65
+ FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP
66
+ rows := substring(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)');
67
+ EXIT WHEN rows IS NOT NULL;
68
+ END LOOP;
69
+ RETURN rows;
70
+ END
71
+ $func$ LANGUAGE plpgsql;
73
72
  SQL
74
73
  end
75
74
 
@@ -8,7 +8,7 @@ module QuickCount
8
8
 
9
9
  def quick_count(threshold: nil)
10
10
  threshold = threshold ? ", #{threshold}" : nil
11
- result = ::ActiveRecord::Base.connection.execute("SELECT quick_count('#{table_name}'#{threshold})")
11
+ result = connection.execute("SELECT quick_count('#{table_name}'#{threshold})")
12
12
  result[0]["quick_count"].to_i
13
13
  end
14
14
 
@@ -1,3 +1,3 @@
1
1
  module QuickCount
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2-1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quick_count
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Stevens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-27 00:00:00.000000000 Z
11
+ date: 2020-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0.20'
19
+ version: '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
- version: '0.20'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -68,58 +68,58 @@ dependencies:
68
68
  name: pry-byebug
69
69
  requirement: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - "~>"
71
+ - - ">="
72
72
  - !ruby/object:Gem::Version
73
- version: '3'
73
+ version: '0'
74
74
  type: :development
75
75
  prerelease: false
76
76
  version_requirements: !ruby/object:Gem::Requirement
77
77
  requirements:
78
- - - "~>"
78
+ - - ">="
79
79
  - !ruby/object:Gem::Version
80
- version: '3'
80
+ version: '0'
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: bundler
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - "~>"
85
+ - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: '1.3'
87
+ version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - "~>"
92
+ - - ">="
93
93
  - !ruby/object:Gem::Version
94
- version: '1.3'
94
+ version: '0'
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: rake
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  requirements:
99
- - - "~>"
99
+ - - ">="
100
100
  - !ruby/object:Gem::Version
101
- version: '12.0'
101
+ version: '0'
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  requirements:
106
- - - "~>"
106
+ - - ">="
107
107
  - !ruby/object:Gem::Version
108
- version: '12.0'
108
+ version: '0'
109
109
  - !ruby/object:Gem::Dependency
110
110
  name: combustion
111
111
  requirement: !ruby/object:Gem::Requirement
112
112
  requirements:
113
- - - "~>"
113
+ - - ">="
114
114
  - !ruby/object:Gem::Version
115
- version: '0.7'
115
+ version: '0'
116
116
  type: :development
117
117
  prerelease: false
118
118
  version_requirements: !ruby/object:Gem::Requirement
119
119
  requirements:
120
- - - "~>"
120
+ - - ">="
121
121
  - !ruby/object:Gem::Version
122
- version: '0.7'
122
+ version: '0'
123
123
  description: Installs two database functions, `quick_count` and `count_estimate` for
124
124
  getting count estimations on large tables
125
125
  email:
@@ -132,10 +132,8 @@ files:
132
132
  - LICENSE
133
133
  - README.md
134
134
  - lib/count_estimate/active_record.rb
135
- - lib/count_estimate/active_record/relation.rb
136
135
  - lib/quick_count.rb
137
136
  - lib/quick_count/active_record.rb
138
- - lib/quick_count/active_record/base.rb
139
137
  - lib/quick_count/railtie.rb
140
138
  - lib/quick_count/version.rb
141
139
  homepage: https://github.com/TwilightCoders/quick_count
@@ -155,12 +153,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
153
  version: '2.0'
156
154
  required_rubygems_version: !ruby/object:Gem::Requirement
157
155
  requirements:
158
- - - ">="
156
+ - - ">"
159
157
  - !ruby/object:Gem::Version
160
- version: '0'
158
+ version: 1.3.1
161
159
  requirements: []
162
160
  rubyforge_project:
163
- rubygems_version: 2.7.7
161
+ rubygems_version: 2.7.6.2
164
162
  signing_key:
165
163
  specification_version: 4
166
164
  summary: Quickly get an accurate count estimation for large tables.
@@ -1,15 +0,0 @@
1
- require 'active_support/concern'
2
-
3
- module CountEstimate
4
- module ActiveRecord
5
- module Relation
6
-
7
- def count_estimate
8
- my_statement = ::ActiveRecord::Base.connection.quote(to_sql)
9
- result = ::ActiveRecord::Base.connection.execute("SELECT count_estimate(#{my_statement})")
10
- result[0]["count_estimate"].to_i
11
- end
12
-
13
- end
14
- end
15
- end
@@ -1,20 +0,0 @@
1
- require 'active_support/concern'
2
-
3
- module QuickCount
4
- module ActiveRecord
5
- module Base
6
- extend ActiveSupport::Concern
7
-
8
- module ClassMethods
9
-
10
- def quick_count(threshold: nil)
11
- threshold = threshold ? ", #{threshold}" : nil
12
- result = ::ActiveRecord::Base.connection.execute("SELECT quick_count('#{table_name}'#{threshold})")
13
- result[0]["quick_count"].to_i
14
- end
15
-
16
- end
17
-
18
- end
19
- end
20
- end