Re: Really really slow select count(*) - Mailing list pgsql-performance

From Greg Smith
Subject Re: Really really slow select count(*)
Date
Msg-id 4D4C138C.7010304@2ndquadrant.com
Whole thread Raw
In response to Really really slow select count(*)  (felix <crucialfelix@gmail.com>)
Responses Re: Really really slow select count(*)  (Shaun Thomas <sthomas@peak6.com>)
List pgsql-performance
felix wrote:
explain analyze select count(*) from fastadder_fastadderstatus;

Aggregate  (cost=62458.73..62458.74 rows=1 width=0) (actual time=77130.000..77130.000 rows=1 loops=1)
   ->  Seq Scan on fastadder_fastadderstatus  (cost=0.00..61701.18 rows=303018 width=0) (actual time=50.000..76930.000 rows=302479 loops=1)
 Total runtime: 77250.000 ms


PostgreSQL version?  If you're running on 8.3 or earlier, I would be suspicous that your Free Space Map has been overrun.

What you are seeing is that the table itself is much larger on disk than it's supposed to be.  That can be caused by frequent UPDATEs if you don't have vacuum cleanup working effectively, you'll get lots of dead sections left behind from UPDATEs in the middle.  The best way to fix all this is to run CLUSTER on the table.  That will introduce a bit of downtime while it holds a lock on the table (only a few minutes based on what you've shown here), but the copy you'll have afterwards won't be spread all over disk anymore.

-- 
Greg Smith   2ndQuadrant US    greg@2ndQuadrant.com   Baltimore, MD
PostgreSQL Training, Services, and 24x7 Support  www.2ndQuadrant.us
"PostgreSQL 9.0 High Performance": http://www.2ndQuadrant.com/books

pgsql-performance by date:

Previous
From: Kenneth Marshall
Date:
Subject: Re: [HACKERS] Slow count(*) again...
Next
From: Shaun Thomas
Date:
Subject: Re: Really really slow select count(*)