0.01
No release in over 3 years
Low commit activity in last 3 years
Installs two database functions, `quick_count` and `count_estimate` for getting count estimations on large tables
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

Runtime

>= 4, < 6
>= 0
>= 4, < 6
 Project Readme

Version      CI Status   Code Quality

QuickCount

Unfortunately, it's currently notoriously difficult and expensive to get an exact count on large tables.

Luckily, there are some tricks for quickly getting fairly accurate estimates. For example, on a table with over 450 million records, you can get a 99.82% accurate count within a fraction of of the time. See the table below for an example dataset.

Supports:

SQL Result Accuracy Time
SELECT count(*) FROM small_table; 2037104 100.0000000% 4.900s
Post.quick_count 2036407 99.96578476% 0.050s
SELECT count(*) FROM medium_table; 81716243 100.0000000% 257.5s
Post.quick_count 81600513 99.85837577% 0.048s
SELECT count(*) FROM large_table; 455270802 100.0000000% 310.6s
Post.quick_count 454448393 99.81935828% 0.046s

These metrics were pulled from real databases being used in a production environment.

Installation

Add this line to your application's Gemfile:

gem 'quick_count'

And then execute:

$ bundle

That's it. QuickCount automatically integrates with ActiveRecord via a Railtie — no setup step required.

Usage

# Fast estimated count for large tables
User.quick_count

# Override the default threshold (500,000) on a case-by-case basis
User.quick_count(threshold: 1_000_000)

# Estimate row count for an arbitrary query
User.where(active: true).count_estimate

If the estimated count is below the threshold, quick_count falls back to an exact SELECT COUNT(*). For tables above the threshold, it returns the estimate directly.

License

The gem is available as open source under the terms of the MIT License.

Development

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.

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/TwilightCoders/quick_count.