A Ruby gem for quickly getting approximate row counts for large database tables with >99% accuracy for PostgreSQL.
FastCount is a Ruby gem that provides fast, approximate row count estimations for large database tables. It solves the problem of expensive and slow exact `COUNT(*)` queries on massive tables by using database statistics to deliver >99% accurate estimates in milliseconds. The gem supports PostgreSQL, MySQL, MariaDB, and SQLite.
Ruby on Rails developers working with large database tables who need quick row count approximations for analytics, pagination, or monitoring without the performance penalty of exact counts.
Developers choose FastCount because it dramatically reduces query times from minutes to milliseconds while maintaining high accuracy, supports multiple databases, and integrates seamlessly with ActiveRecord. It's particularly valuable in production environments where exact counts are impractical.
Quickly get a count estimation for large tables (>99% of accuracy for PostgreSQL).
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
On PostgreSQL tables with over 450 million rows, FastCount delivers >99% accurate counts in under 0.05 seconds, compared to over 300 seconds for exact counts, as shown in the README's benchmark table.
Works seamlessly with PostgreSQL, MySQL, MariaDB, and SQLite, adapting estimation techniques to each database's internal statistics, providing flexibility across different stacks.
Uses 'Loose Index Scan' to provide exact distinct value counts orders of magnitude faster than standard queries, ideal for columns with few unique values relative to total rows.
Allows setting a size threshold to automatically switch between exact and approximate counts, balancing speed and accuracy based on table size without manual intervention.
The README explicitly states that estimates on MySQL can vary by 40-50% from actual values, making it unreliable for precision-sensitive applications or tight SLAs.
The estimated_count feature relies on database query planner output, which the README warns can be 'very imprecise' and is best suited for rough order-of-magnitude guesses, not precise planning.
Requires creating a database function via a Rails migration for PostgreSQL, adding deployment complexity compared to drop-in gems that work out of the box.
Limited to Ruby 3.2+ and ActiveRecord 7.1+, potentially excluding legacy projects or teams stuck on older, stable versions without upgrade paths.