postgres_loose_table_counts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in postgres_loose_table_counts.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ require "postgres_loose_table_counts/version"
2
+ require "active_record"
3
+
4
+ module PostgresLooseTableCounts
5
+ def loose_count
6
+ connection.select_value "select reltuples from pg_class where relname='#{table_name}'"
7
+ end
8
+ end
9
+
10
+ ActiveRecord::Base.extend(PostgresLooseTableCounts)
@@ -0,0 +1,3 @@
1
+ module PostgresLooseTableCounts
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "postgres_loose_table_counts/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "postgres_loose_table_counts"
7
+ s.version = PostgresLooseTableCounts::VERSION
8
+ s.authors = ["John Barton"]
9
+ s.email = ["jrbarton@gmail.com"]
10
+ s.homepage = "http://github.com/joho/postgres_loose_table_counts"
11
+ s.summary = %q{Postgres counts can be sometimes slow for large records, and sometimes (ie pagination) being exact isn't really that important. Read http://wiki.postgresql.org/wiki/Slow_Counting for more details.}
12
+ s.description = %q{Get a rough idea of a model count from pg_class instead of potentially expensive count()}
13
+
14
+ s.rubyforge_project = "postgres_loose_table_counts"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "bundler"
23
+ s.add_runtime_dependency "activerecord", ">= 3.0"
24
+ # lol, this won't work on mysql
25
+ s.add_runtime_dependency "pg"
26
+ end
@@ -0,0 +1,34 @@
1
+ # Postgres Loose Table Counts
2
+
3
+ * Sometimes select count(*) from table is slow on postgres
4
+ * Sometimes you don't care about 100% accurate counts (ie. infinte scroll pagination, homepage vanity "user counts", etc)
5
+ * Sometimes, you're lazy, and google for a quick fix, and install this gem
6
+
7
+ For more background, read the [Postgres wiki page on slow counting](http://wiki.postgresql.org/wiki/Slow_Counting)
8
+
9
+ ## Usage
10
+
11
+ Put this in your Gemfile
12
+
13
+ gem "postgres_loose_table_counts"
14
+
15
+ Put this in your app
16
+
17
+ @total_thingos = Thingo.loose_count # => 345523
18
+
19
+ Enjoy being that little bit more webscale.
20
+
21
+ By [John Barton](http://whoisjohnbarton.com) for [Goodfilms](http://goodfil.ms) which is the best online movie site of all time.
22
+
23
+ ---
24
+ ## Licence
25
+
26
+
27
+ Copyright (C) <2012> John Barton, Goodfilms
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30
+
31
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
32
+
33
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postgres_loose_table_counts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Barton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70365471211980 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70365471211980
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70365471211480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70365471211480
36
+ - !ruby/object:Gem::Dependency
37
+ name: pg
38
+ requirement: &70365471211060 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70365471211060
47
+ description: Get a rough idea of a model count from pg_class instead of potentially
48
+ expensive count()
49
+ email:
50
+ - jrbarton@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - Rakefile
58
+ - lib/postgres_loose_table_counts.rb
59
+ - lib/postgres_loose_table_counts/version.rb
60
+ - postgres_loose_table_counts.gemspec
61
+ - readme.md
62
+ homepage: http://github.com/joho/postgres_loose_table_counts
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project: postgres_loose_table_counts
82
+ rubygems_version: 1.8.11
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Postgres counts can be sometimes slow for large records, and sometimes (ie
86
+ pagination) being exact isn't really that important. Read http://wiki.postgresql.org/wiki/Slow_Counting
87
+ for more details.
88
+ test_files: []