Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 5E9BA9FB348 for ; Thu, 4 Oct 2007 15:33:48 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 36617-07 for ; Thu, 4 Oct 2007 15:33:40 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from newmail.silentmedia.com (earth.silentmedia.com [216.254.19.12]) by postgresql.org (Postfix) with ESMTP id D12A29FB416 for ; Thu, 4 Oct 2007 15:33:41 -0300 (ADT) Received: by newmail.silentmedia.com (Postfix, from userid 99) id E4DEF35E3C5; Thu, 4 Oct 2007 11:33:39 -0700 (PDT) Received: from GRD (GRD [10.125.42.6]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by newmail.silentmedia.com (Postfix) with ESMTP id A04CE35E3BF; Thu, 4 Oct 2007 11:33:39 -0700 (PDT) Date: Thu, 4 Oct 2007 11:33:39 -0700 (PDT) From: Ben X-X-Sender: bench@localhost.localdomain To: Bill Moran cc: pgsql-performance@postgresql.org Subject: Re: quickly getting the top N rows In-Reply-To: <20071004141411.9509fe66.wmoran@collaborativefusion.com> Message-ID: References: <20071004141411.9509fe66.wmoran@collaborativefusion.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/39 X-Sequence-Number: 27061 On Thu, 4 Oct 2007, Bill Moran wrote: > However, 2 guesses: > 1) You never analyzed the table, thus PG has awful statistics and > doesn't know how to pick a good plan. > 2) You have so few rows in the table that a seq scan is actually > faster than an index scan, which is why PG uses it instead. No, the tables are recently analyzed and there are a couple hundred thousand rows in there. But I think I just figured it out.... it's a 3-column index, and two columns of that index are the same for every row. When I drop those two columns from the ordering restriction, the index gets used and things speed up 5 orders of magnitude. Maybe the planner is smart enough to think that if a column in the order by clause is identical for most rows, then using an index won't help.... but not smart enough to realize that if said column is at the *end* of the order by arguments, after columns which do sort quite well, then it should use an index after all.