X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9AFCCD1B1DA for ; Mon, 5 Jul 2004 07:11:24 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 11168-08 for ; Mon, 5 Jul 2004 10:11:21 +0000 (GMT) Received: from ludojad.itpp.pl (ludojad.itpp.pl [193.41.112.10]) by svr1.postgresql.org (Postfix) with ESMTP id 20BEFD1B196 for ; Mon, 5 Jul 2004 07:11:18 -0300 (ADT) Received: by ludojad.itpp.pl (Postfix, from userid 1111) id 7E4A6B71DF; Mon, 5 Jul 2004 12:15:15 +0200 (CEST) Date: Mon, 5 Jul 2004 12:15:15 +0200 From: eleven@ludojad.itpp.pl To: pgsql-performance@postgresql.org Subject: Seq scan vs. Index scan with different query conditions Message-ID: <20040705101515.GA12879@ludojad.itpp.pl> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-2 Content-Disposition: inline User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.3 tagged_above=0.0 required=5.0 tests=NO_REAL_NAME X-Spam-Level: X-Archive-Number: 200407/9 X-Sequence-Number: 7400 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