Re: why does explain tell me I'm using a seq scan?

From: Adam Ruth <aruth(at)intercation(dot)com>
To: Mark Harrison <mh(at)pixar(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: why does explain tell me I'm using a seq scan?
Date: 2003-11-08 00:33:44
Message-ID: 35A687A6-1183-11D8-BC58-000A959D1424@intercation.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Nov 7, 2003, at 5:11 PM, Mark Harrison wrote:

> I have indexed two columns in a table. Can somebody explain to me why
> the first query below uses an Index Scan while the second uses a Seq
> Scan?
>
> Many TIA!
> Mark
>
>
> planb=# \d abcs
> Table "public.abcs"
> Column | Type | Modifiers
> -----------+------------------------
> +-----------------------------------------------
> abcid | integer | not null default
> nextval('abcid_seq'::text)
> type | character varying(255) |
> versionof | integer |
> Indexes: abcs_pkey primary key btree (abcid),
> abcs_versionof btree (versionof)
>
> planb=# explain select type from abcs where abcid = 6339;
> QUERY PLAN
> -----------------------------------------------------------------------
> -----
> Index Scan using abcs_pkey on abcs (cost=0.00..6.01 rows=1 width=145)
> Index Cond: (abcid = 6339)
> (2 rows)
>
> planb=# explain select type from abcs where versionof = 6339;
> QUERY PLAN
> ----------------------------------------------------------------
> Seq Scan on abcs (cost=0.00..59182.10 rows=16137 width=145)
> Filter: (versionof = 6339)
> (2 rows)
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
>
>
The first query is using a unique index while the second is not.

The first query is guaranteed to only return a single row, so it
doesn't take many records before the index is faster.

The optimizer is looking at the statistics and figuring that the second
query could be done better with a sequential scan (perhaps there aren't
many rows). Have you analyzed the table to get the statistics up to
date? It could also be that the seq scan is faster with the size of
your table.

Adam Ruth

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2003-11-08 01:54:02 Re: Recovery Data Cant Be!!!
Previous Message Rod Taylor 2003-11-08 00:19:24 Re: why does explain tell me I'm using a seq scan?