8

I've seen answers to this question for other databases (MySQL, SQL Server, etc.) but not for PostgreSQL. So, is COUNT(1) or COUNT(*) faster/better for selecting the row count of a table?

5
  • 1
    There is no DBMS where count(1) is faster than count(*). And in Postgres count(*) is actually slightly faster: blog.jooq.org/whats-faster-count-or-count1 Commented Mar 23, 2022 at 14:19
  • 1
    Any database system, any at all, where this actually makes a difference, is arguably broken, as the optimization (if one is required at all) is utterly trivial. The worst thing about this is that any actual difference (such as still exists in Postgres, apparently) will be seized on by people to prefer one expression or the other (and memorized long past the point of validity, probably), wasting a bunch of (human) memory and brain cycles. It's worth fixing for that reason alone. Commented Mar 23, 2022 at 14:22
  • 4
    count(*) is the 'standard' way. Don't confuse people. Commented Mar 23, 2022 at 14:26
  • General rule of thumb, follow the documentation which of course uses COUNT(*) to count rows in a result. The devs will normally optimize the documented scenario(s) before any others. Postgres doc: postgresql.org/docs/current/functions-aggregate.html Commented Mar 23, 2022 at 16:05
  • 1
    Postgres has a separate (faster) implementation for count(*). See: dba.stackexchange.com/a/309924/3684 And count(1) is utterly pointless to begin with, in any RDBMS (that I know of). Generally replace that with count(*). Commented Mar 23, 2022 at 18:15

2 Answers 2

12

Benchmarking the difference

The last time I've benchmarked the difference between COUNT(*) and COUNT(1) for PostgreSQL 11.3, I've found that COUNT(*) was about 10% faster. The explanation by Vik Fearing at the time has been that the constant expression 1 (or at least its nullability) is being evaluated for the entire count loop. I haven't checked whether this has been fixed in PostgreSQL 14.

Don't worry about this in real world queries

However, you shouldn't worry about such a performance difference. The difference of 10% was measurable in a benchmark, but I doubt you can consistently measure such a difference in an ordinary query. Also, ideally, all SQL vendors optimise the two things in the same way, given that 1 is a constant expression, and thus can be eliminated. As mentioned in the above article, I couldn't find any difference in any other RDBMS that I've tested (MySQL, Oracle, SQL Server), and I wouldn't expect there to be any difference.

Sign up to request clarification or add additional context in comments.

4 Comments

OK. I suppose at some point I should just test it, since a lot can change from version 11 to 14.
"I haven't checked whether this has been fixed in PostgreSQL 14" - I don't think so. The Postgres devs don't really think it's worth the effort: postgresql.org/message-id/flat/…
@a_horse_with_no_name: Thanks for digging it up. Someone might still change their opinion in the future, though...
I don't believe that the difference will be measurable if all the records must come from disk.
2

Just as an additional data point, it looks like PostgreSQL does interpret different COUNT statements in a different way. I have a table with 40 million records running on PostgreSQL 14.3 and these are the results that I get:

select count(1) from "DATA" p -- 2m3s, 2m3s, 2m17s

select count(*) from "DATA" p -- 4m39s, 4m39s, 3m16s

select count(distinct "ID") from "DATA" p -- 4m39, 4m46s

select count("ID") from "DATA" p -- 2m31s, 2m25s

This seems to show that COUNT(1) is a better option in general than COUNT(*). The timing has been taken a few times, one after the other so it should be pretty accurate.

1 Comment

These results run contrary to the other answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.