Count queries without a where clause cause PostgreSQL to do a full table scan to count the exact number of rows. Approximately uses metadata stored by PostgreSQL to return an approximate count of the rows without the full table scan.
The number will be correct immediately following a VACUUM
or ANALYZE
on
the table and will not be updated until the next VACUUM
or ANALYZE
. This
makes it most appropriate for tables that are subject to [autovacuum
] on a
regular basis. See the Postgres documentation on [row estimation] and
autovacuum for more.
Add this line to your application's Gemfile:
gem 'approximately'
And then execute:
$ bundle
Or install it yourself as:
$ gem install approximately
ApproximateCount.of(Vote.table_name) # => 256233
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request