Question about index scan vs seq scan when using count()

From: "Kashmira Patel \(kupatel\)" <kupatel(at)cisco(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Question about index scan vs seq scan when using count()
Date: 2006-02-23 21:44:43
Message-ID: 18AE59788A3FC640A367E5652E664D800199BEA7@xmb-sjc-237.amer.cisco.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello,
I am using postgres version 7.4, and I read this in the
documentation:

"The aggregate functions sum and count always require a sequential scan
if applied to the entire table."

My understanding of this statement is that if I use count() without a
WHERE clause, then essentially, it is applied to the entire table and
hence requires a seq scan.
But it should not require a seq scan if I have a condition.

For example: I have a table vm_message with an index on column msgid.
Will the following do a sequential scan or an index?

select count(*) from vm_message where msgid = 3;

I used explain, and it said it would do a sequential scan. Why is that?
In fact explain select * from vm_message where msgid = 3 also did a
sequential scan.

What am I doing wrong here? I want it to use my index.

Thanks,
Kashmira

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Andrew Sullivan 2006-02-23 22:12:23 Re: Question about index scan vs seq scan when using count()
Previous Message Mario Splivalo 2006-02-23 20:07:58 Re: 'locking' the SELECTs based on indices...