X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7E4D9DB6CC for ; Tue, 1 Nov 2005 15:11:45 -0400 (AST) 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 63805-10 for ; Tue, 1 Nov 2005 19:11:42 +0000 (GMT) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.187]) by svr1.postgresql.org (Postfix) with ESMTP id 4B109DB6BF for ; Tue, 1 Nov 2005 15:11:42 -0400 (AST) Received: from [194.25.154.138] (helo=webserv.wug.de) by mrelayeu.kundenserver.de (node=mrelayeu4) with ESMTP (Nemesis), id 0ML21M-1EX1Xo2ayz-0003Fy; Tue, 01 Nov 2005 20:11:40 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1EX1Y8-0000t2-00 for pgsql-sql@postgresql.org; Tue, 01 Nov 2005 20:12:00 +0100 Date: Tue, 1 Nov 2005 20:12:00 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Index lookup on > and < criteria Message-ID: <20051101191200.GA2934@webserv.wug-glas.de> Mail-Followup-To: "A. Kretschmer" , pgsql-sql@postgresql.org References: <4367B16B.2050003@vailsys.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4367B16B.2050003@vailsys.com> X-DEST: 208 X-MSMail-Priority: High X-Message-Flag: Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) X-OS: Linux - weil ich es mir Wert bin! X-Info: registrierter Linux-User 97922 http://counter.li.org User-Agent: mutt-ng 1.5.9i (Linux) X-Provags-ID: kundenserver.de abuse@kundenserver.de login:51ea9561fc3f6d9539a2acd743e4748c X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.394 required=5 tests=[AWL=0.000, MISSING_MIMEOLE=1.394] X-Spam-Score: 1.394 X-Spam-Level: * X-Archive-Number: 200511/5 X-Sequence-Number: 23257 am 01.11.2005, um 12:18:19 -0600 mailte David Durham folgendes: > Apologies if this questions is asked often. I'm doing some select > statements based on a timestamp field. I have an index on the field, and > when I use the '=' operator the index is used. However, if I use the '>' > or '<' operators, then it does a full table scan. I've got around 6 > million rows, so I would think that an index scan would be more > appropriate. > > > Here are the statements I'm looking at: > > select * from myTable where myTimeStamp = '10/1/2005'; > > uses an index. > > select max(myTimeStamp) from myTable; > select * from myTable where myTimeStamp < '10/2/2005'; > select * from myTable where myTimeStamp < '10/2/2005' and myTimeStamp > >= '10/1/2005'; > > do not use indexes. Can anyone point me to some info about what's going > on? I've started reading through the manual (chapter 13) which I think Nice question. My guess: The planner fels its better to use seq-scan. My test: ,----[ Test ] | Produktionsreport=# explain select * from bde_meldungen where zeitpunkt > '2005-08-30'::timestamp; | QUERY PLAN | ---------------------------------------------------------------------------- | Seq Scan on bde_meldungen (cost=0.00..33862.46 rows=55106 width=139) | Filter: (zeitpunkt > '2005-08-30 00:00:00'::timestamp without time zone) | (2 Zeilen) | | Produktionsreport=# explain select * from bde_meldungen where zeitpunkt > '2005-09-30'::timestamp; | QUERY PLAN | ------------------------------------------------------------------------------------------- | Index Scan using bde_zeitpunkt on bde_meldungen (cost=0.00..8255.23 rows=9521 width=139) | Index Cond: (zeitpunkt > '2005-09-30 00:00:00'::timestamp without time zone) | (2 Zeilen) `---- Sorry about the german column names, 'zeitpunkt' is a timestamp. On the first query the result set is estimeted 55.000 rows long -> seq-scan. The second test: estimated to rows=9521 -> index scan. Btw.: min/max cant use index, this is coming with 8.1. I'm using for examples above 7.4.6. PS.: you can use set ..., example: ,----[ Test with set enable_seqscan=... ] | Produktionsreport=# set enable_seqscan=on; | SET | Produktionsreport=# explain analyse select * from bde_meldungen where zeitpunkt > '2005-08-30'::timestamp; | QUERY PLAN | --------------------------------------------------------------------------------------------------------------------------- | Seq Scan on bde_meldungen (cost=0.00..33862.46 rows=55106 width=139) (actual time=2574.004..4892.563 rows=99915 loops=1) | Filter: (zeitpunkt > '2005-08-30 00:00:00'::timestamp without time zone) | Total runtime: 4971.179 ms | (3 Zeilen) | | Produktionsreport=# set enable_seqscan=off; | SET | Produktionsreport=# explain analyse select * from bde_meldungen where zeitpunkt > '2005-08-30'::timestamp; | QUERY PLAN | ----------------------------------------------------------------------------------------------------------------------------------------------- | Index Scan using bde_zeitpunkt on bde_meldungen (cost=0.00..47679.39 rows=55106 width=139) (actual time=57.387..1649.591 rows=99915 loops=1) | Index Cond: (zeitpunkt > '2005-08-30 00:00:00'::timestamp without time zone) | Total runtime: 1729.420 ms | (3 Zeilen) `---- Now it using the index _and_ it is faster! HTH, Andreas -- Andreas Kretschmer (Kontakt: siehe Header) Heynitz: 035242/47212, D1: 0160/7141639 GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net === Schollglas Unternehmensgruppe ===