quick_count 0.0.1 → 0.0.2
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 +4 -4
- data/.gitignore +9 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +103 -0
- data/MIT-LICENSE +20 -0
- data/README.md +73 -0
- data/Rakefile +6 -0
- data/lib/count_estimate/active_record/relation.rb +15 -0
- data/lib/quick_count/active_record/base.rb +25 -0
- data/lib/quick_count/railtie.rb +20 -0
- data/lib/quick_count/version.rb +3 -0
- data/lib/quick_count.rb +64 -0
- data/quick_count.gemspec +43 -0
- metadata +14 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2c460d264424aeda14e8f593465e17425dca7fac
|
|
4
|
+
data.tar.gz: b2e0b54686180053afc2726ab7186b690735f80c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 82e0482436336ebcb1810d73e051435a7c215e1e0a73917b9d11464ecf15d8300f79634b1e15f9ae0223998798f0841629f5da2b7b55725c4d727f6b3a32d205
|
|
7
|
+
data.tar.gz: 855be602cff1d114638664896baaef61f1496085327851d66f0c1d7cc5b119cba150229a80a740989c768e98008d5c52b950647fa520ade73952032d80a5f844
|
data/.gitignore
ADDED
data/Gemfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
# Declare your gem's dependencies in quick_count.gemspec.
|
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
|
5
|
+
# development dependencies will be added by default to the :development group.
|
|
6
|
+
gemspec
|
|
7
|
+
|
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
|
11
|
+
# your gem to rubygems.org.
|
|
12
|
+
group :development, :test do
|
|
13
|
+
gem 'pg', platform: :mri
|
|
14
|
+
gem 'activerecord-jdbcpostgresql-adapter', platform: :jruby
|
|
15
|
+
end
|
|
16
|
+
|
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
quick_count (0.0.1)
|
|
5
|
+
activemodel (>= 4.1, < 6)
|
|
6
|
+
activerecord (>= 4.1, < 6)
|
|
7
|
+
activesupport (>= 4.1, < 6)
|
|
8
|
+
pg (>= 0.12.0, < 0.30.0)
|
|
9
|
+
railties (>= 4.1, < 6)
|
|
10
|
+
|
|
11
|
+
GEM
|
|
12
|
+
remote: https://rubygems.org/
|
|
13
|
+
specs:
|
|
14
|
+
actionpack (5.0.2)
|
|
15
|
+
actionview (= 5.0.2)
|
|
16
|
+
activesupport (= 5.0.2)
|
|
17
|
+
rack (~> 2.0)
|
|
18
|
+
rack-test (~> 0.6.3)
|
|
19
|
+
rails-dom-testing (~> 2.0)
|
|
20
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
|
21
|
+
actionview (5.0.2)
|
|
22
|
+
activesupport (= 5.0.2)
|
|
23
|
+
builder (~> 3.1)
|
|
24
|
+
erubis (~> 2.7.0)
|
|
25
|
+
rails-dom-testing (~> 2.0)
|
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
|
27
|
+
activemodel (5.0.2)
|
|
28
|
+
activesupport (= 5.0.2)
|
|
29
|
+
activerecord (5.0.2)
|
|
30
|
+
activemodel (= 5.0.2)
|
|
31
|
+
activesupport (= 5.0.2)
|
|
32
|
+
arel (~> 7.0)
|
|
33
|
+
activesupport (5.0.2)
|
|
34
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
35
|
+
i18n (~> 0.7)
|
|
36
|
+
minitest (~> 5.1)
|
|
37
|
+
tzinfo (~> 1.1)
|
|
38
|
+
arel (7.1.4)
|
|
39
|
+
builder (3.2.3)
|
|
40
|
+
coderay (1.1.1)
|
|
41
|
+
concurrent-ruby (1.0.5)
|
|
42
|
+
diff-lcs (1.3)
|
|
43
|
+
erubis (2.7.0)
|
|
44
|
+
i18n (0.8.1)
|
|
45
|
+
loofah (2.0.3)
|
|
46
|
+
nokogiri (>= 1.5.9)
|
|
47
|
+
method_source (0.8.2)
|
|
48
|
+
mini_portile2 (2.1.0)
|
|
49
|
+
minitest (5.10.1)
|
|
50
|
+
nokogiri (1.7.1)
|
|
51
|
+
mini_portile2 (~> 2.1.0)
|
|
52
|
+
pg (0.20.0)
|
|
53
|
+
pry (0.10.4)
|
|
54
|
+
coderay (~> 1.1.0)
|
|
55
|
+
method_source (~> 0.8.1)
|
|
56
|
+
slop (~> 3.4)
|
|
57
|
+
rack (2.0.1)
|
|
58
|
+
rack-test (0.6.3)
|
|
59
|
+
rack (>= 1.0)
|
|
60
|
+
rails-dom-testing (2.0.2)
|
|
61
|
+
activesupport (>= 4.2.0, < 6.0)
|
|
62
|
+
nokogiri (~> 1.6)
|
|
63
|
+
rails-html-sanitizer (1.0.3)
|
|
64
|
+
loofah (~> 2.0)
|
|
65
|
+
railties (5.0.2)
|
|
66
|
+
actionpack (= 5.0.2)
|
|
67
|
+
activesupport (= 5.0.2)
|
|
68
|
+
method_source
|
|
69
|
+
rake (>= 0.8.7)
|
|
70
|
+
thor (>= 0.18.1, < 2.0)
|
|
71
|
+
rake (12.0.0)
|
|
72
|
+
rspec (3.5.0)
|
|
73
|
+
rspec-core (~> 3.5.0)
|
|
74
|
+
rspec-expectations (~> 3.5.0)
|
|
75
|
+
rspec-mocks (~> 3.5.0)
|
|
76
|
+
rspec-core (3.5.4)
|
|
77
|
+
rspec-support (~> 3.5.0)
|
|
78
|
+
rspec-expectations (3.5.0)
|
|
79
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
80
|
+
rspec-support (~> 3.5.0)
|
|
81
|
+
rspec-mocks (3.5.0)
|
|
82
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
83
|
+
rspec-support (~> 3.5.0)
|
|
84
|
+
rspec-support (3.5.0)
|
|
85
|
+
slop (3.6.0)
|
|
86
|
+
thor (0.19.4)
|
|
87
|
+
thread_safe (0.3.6)
|
|
88
|
+
tzinfo (1.2.3)
|
|
89
|
+
thread_safe (~> 0.1)
|
|
90
|
+
|
|
91
|
+
PLATFORMS
|
|
92
|
+
ruby
|
|
93
|
+
|
|
94
|
+
DEPENDENCIES
|
|
95
|
+
activerecord-jdbcpostgresql-adapter
|
|
96
|
+
bundler (~> 1.5)
|
|
97
|
+
pg
|
|
98
|
+
pry (~> 0)
|
|
99
|
+
quick_count!
|
|
100
|
+
rspec (~> 3.3)
|
|
101
|
+
|
|
102
|
+
BUNDLED WITH
|
|
103
|
+
1.13.6
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright 2017 Dale Stevens
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# QuickCount
|
|
2
|
+
|
|
3
|
+
Based on the answer http://stackoverflow.com/a/7945274/1454158.
|
|
4
|
+
|
|
5
|
+
It currently supports only Postgres.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add this line to your application's Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'quick_count'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
And then execute:
|
|
17
|
+
|
|
18
|
+
$ bundle
|
|
19
|
+
|
|
20
|
+
And in a rails console:
|
|
21
|
+
|
|
22
|
+
$ QuickCount.install
|
|
23
|
+
|
|
24
|
+
Or install it yourself as:
|
|
25
|
+
|
|
26
|
+
$ gem install quick_count
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
# user.rb
|
|
32
|
+
|
|
33
|
+
class User < ActiveRecord::Base
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
User.quick_count
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
## Uninstallation
|
|
42
|
+
|
|
43
|
+
Remove this line to your application's Gemfile:
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
gem 'quick_count'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
And then execute:
|
|
50
|
+
|
|
51
|
+
$ bundle
|
|
52
|
+
|
|
53
|
+
And in a rails console:
|
|
54
|
+
|
|
55
|
+
$ QuickCount.uninstall
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
Released under the MIT license - http://opensource.org/licenses/MIT
|
|
59
|
+
|
|
60
|
+
## Development
|
|
61
|
+
|
|
62
|
+
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.
|
|
63
|
+
|
|
64
|
+
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).
|
|
65
|
+
|
|
66
|
+
## Contributing
|
|
67
|
+
|
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/quick_count.
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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"]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
|
11
|
+
result = ::ActiveRecord::Base.connection.execute("SELECT quick_count('#{table_name}')")
|
|
12
|
+
result[0]["quick_count"]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def count_estimate
|
|
16
|
+
my_statement = ::ActiveRecord::Base.connection.quote(to_sql)
|
|
17
|
+
result = ::ActiveRecord::Base.connection.execute("SELECT count_estimate(#{my_statement})")
|
|
18
|
+
result[0]["count_estimate"]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'rails/railtie'
|
|
2
|
+
require 'quick_count/active_record/base'
|
|
3
|
+
require 'count_estimate/active_record/relation'
|
|
4
|
+
require 'pry'
|
|
5
|
+
|
|
6
|
+
module QuickCount
|
|
7
|
+
class Railtie < Rails::Railtie
|
|
8
|
+
|
|
9
|
+
# rake_tasks do
|
|
10
|
+
# load "../tasks/quick_count_tasks.rake"
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
initializer 'quick_count.load' do |app|
|
|
14
|
+
ActiveSupport.on_load(:active_record) do
|
|
15
|
+
QuickCount.load
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
end
|
data/lib/quick_count.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'quick_count/railtie'
|
|
2
|
+
require 'active_record'
|
|
3
|
+
require 'rake'
|
|
4
|
+
|
|
5
|
+
module QuickCount
|
|
6
|
+
|
|
7
|
+
def self.root
|
|
8
|
+
@root ||= Pathname.new(File.dirname(File.expand_path(File.dirname(__FILE__), '/../')))
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.load
|
|
12
|
+
::ActiveRecord::Base.send :include, QuickCount::ActiveRecord::Base
|
|
13
|
+
::ActiveRecord::Relation.send :include, CountEstimate::ActiveRecord::Relation
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.install
|
|
17
|
+
::ActiveRecord::Base.connection.execute(<<-eos
|
|
18
|
+
CREATE OR REPLACE FUNCTION quick_count(table_name text) RETURNS bigint AS
|
|
19
|
+
$func$
|
|
20
|
+
DECLARE
|
|
21
|
+
rec record;
|
|
22
|
+
rows integer;
|
|
23
|
+
BEGIN
|
|
24
|
+
RETURN (SELECT SUM(estimate) AS estimate FROM (
|
|
25
|
+
SELECT
|
|
26
|
+
SUM(child.reltuples::bigint) AS estimate
|
|
27
|
+
FROM pg_inherits
|
|
28
|
+
JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
|
|
29
|
+
JOIN pg_class child ON pg_inherits.inhrelid = child.oid
|
|
30
|
+
JOIN pg_namespace nmsp_parent ON nmsp_parent.oid = parent.relnamespace
|
|
31
|
+
JOIN pg_namespace nmsp_child ON nmsp_child.oid = child.relnamespace
|
|
32
|
+
WHERE parent.relname = table_name
|
|
33
|
+
GROUP BY parent.reltuples
|
|
34
|
+
UNION SELECT reltuples::bigint AS estimate FROM pg_class where relname=table_name) as tables);
|
|
35
|
+
END
|
|
36
|
+
$func$ LANGUAGE plpgsql;
|
|
37
|
+
eos
|
|
38
|
+
)
|
|
39
|
+
::ActiveRecord::Base.connection.execute(<<-eos
|
|
40
|
+
CREATE OR REPLACE FUNCTION count_estimate(query text) RETURNS integer AS
|
|
41
|
+
$func$
|
|
42
|
+
DECLARE
|
|
43
|
+
rec record;
|
|
44
|
+
rows integer;
|
|
45
|
+
BEGIN
|
|
46
|
+
FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP
|
|
47
|
+
rows := substring(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)');
|
|
48
|
+
EXIT WHEN rows IS NOT NULL;
|
|
49
|
+
END LOOP;
|
|
50
|
+
|
|
51
|
+
RETURN rows;
|
|
52
|
+
END
|
|
53
|
+
$func$ LANGUAGE plpgsql;
|
|
54
|
+
eos
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.uninstall
|
|
59
|
+
::ActiveRecord::Base.connection.execute("DROP FUNCTION quick_count(text);")
|
|
60
|
+
::ActiveRecord::Base.connection.execute("DROP FUNCTION count_estimate(text);")
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
end
|
|
64
|
+
|
data/quick_count.gemspec
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'quick_count/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'quick_count'
|
|
8
|
+
spec.version = QuickCount::VERSION
|
|
9
|
+
spec.authors = ['Dale Stevens']
|
|
10
|
+
spec.email = ['dale@twilightcoders.net']
|
|
11
|
+
|
|
12
|
+
spec.summary = 'Quickly get an accurate count estimation for large tables.'
|
|
13
|
+
spec.homepage = "https://github.com/TwilightCoders/quick_count"
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
|
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
18
|
+
if spec.respond_to?(:metadata)
|
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
|
20
|
+
else
|
|
21
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
25
|
+
spec.bindir = 'exe'
|
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
27
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
|
28
|
+
spec.require_paths = ['lib']
|
|
29
|
+
|
|
30
|
+
spec.required_ruby_version = '~> 2.1'
|
|
31
|
+
|
|
32
|
+
rails_versions = ['>= 4.1', '< 6']
|
|
33
|
+
|
|
34
|
+
spec.add_dependency 'pg', ['>= 0.12.0', '< 0.30.0']
|
|
35
|
+
spec.add_dependency 'activerecord', rails_versions
|
|
36
|
+
spec.add_dependency 'activesupport', rails_versions
|
|
37
|
+
spec.add_dependency 'activemodel', rails_versions
|
|
38
|
+
spec.add_dependency 'railties', rails_versions
|
|
39
|
+
|
|
40
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
|
41
|
+
spec.add_development_dependency 'rspec', '~> 3.3'
|
|
42
|
+
spec.add_development_dependency 'pry', '~> 0'
|
|
43
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: quick_count
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dale Stevens
|
|
@@ -158,7 +158,19 @@ email:
|
|
|
158
158
|
executables: []
|
|
159
159
|
extensions: []
|
|
160
160
|
extra_rdoc_files: []
|
|
161
|
-
files:
|
|
161
|
+
files:
|
|
162
|
+
- ".gitignore"
|
|
163
|
+
- Gemfile
|
|
164
|
+
- Gemfile.lock
|
|
165
|
+
- MIT-LICENSE
|
|
166
|
+
- README.md
|
|
167
|
+
- Rakefile
|
|
168
|
+
- lib/count_estimate/active_record/relation.rb
|
|
169
|
+
- lib/quick_count.rb
|
|
170
|
+
- lib/quick_count/active_record/base.rb
|
|
171
|
+
- lib/quick_count/railtie.rb
|
|
172
|
+
- lib/quick_count/version.rb
|
|
173
|
+
- quick_count.gemspec
|
|
162
174
|
homepage: https://github.com/TwilightCoders/quick_count
|
|
163
175
|
licenses:
|
|
164
176
|
- MIT
|