Unix Technical Forum

Seq scan is used for "select count(*) from table" queries

This is a discussion on Seq scan is used for "select count(*) from table" queries within the Pgsql Performance forums, part of the PostgreSQL category; --> Hi, When running queries like "select count(*) from table", seq scan is used. It makes such queries very slow ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql Performance

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 11:41 AM
staskorz@gmail.com
 
Posts: n/a
Default Seq scan is used for "select count(*) from table" queries

Hi,

When running queries like "select count(*) from table", seq scan is
used. It makes such queries very slow for large tables. I've tested it
on Oracle and it seems to also use the "seq scan". But, for some
reason, MS-SQL uses index scans for counting rows, so similar queries
are accomplished almost immediately.

I've tried to prevent PostgreSQL from using seq scans by running "set
enable_seqscan=off", but for some reason it was ignored and the seq
scan was used anyway.

Is there any way to force using index scans?

I am using PostgreSQL 8.1.

I've run both "VACUUM" and "ANALYZE" commands.

Here is the test table:

testdb1=# \d test1
Table "public.test1"
Column | Type | Modifiers
--------+-------------------+-----------
a | character varying | not null
b | character varying |
Indexes:
"test1_pkey" PRIMARY KEY, btree (a)

testdb1=#


"Explain analyze" output:

testdb1=# explain analyze select count(*) from test1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Aggregate (cost=21741.50..21741.51 rows=1 width=0) (actual
time=2781.242..2781.243 rows=1 loops=1)
-> Seq Scan on test1 (cost=0.00..19242.20 rows=999720 width=0)
(actual time=0.042..1575.085 rows=1000000 loops=1)
Total runtime: 2781.314 ms
(3 rows)

testdb1=#


A sample query that does use the index:

testdb1=# explain analyze select * from test1 where a = 'a0';
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
Index Scan using test1_pkey on test1 (cost=0.00..3.96 rows=1
width=32) (actual time=0.025..0.027 rows=1 loops=1)
Index Cond: ((a)::text = 'a0'::text)
Total runtime: 0.065 ms
(3 rows)

testdb1=#

Any help would be very much appreciated.

Thanks!

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 06:01 AM.


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