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 D0900529CB for ; Tue, 28 Jun 2005 03:41:10 -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 38441-10 for ; Tue, 28 Jun 2005 06:41:01 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id C1AEE52A21 for ; Tue, 28 Jun 2005 03:40:59 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j5S6euqE011498; Tue, 28 Jun 2005 02:40:57 -0400 (EDT) To: "Karl O. Pinc" Cc: pgsql-performance@postgresql.org Subject: Re: Poor index choice -- multiple indexes of the same columns In-reply-to: <1119913766l.4428l.2l@mofo> References: <1119913766l.4428l.2l@mofo> Comments: In-reply-to "Karl O. Pinc" message dated "Mon, 27 Jun 2005 23:09:26 -0000" Date: Tue, 28 Jun 2005 02:40:56 -0400 Message-ID: <11497.1119940856@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/544 X-Sequence-Number: 13181 "Karl O. Pinc" writes: > 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 don't believe that any existing release can tell the difference between these two indexes as far as costs go. I just recently added some code to btcostestimate that would cause it to prefer the index on (sname, date) but of course that's not released yet. However: isn't the above query pretty seriously underspecified? With a LIMIT and no ORDER BY, you are asking for a random one of the rows matching the condition. I realize that with "select 1" you may not care much, but adding a suitable ORDER BY would help push the planner towards using the right index. In this case "ORDER BY sname DESC, date DESC" would probably do the trick. regards, tom lane