pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: eleven@ludojad.itpp.pl
To: pgsql-performance@postgresql.org
Subject: Seq scan vs. Index scan with different query conditions
Date: Mon, 5 Jul 2004 12:15:15 +0200
Message-ID: <20040705101515.GA12879@ludojad.itpp.pl> (raw)

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



view thread (5+ messages)  latest in thread

Message-ID: <20040705101515.GA12879@ludojad.itpp.pl>
Permalink:  ../20040705101515.GA12879@ludojad.itpp.pl/
Also on:    postgresql.org/message-id/20040705101515.GA12879@ludojad.itpp.pl

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: eleven@ludojad.itpp.pl
  Subject: Re: Seq scan vs. Index scan with different query conditions
  In-Reply-To: <20040705101515.GA12879@ludojad.itpp.pl>

* 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