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 E1214528BB for ; Mon, 27 Jun 2005 19:20:31 -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 20202-01 for ; Mon, 27 Jun 2005 22:20:22 +0000 (GMT) Received: from mail28.sea5.speakeasy.net (mail28.sea5.speakeasy.net [69.17.117.30]) by svr1.postgresql.org (Postfix) with ESMTP id A93255286D for ; Mon, 27 Jun 2005 19:20:20 -0300 (ADT) Received: (qmail 26064 invoked from network); 27 Jun 2005 22:20:23 -0000 Received: from dsl093-114-095.chi2.dsl.speakeasy.net (HELO mofo.meme.com) ([66.93.114.95]) (envelope-sender ) by mail28.sea5.speakeasy.net (qmail-ldap-1.03) with SMTP for ; 27 Jun 2005 22:20:23 -0000 Received: from mofo (localhost.localdomain [127.0.0.1]) by mofo.meme.com (Postfix) with ESMTP id 96ACB6E421 for ; Mon, 27 Jun 2005 18:09:26 -0500 (CDT) Date: Mon, 27 Jun 2005 23:09:26 +0000 From: "Karl O. Pinc" Subject: Poor index choice -- multiple indexes of the same columns To: pgsql-performance@postgresql.org X-Mailer: Balsa 2.3.0 Message-Id: <1119913766l.4428l.2l@mofo> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; DelSp=Yes; Format=Flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200506/504 X-Sequence-Number: 13141 Postgresql 8.0.3 Hi, I have a query select 1 from census where date < '1975-9-21' and sname = 'RAD' and status != 'A' limit 1; Explain analyze says it always uses the index made by: CREATE INDEX census_date_sname ON census (date, sname); this is even after I made the index: CREATE INDEX census_sname_date ON census (sname, date); I made census_sname_date because it ran too slow. By deleting census_date_sname (temporarly, because my apps don't like this) I can force the use of census_sname_date and the query runs fine. Seems to me that when there's a constant value in the query and an = comparision it will always be faster to use the (b-tree) index that's ordered first by the constant value, as then all further blocks are guarenteed to have a higher relevant information density. At least when compared with another index that has the same columns in it. As you might imagine there are relatively few sname values and relatively many date values in my data. I use a query like the above in a trigger to enforce bounds limitations. I don't expect (want) to find any rows returned. I've figured out how to avoid executing this code very often, so this is not presently a serious problem for me. Karl Free Software: "You don't pay back, you pay forward." -- Robert A. Heinlein