X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B9DDDD791C for ; Fri, 26 Aug 2005 13:28:06 -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 32381-04 for ; Fri, 26 Aug 2005 16:28:04 +0000 (GMT) Received: from flake.decibel.org (flake.decibel.org [67.100.216.10]) by svr1.postgresql.org (Postfix) with ESMTP id BC865D7745 for ; Fri, 26 Aug 2005 13:28:02 -0300 (ADT) Received: by flake.decibel.org (Postfix, from userid 1001) id 7133B1529A; Fri, 26 Aug 2005 11:28:02 -0500 (CDT) Date: Fri, 26 Aug 2005 11:28:02 -0500 From: "Jim C. Nasby" To: Tom Lane Cc: Mark Fox , pgsql-performance@postgresql.org Subject: Re: Performance indexing of a simple query Message-ID: <20050826162801.GF11282@pervasive.com> References: <16554.1124926920@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <16554.1124926920@sss.pgh.pa.us> X-Operating-System: FreeBSD 4.11-RELEASE-p10 i386 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.9i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.012 required=5 tests=[AWL=0.012] X-Spam-Level: X-Archive-Number: 200508/447 X-Sequence-Number: 14194 On Wed, Aug 24, 2005 at 07:42:00PM -0400, Tom Lane wrote: > Mark Fox writes: > > The sort of queries I want to execute (among others) are like: > > SELECT * FROM jobs > > WHERE completion_time > SOMEDATE AND start_time < SOMEDATE; > > In plain english: All the jobs that were running at SOMEDATE. Uh, the plain english and the SQL don't match. That query will find every job that was NOT running at the time you said. > AFAIK there is no good way to do this with btree indexes; the problem > is that it's fundamentally a 2-dimensional query and btrees are > 1-dimensional. There are various hacks you can try if you're willing > to constrain the problem (eg, if you can assume some not-very-large > maximum on the running time of jobs) but in full generality btrees are > just the Wrong Thing. Ignoring the SQL and doing what the author actually wanted, wouldn't a bitmap combination of indexes work here? Or with an index on (start_time, completion_time), start an index scan at start_time = SOMEDATE and only include rows where completion_time < SOMEDATE. Of course if SOMEDATE is near the beginning of the table that wouldn't help. -- Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com Pervasive Software http://pervasive.com 512-569-9461