Company
Date Published
Author
Kirk Haines
Word count
2371
Language
English
Hacker News points
None

Summary

Counting records in a database table seems like it should be a fast task. However, with tools like MySQL and PostgreSQL, that's not the case. In these databases, counting rows is slow because they use Multiversion Concurrency Control, which means each unique transaction may have slightly different data from other transactions processed at the same time. This makes it impossible to get an accurate count of rows in a database table like there is with MySQL/MyISAM tables. To overcome this challenge, one can leverage indexes or use triggers and functions to maintain a MySQL/MyISAM-like count that is kept up-to-date at all times. PostgreSQL and MySQL/InnoDB engines also have estimated row counts stored in their respective tables, which can be used for fast but inaccurate estimates of table size. Additionally, using the EXPLAIN command and scraping data from it can help build a count estimator for queries. The best approach depends on whether an exact or estimated count is needed, with accurate counts requiring more efficient queries, while estimated counts offer faster performance at the cost of accuracy.