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.

1 Answer 1


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).

  • 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, 2011 at 21:39
  • Take a look at wiki.postgresql.org/wiki/… (and various other chunks of the wiki)
    – voretaq7
    Feb 9, 2011 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .