Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've done some reading in google and the only thing I did was get myself confused. Some of the people say that count() is slower with many records, others say you can cache the number or even use sequence...

What's the best way to count many rows in a table with best performance and lowest cpu/ram usage? By many I mean >=500,000 rows.

share|improve this question
up vote 4 down vote accepted

The best way I know of to tweak count()'s performance on huge data sets is to count() something that's been indexed (if you're trying to count a partial result, you can create a matching partial index).

share|improve this answer
This seems perfectly logical. I've never did this in postgresql.. Do you have an example from the net which you think will do the trick ? – tftd Feb 9 '11 at 21:39
Take a look at… (and various other chunks of the wiki) – voretaq7 Feb 9 '11 at 21:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.