Thread: seq scan on indexed column

seq scan on indexed column

From
"Zhang, Anna"
Date:
Hi,
I always have questions on sql tunning, here is the one:

gtld_analysis=# \d gtld_owner

                  Table "gtld_owner"
   Attribute    |          Type          |  Modifier
----------------+------------------------+------------
 owner_name     | character varying(100) |
 netblock_start | integer                | not null    /* primary key */
 netblock_end   | integer                | not null
 country        | character varying(100) |
 country_c      | smallint               | default -1
 region         | character varying(100) |
 region_c       | smallint               | default -1
 city           | character varying(100) |
 city_c         | smallint               | default -1
 lat            | numeric(9,4)           |
 long           | numeric(9,4)           |
Indices: gtld_owner_pkey,
         owner_nb_end

gtld_analysis=# select count(*) from gtld_owner;
  count
---------
 2174335
(1 row)

gtld_analysis=# explain SELECT NETBLOCK_START
gtld_analysis-# FROM GTLD_OWNER
gtld_analysis-# WHERE NETBLOCK_START = -2147483648;
NOTICE:  QUERY PLAN:

Seq Scan on gtld_owner  (cost=0.00..80021.37 rows=23 width=4)

EXPLAIN


Why it didn't use index scan? what rows=23 means? We have an application
which loops each row and select netblock_start, and it is slow overall.

Anyone can give me a clue? Thanks!

Anna Zhang



Re: seq scan on indexed column

From
postgresql@fruru.com
Date:
On Thu, 14 Mar 2002, Zhang, Anna wrote:

> gtld_analysis=# explain SELECT NETBLOCK_START
> gtld_analysis-# FROM GTLD_OWNER
> gtld_analysis-# WHERE NETBLOCK_START = -2147483648;

You might want to try the same query but with the constant integer
enclosed in single quotes.  I find that (at least for int8) this changes
the behaviour wrt index usage -- most probably due to automatic
typecasting in the postgresql SQL parser.

Hope this helps...
Tycho

--
Tycho Fruru            tycho.fruru@conostix.com
Users' impressions of different operating systems, expressed as emoticons:
Linux:   :)
Windows: XP



Re: seq scan on indexed column

From
"Zhang, Anna"
Date:
Tycho,You saved us!
This problem really bothered us for a while, now we know why we had hard
time before. But I think it is a postgres bug. Thanks a lot!

Anna Zhang

-----Original Message-----
From: postgresql@fruru.com [mailto:postgresql@fruru.com]
Sent: Thursday, March 14, 2002 5:29 PM
To: Zhang, Anna
Cc: 'pgsql-admin@postgresql.org'
Subject: Re: [ADMIN] seq scan on indexed column


On Thu, 14 Mar 2002, Zhang, Anna wrote:

> gtld_analysis=# explain SELECT NETBLOCK_START
> gtld_analysis-# FROM GTLD_OWNER
> gtld_analysis-# WHERE NETBLOCK_START = -2147483648;

You might want to try the same query but with the constant integer
enclosed in single quotes.  I find that (at least for int8) this changes
the behaviour wrt index usage -- most probably due to automatic
typecasting in the postgresql SQL parser.

Hope this helps...
Tycho

--
Tycho Fruru            tycho.fruru@conostix.com
Users' impressions of different operating systems, expressed as emoticons:
Linux:   :)
Windows: XP