Received: from fw.mccons.net (adsl-65-64-105-41.dsl.kscymo.swbell.net [65.64.105.41]) by postgresql.org (8.11.3/8.11.4) with ESMTP id g1EFFiE81035 for ; Thu, 14 Feb 2002 10:15:45 -0500 (EST) (envelope-from bmccane@mccons.net) Received: (from root@localhost) by fw.mccons.net (8.11.3/8.11.4) id g1EFFl440548 for pgsql-admin@postgresql.org; Thu, 14 Feb 2002 09:15:47 -0600 (CST) (envelope-from bmccane@mccons.net) Received: from localhost (localhost [127.0.0.1]) by fw.mccons.net (8.11.3/8.11.4av) with ESMTP id g1EFFjx40537 for ; Thu, 14 Feb 2002 09:15:45 -0600 (CST) (envelope-from bmccane@mccons.net) Date: Thu, 14 Feb 2002 09:15:45 -0600 (CST) From: Brian McCane To: pg-admin Subject: Re: Useless index In-Reply-To: <8617.1013699169@sss.pgh.pa.us> Message-ID: <20020214091349.A33261-100000@fw.mccons.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS perl-11 X-Archive-Number: 200202/175 X-Sequence-Number: 3332 On Thu, 14 Feb 2002, Tom Lane wrote: > Brian McCane writes: > > CREATE INDEX foo_index ON foo (bazid, score desc) ; > > > Which would be exactly what I want, and would complete in a split second. > > Instead, this thing runs FOREVER (okay, it just seems that way to my > > client :). Is there any way to get the equivalent index from PostgreSQL? > > You don't need a funny index, you just need to get the planner to notice > that that index can serve to create the desired output ordering. Try > > create table foo(bazid int, score int); > CREATE INDEX foo_index ON foo (bazid, score) ; > > explain select * from foo where bazid = 123456 > order by bazid desc, score desc limit 100 ; > > NOTICE: QUERY PLAN: > > Limit (cost=0.00..17.07 rows=5 width=8) > -> Index Scan Backward using foo_index on foo (cost=0.00..17.07 rows=5 width=8) > > EXPLAIN > > > regards, tom lane > Thanks, That did it. I tried the explain with "score desc, bazid desc" and it looked the same to me. I had myself convinced it had to be that way. Then I tried your way (after I realized I had already selected a specific bazid so it didn't matter if it was descending :), and it was all okay. -brian Wm. Brian McCane | Life is full of doors that won't open Search http://recall.maxbaud.net/ | when you knock, equally spaced amid those Usenet http://freenews.maxbaud.net/ | that open when you don't want them to. Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"