r/databases • u/devtimi • Dec 27 '17
Does SELECT COUNT(*) load the entire table's data to count it?
There is a person in a small community insisting that SELECT COUNT(1) is faster than SELECT COUNT(*) because the later will load all the columns and rows to count the rows. Does anybody know if this is true or have sources that explain it either way?
1
Upvotes
1
u/NotImplemented Dec 27 '17 edited Dec 27 '17
There is no difference. In MS SQL Server both statement result in the same execution plan. See here: https://www.mssqltips.com/sqlservertip/4460/sql-server-count-function-performance-comparison/
1
u/Tiquortoo Dec 29 '17
On a purely logical basis this misunderstanding makes sense. The query optimizer knows to not do that though.
1
u/[deleted] Feb 12 '18
In Postgres 9.2 (https://www.postgresql.org/docs/9.2/static/functions-aggregate.html):
Note: Users accustomed to working with other SQL database management systems might be disappointed by the performance of the count aggregate when it is applied to the entire table. A query like: SELECT count(*) FROM sometable; will require effort proportional to the size of the table: PostgreSQL will need to scan either the entire table or the entirety of an index which includes all rows in the table.