Unix Technical Forum

SELECT COUNT(*) does a scan?

This is a discussion on SELECT COUNT(*) does a scan? within the pgsql Interfaces jdbc forums, part of the PostgreSQL category; --> When I do an EXPLAIN SELECT COUNT(*) FROM tablename, I noted that it does a table scan. I thought ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Interfaces jdbc

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-15-2008, 11:36 PM
David Wall
 
Posts: n/a
Default SELECT COUNT(*) does a scan?

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-15-2008, 11:36 PM
Oliver Jowett
 
Posts: n/a
Default Re: SELECT COUNT(*) does a scan?

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

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-15-2008, 11:37 PM
Dave Cramer
 
Posts: n/a
Default Re: SELECT COUNT(*) does a scan?

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



---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:42 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com