pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joseph Lemm <joelemm@yahoo.com>
To: pgsql-admin@postgresql.org
Subject: Re: what is the cause that scan type is showing as 'seq scan' after indexing
Date: Sat, 3 Jan 2004 21:30:20 -0800 (PST)
Message-ID: <20040104053020.59145.qmail@web40909.mail.yahoo.com> (raw)

IN RELATION TO THIS POST:

On Sun, Jan 26, 2003 at 06:31:50PM +0530, shreedhar wrote:
> Hi All,
> 
> Before indexing query plan was showing cost as 40.00, after indexing query
> plan again showing as 'seq scan' and cost as 3060.55.
> The field which i indexed is primary key to this table.
> May i know
> 1) what is the cause that scan type is showing as 'seq scan' after indexing
> also
> 2) why it is showing cost as high value compare to previous.

TO WHICH ROSS REPLIED:

>You trimmed out the other parts of the EXPLAIN, so I'm just guessing,
>but that cost seems suspiciously round: I'm guessing that you haven't
>run VACUUM ANALYZE at all. One thing indexing does is update the 'number
>of tuples' statistic. See the archives for why sequential scans still
>show up (short answer: index scans aren't free, so at some point, it's
>cheaper to scan the entire table than to scan both the index and the
>subset of the table returned)



OK, so then what is the explanation for this:

               Table "public.post"
 Column |            Type             | Modifiers
--------+-----------------------------+-----------
 id     | integer                     |
 author | character varying(80)       |
 text   | text                        |
 hidden | boolean                     |
 date   | timestamp without time zone |
 host   | character varying(80)       |
Indexes: idx_post_id unique btree (id),
         post_author_index btree (author)


VACUUM ANALYZE;
VACUUM

EXPLAIN ANALYZE select host from post where author='George';
                                               QUERY PLAN

--------------------------------------------------------------------------------------------------------
 Seq Scan on post  (cost=0.00..2869.30 rows=1768 width=27) (actual
time=0.23..520.65 rows=1774 loops=1)
   Filter: (author = 'George'::character varying)
 Total runtime: 525.77 msec
(3 rows)

So the optimizer decided it's less costly to do a sequential scan here than use
the index, right?


Now:

SET ENABLE_SEQSCAN=OFF;

EXPLAIN ANALYZE select host from post where author='George';
                                                            QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
 Index Scan using post_author_index on post  (cost=0.00..5253.63 rows=1768
width=27) (actual time=28.92..210.25 rows=1774 loops=1)
   Index Cond: (author = 'George'::character varying)
 Total runtime: 215.00 msec
(3 rows)


So if I force an index scan, I get much better performance (215 vs 525 msec).
Does this mean that the optimizer screwed up when it recommended a sequential
scan?

Thanks.


=====
J.

__________________________________
Do you Yahoo!?
Find out what made the Top Yahoo! Searches of 2003
http://search.yahoo.com/top2003



view thread (7+ messages)  latest in thread

Message-ID: <20040104053020.59145.qmail@web40909.mail.yahoo.com>
Permalink:  ../20040104053020.59145.qmail@web40909.mail.yahoo.com/
Also on:    postgresql.org/message-id/20040104053020.59145.qmail@web40909.mail.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: joelemm@yahoo.com
  Subject: Re: what is the cause that scan type is showing as 'seq scan' after indexing
  In-Reply-To: <20040104053020.59145.qmail@web40909.mail.yahoo.com>
  References: <20040104053020.59145.qmail@web40909.mail.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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