3 ms·
> I would strongly doubt that count() without a where clause isn’t already highly optimized It's actually not well optimized in PostgreSQL because of MVCC. See
by phamilton 3y ago
> I would strongly doubt that count() without a where clause isn’t already highly optimized
It's actually not well optimized in PostgreSQL because of MVCC. See https://wiki.postgresql.org/wiki/Slow_Counting https://wiki.postgresql.org/wiki/Slow_Counting
TL;DR:
The fact that multiple transactions can see different
states of the data means that there can be no
straightforward way for "COUNT(*)" to summarize data across
the whole table. PostgreSQL must walk through all rows to
determine visibility.