X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id E13489FA38E for ; Wed, 10 May 2006 13:46:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 27938-05 for ; Wed, 10 May 2006 13:45:59 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.logix-tt.com (unknown [212.211.145.186]) by postgresql.org (Postfix) with ESMTP id A2CA69FA293 for ; Wed, 10 May 2006 13:45:59 -0300 (ADT) Received: from kingfisher.intern.logix-tt.com (p549D13DA.dip0.t-ipconnect.de [84.157.19.218]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.logix-tt.com (Postfix) with ESMTP id A772665837; Wed, 10 May 2006 18:47:10 +0200 (CEST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by kingfisher.intern.logix-tt.com (Postfix) with ESMTP id CA85E181520E4; Wed, 10 May 2006 18:46:10 +0200 (CEST) Message-ID: <446218D2.8030002@logix-tt.com> Date: Wed, 10 May 2006 18:46:10 +0200 From: Markus Schaber Organization: Logical Tracking and Tracing International AG, Switzerland User-Agent: Debian Thunderbird 1.0.7 (X11/20051017) X-Accept-Language: en-us, en MIME-Version: 1.0 To: ogjunk-pgjedan@yahoo.com Cc: pgsql-sql@postgresql.org Subject: Re: Multi-column index not used, new flipped column index is References: <20060510162143.96628.qmail@web50306.mail.yahoo.com> In-Reply-To: <20060510162143.96628.qmail@web50306.mail.yahoo.com> X-Enigmail-Version: 0.93.0.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/87 X-Sequence-Number: 25118 Hi, Otis, ogjunk-pgjedan@yahoo.com wrote: > I'm using PG 8.0.3 and recently spotted a query that was not using a > multi-column index I had created. The index looks like: > > CREATE INDEX ..... ON FooTable(fkColumnOne, fkColumnTwo); > > The query that was not using the index was using: > > SELECT .... a bunch of columns and joins .... WHERE > FooTable.fkColumnTwo=1000; That's how it is supposed to be currently. AFAIK, Oracle has a technique called "index skip scan" that could take (limited) use of that second index, but PostgreSQL currently does not support them. > So I added another index where the indexed columns are flipped: > > CREATE INDEX ..... ON FooTable(fkColumnTwo, fkColumnOne); > > Now the query started to use that index -- gooooood! But now I have 2 > indices that are nearly the same, and that means overhead during > INSERTs/DELETEs. :( > Is there a way to get this to use that first index, so I don't have > to have this additional index? If I had PG 8.1.3, would PG know how > to use that first index? I seem to recall something about this > somewhere... but I don't see it at > http://www.postgresql.org/docs/whatsnew . Bitmap Index Scans can be your solution, but AFAIK they were invented in 8.1. For bitmap index scans, you have one index on fkColumnOne and one on fkColumnTwo, and the query planner knows to combine them when both columns are given in a WHERE clause. HTH, Markus -- Markus Schaber | Logical Tracking&Tracing International AG Dipl. Inf. | Software Development GIS Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org