Обсуждение: Question about index scan vs seq scan when using count()

Поиск
Список
Период
Сортировка

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

От
"Kashmira Patel \(kupatel\)"
Дата:
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

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

От
Andrew Sullivan
Дата:
On Thu, Feb 23, 2006 at 01:44:43PM -0800, Kashmira Patel (kupatel) wrote:
> 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. 

It may not require it, but it might select it anyway.

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

How much of the table is that?  How many rows?  EXPLAIN ANALYSE will
tell you if you have the right plan (estimate vs. actual).  The real
question is, are you sure an indexscan is faster?

A
-- 
Andrew Sullivan  | ajs@crankycanuck.ca
It is above all style through which power defers to reason.    --J. Robert Oppenheimer


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

От
"Kashmira Patel \(kupatel\)"
Дата:
> 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;

How much of the table is that?  How many rows?  EXPLAIN ANALYSE will
tell you if you have the right plan (estimate vs. actual).  The real
question is, are you sure an indexscan is faster?

[Kashmira] I did do an EXPLAIN ANALYZE as well, it also showed a
sequential scan. The table has about 600+ rows, with around 6 of them
matching the given id. Wouldn't an index scan be faster in this case?
Also, I have two more indices defined on this table, for other types of
queries I do on it. Would they be causing a problem? In general, is
there a rule of thumb as to when an index scan would be better than a
sequential scan?

Thanks,
Kashmira


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

От
"Kashmira Patel \(kupatel\)"
Дата:
The index has been around since I first created the table. I did VACUUM
ANALYZE, that should have taken care of analyzing this table, right?

-----Original Message-----
From: Tomas Vondra [mailto:tv@fuzzy.cz]
Sent: Thursday, February 23, 2006 2:29 PM
To: Kashmira Patel (kupatel)
Subject: Re: [SQL] Question about index scan vs seq scan when using
count()

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

For small tables the sequential scan is faster, that means less disk
reads is required the whole table than to use the index.

If it is a large table, the index should be used. Have you created the
index recently? Have you analyzed the table since that time (ANALYZE
tablename)

t.v.


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

От
Andrew Sullivan
Дата:
On Thu, Feb 23, 2006 at 02:25:34PM -0800, Kashmira Patel (kupatel) wrote:
> 
> [Kashmira] I did do an EXPLAIN ANALYZE as well, it also showed a
> sequential scan. The table has about 600+ rows, with around 6 of them
> matching the given id. Wouldn't an index scan be faster in this case?

EXPLAIN ANALYSE will always choose the same plan as EXPLAIN.  The
difference is that it shows you the estimate and actual.

I am surprised you're getting a seqscan for that, though.  Is there
something about the index you're not telling us?  

Is your system tuned correctly?  Maybe 600 rows is so small that a
seqscan's just as fast.

A

-- 
Andrew Sullivan  | ajs@crankycanuck.ca
"The year's penultimate month" is not in truth a good way of saying
November.    --H.W. Fowler


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

От
"Kashmira Patel \(kupatel\)"
Дата:
This is how I created the index:

CREATE INDEX msgid_index ON vm_message(msgid);

I guess it doing this because its a small table then. I will try putting
more values.

Thanks,
Kashmira

-----Original Message-----
From: pgsql-sql-owner@postgresql.org
[mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Andrew Sullivan
Sent: Thursday, February 23, 2006 2:47 PM
To: pgsql-sql@postgresql.org
Subject: Re: [SQL] Question about index scan vs seq scan when using
count()

On Thu, Feb 23, 2006 at 02:25:34PM -0800, Kashmira Patel (kupatel)
wrote:
>
> [Kashmira] I did do an EXPLAIN ANALYZE as well, it also showed a
> sequential scan. The table has about 600+ rows, with around 6 of them
> matching the given id. Wouldn't an index scan be faster in this case?

EXPLAIN ANALYSE will always choose the same plan as EXPLAIN.  The
difference is that it shows you the estimate and actual.

I am surprised you're getting a seqscan for that, though.  Is there
something about the index you're not telling us?

Is your system tuned correctly?  Maybe 600 rows is so small that a
seqscan's just as fast.

A

--
Andrew Sullivan  | ajs@crankycanuck.ca
"The year's penultimate month" is not in truth a good way of saying
November.    --H.W. Fowler

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate      subscribe-nomail command to
majordomo@postgresql.orgso that your      message can get through to the mailing list cleanly 


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

От
"Owen Jacobson"
Дата:
Kashmira Patel wrote:

> I did do an EXPLAIN ANALYZE as well, it also showed a
> sequential scan. The table has about 600+ rows, with around 6 of them
> matching the given id. Wouldn't an index scan be faster in this case?

Not necessarily.  It's entirely possible, if your rows are small, that 600 rows will fit on a single disk page.  The
indexwill be stored on a(t least one) separate disk page.  The cost of loading a page from disk pretty much swamps the
costof processing rows on a page, so in general the server tries to minimize the number of pages used.  To use an index
fora one-page table, it'd have to load two pages (the table and the index); to do a sequential scan over a one-page
tableit only has to load the table. 

Indexes are useful because they allow the DB to reduce the total number of pages loaded to complete a query.

-Owen