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 92A8832A9C1 for ; Mon, 11 Oct 2004 13:25:30 +0100 (BST) 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 90995-07 for ; Mon, 11 Oct 2004 12:25:22 +0000 (GMT) Received: from pw6tiger.de (pw6tiger.de [217.160.140.116]) by svr1.postgresql.org (Postfix) with SMTP id 2533232A937 for ; Mon, 11 Oct 2004 13:25:24 +0100 (BST) Received: (qmail 31354 invoked by uid 505); 11 Oct 2004 12:25:25 -0000 Received: from vygen@gmx.de by planwerk6 by uid 89 with qmail-scanner-1.14 (virus scan: Clear:. Processed in 0.170588 secs); 11 Oct 2004 12:25:25 -0000 Received: from isi-dial-146-203.isionline-dialin.de (HELO ?192.168.1.10?) (vygen@planwerk6.de@195.158.146.203) by pw6tiger.de with SMTP; 11 Oct 2004 12:25:24 -0000 From: Janning Vygen To: HyunSung Jang Subject: Re: why my query is not using index?? Date: Mon, 11 Oct 2004 14:25:02 +0200 User-Agent: KMail/1.7 References: <41639F3C.4050103@siche.net> In-Reply-To: <41639F3C.4050103@siche.net> Cc: pgsql-performance@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200410111425.02671.vygen@gmx.de> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/134 X-Sequence-Number: 8610 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