Re: why my query is not using index??

From: Janning Vygen <vygen(at)gmx(dot)de>
To: HyunSung Jang <siche(at)siche(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: why my query is not using index??
Date: 2004-10-11 12:25:02
Message-ID: 200410111425.02671.vygen@gmx.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Am Mittwoch, 6. Oktober 2004 09:31 schrieben Sie:
> postgres=# explain ANALYZE select * from test where today < '2004-01-01';
> QUERY PLAN
>------------------------- Seq Scan on test (cost=0.00..19.51 rows=334
> width=44) (actual
> time=0.545..2.429 rows=721 loops=1)
> Filter: (today < '2004-01-01 00:00:00'::timestamp without time zone)
> Total runtime: 3.072 ms
> (3 rows)
>
> postgres=# explain ANALYZE select * from test where today > '2003-01-01'
> and today < '2004-01-01';
> QUERY
> PLAN
> --------------------------------------------------------------- Index
> Scan using idx_today on test (cost=0.00..18.89 rows=6 width=44) (actual
> time=0.055..1.098 rows=365 loops=1)
> Index Cond: ((today > '2003-01-01 00:00:00'::timestamp without time
> zone) AND (today < '2004-01-01 00:00:00'::timestamp without time zone))
> Total runtime: 1.471 ms
> (3 rows)
>
> hello
>
> I was expected 1st query should using index, but it doesn't
> 2nd query doing perfect as you see.

postgres uses a seq scan if its faster. In your case postgres seems to know
that most of your rows have a date < 2004-01-01 and so doesn't need to
consult the index if it has to read every page anyway. seq scan can be faster
on small tables. try (in psql) "SET enable_seqscan TO off;" before running
your query and see how postgres plans it without using seq scan.

janning

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2004-10-11 14:14:18 Re: Views, joins and LIMIT
Previous Message Janning Vygen 2004-10-11 12:15:56 Re: EXPLAIN ANALYZE much slower than running query normally