Sobes.tech
Middle

Which is faster: count(*) or count(column_name)?

sobes.tech AI

Answer from AI

In most DBMS, count(*) and count(column_name) work with different logic:

  • count(*) counts all rows, including those where column values may be NULL.
  • count(column_name) counts only rows where the specified column's value is not NULL.

In terms of performance, count(*) is usually faster or at least not slower, because the DBMS doesn't need to check the values in a specific column, it just counts rows. In some optimized engines, count(*) can use metadata or indexes for quick counting.

Example:

SELECT count(*) FROM users; -- counts all rows
SELECT count(email) FROM users; -- counts only rows where email is not NULL

Conclusion: if you just need the total number of rows, it's better to use count(*) — it's usually faster and more accurate.