Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

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 wiki.postgresql.org/wiki/… (and various other chunks of the wiki) –  voretaq7 Feb 9 '11 at 21:52

Your Answer

 
discard

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.