Seq scan vs. Index scan with different query conditions

From: eleven(at)ludojad(dot)itpp(dot)pl
To: pgsql-performance(at)postgresql(dot)org
Subject: Seq scan vs. Index scan with different query conditions
Date: 2004-07-05 10:15:15
Message-ID: 20040705101515.GA12879@ludojad.itpp.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hello,

Can anybody suggest any hint on this:

temp=> EXPLAIN SELECT DISTINCT "number" FROM "tablex" WHERE "Date" BETWEEN '2004-06-28'::date AND '2004-07-04'::date AND "Time" BETWEEN '00:00:00'::time AND '18:01:00'::time;

Unique (cost=305669.92..306119.43 rows=89 width=8)
-> Sort (cost=305669.92..305894.67 rows=89903 width=8)
Sort Key: "number"
-> Index Scan using "DateTimeIndex" on "tablex" (cost=0.00..298272.66 rows=89903 width=8)
Index Cond: (("Date" >= '2004-06-28'::date) AND ("Date" <= '2004-07-04'::date) AND ("Time" >= '00:00:00'::time without time zone) AND ("Time" <= '18:01:00'::time without time zone))

temp=> EXPLAIN SELECT DISTINCT "number" FROM "tablex" WHERE "Date" BETWEEN '2004-06-28'::date AND '2004-07-04'::date AND "Time" BETWEEN '00:00:00'::time AND '19:01:00'::time;

Unique (cost=315252.77..315742.27 rows=97 width=8)
-> Sort (cost=315252.77..315497.52 rows=97900 width=8)
Sort Key: "number"
-> Seq Scan on "tablex" (cost=0.00..307137.34 rows=97900 width=8)
Filter: (("Date" >= '2004-06-28'::date) AND ("Date" <= '2004-07-04'::date) AND ("Time" >= '00:00:00'::time without time zone) AND ("Time" <= '19:01:00'::time without time zone))

Basically, the difference is in upper "Time" value (as you can see, it's
18:01:00 in the first query and 19:01:00 in the other one).
The question is - why does it use index in first case and
it tries to do full sequential scan when the upper "Time" value
is different?

DateTimeIndex was created on both columns (Date/Time):
CREATE INDEX "DateTimeIndex" ON "tablex" USING btree ("Date", "Time");

--
wr

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Richard Huxton 2004-07-05 11:41:16 Re: Seq scan vs. Index scan with different query conditions
Previous Message Steinar H. Gunderson 2004-07-05 09:18:36 Odd sorting behaviour