pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedSELECT COUNT(*) does a scan?
3+ messages / 3 participants
[nested] [flat]
* SELECT COUNT(*) does a scan?
@ 2005-09-08 15:22 David Wall <d.wall@computer.org>
0 siblings, 2 replies; 3+ messages in thread
From: David Wall @ 2005-09-08 15:22 UTC (permalink / raw)
To: pgsql-jdbc
When I do an EXPLAIN SELECT COUNT(*) FROM tablename, I noted that it
does a table scan. I thought PG had some sort of table stat that kept
track of the current number of rows in a table, but that doesn't appear
to always be the case.
It seems that right after a VACUUM ANALYZE, that command is very fast
(on a table with 100,000+ rows), but it can also get quite slow, as if a
table scan is taking place.
Does this make sense? Is there an algorithm that says to use the stats
from analyze only until sufficient updates/inserts/deletes have taken
place to make them "out of date"?
David
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: SELECT COUNT(*) does a scan?
@ 2005-09-08 15:40 Oliver Jowett <oliver@opencloud.com>
parent: David Wall <d.wall@computer.org>
1 sibling, 0 replies; 3+ messages in thread
From: Oliver Jowett @ 2005-09-08 15:40 UTC (permalink / raw)
To: David Wall <d.wall@computer.org>; +Cc: pgsql-jdbc
David Wall wrote:
> When I do an EXPLAIN SELECT COUNT(*) FROM tablename, I noted that it
> does a table scan. I thought PG had some sort of table stat that kept
> track of the current number of rows in a table, but that doesn't appear
> to always be the case.
It's not the case, and this is a FAQ -- search archives.postgresql.org
for more details (the short version is that maintaining a row count
doesn't work well with MVCC).
> It seems that right after a VACUUM ANALYZE, that command is very fast (on a table with 100,000+ rows), but it can also get quite slow, as if a table scan is taking place.
> Does this make sense? Is there an algorithm that says to use the stats from analyze only until sufficient updates/inserts/deletes have taken place to make them "out of date"?
Most likely a VACUUM ANALYZE is just pulling the whole table into cache,
so there is less disk I/O needed to do the scan.
-O
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: SELECT COUNT(*) does a scan?
@ 2005-09-08 18:15 Dave Cramer <davec@fastcrypt.com>
parent: David Wall <d.wall@computer.org>
1 sibling, 0 replies; 3+ messages in thread
From: Dave Cramer @ 2005-09-08 18:15 UTC (permalink / raw)
To: David Wall <d.wall@computer.org>; +Cc: pgsql-jdbc
See Oliver's post:
Additionally you can get count to use an index, but you need a where
clause.
Dave
On 8-Sep-05, at 11:22 AM, David Wall wrote:
> When I do an EXPLAIN SELECT COUNT(*) FROM tablename, I noted that
> it does a table scan. I thought PG had some sort of table stat
> that kept track of the current number of rows in a table, but that
> doesn't appear to always be the case.
>
> It seems that right after a VACUUM ANALYZE, that command is very
> fast (on a table with 100,000+ rows), but it can also get quite
> slow, as if a table scan is taking place.
> Does this make sense? Is there an algorithm that says to use the
> stats from analyze only until sufficient updates/inserts/deletes
> have taken place to make them "out of date"?
>
> David
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2005-09-08 18:15 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-09-08 15:22 SELECT COUNT(*) does a scan? David Wall <d.wall@computer.org>
2005-09-08 15:40 ` Oliver Jowett <oliver@opencloud.com>
2005-09-08 18:15 ` Dave Cramer <davec@fastcrypt.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox