Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 68EB19FB6CB for ; Thu, 4 Oct 2007 17:33:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 68437-10 for ; Thu, 4 Oct 2007 17:32:57 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.170]) by postgresql.org (Postfix) with ESMTP id D3C949FB757 for ; Thu, 4 Oct 2007 17:32:59 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id j3so442516ugf for ; Thu, 04 Oct 2007 13:32:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=J9D0L8l8D9jPawpX+lBcnWB6x2H8z3fx3dFSQpSvGwI=; b=cTfIaUVa5eYEUu7XkzBimfYhl1G7I2WwzLQqPH0cGWX4MGRKcbWU0JqN/UujE8gAP8gybtLyQm7bE+ZFX9IDdLzoIwNI0O8xlxvOEgj7DqZ0laGQdcRXQD8LPZILLsDMq+tVRP3BVAOHCv+onW0xnc2VaiBcsvvhXtUSvpMsl2A= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=NMvbdxS7wtqY8Ti0blheMA3muYm/Kl222cAEIexqGy23MuVqDfqsqQGJ0ULDkIZA5WtwCmyFejN1yqf8ZImbcGubRSYpi02hwO2nxKoqDTe7uWO+jNUZpcTTIGW8GmFbLFjDei/sr/OqDo3X5dnwB/Q24hNhuEMjDLQFL0ktQBI= Received: by 10.67.16.5 with SMTP id t5mr2435037ugi.1191529975868; Thu, 04 Oct 2007 13:32:55 -0700 (PDT) Received: by 10.66.220.7 with HTTP; Thu, 4 Oct 2007 13:32:55 -0700 (PDT) Message-ID: Date: Thu, 4 Oct 2007 15:32:55 -0500 From: "Scott Marlowe" To: Ben Subject: Re: quickly getting the top N rows Cc: pgsql-performance@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/45 X-Sequence-Number: 27067 On 10/4/07, Ben wrote: > If I have this: > > create table foo (bar int primary key); > > ...then in my ideal world, Postgres would be able to use that index on bar > to help me with this: > > select bar from foo order by bar desc limit 20; > > But in my experience, PG8.2 is doing a full table scan on foo, then > sorting it, then doing the limit. I have a more complex primary key, but I > was hoping the same concept would still apply. Am I doing something wrong, > or just expecting something that doesn't exist? pg uses an intelligent planner. It looks at the table, the number of rows, the distribution of values, and makes a decision whether to use seq scan or index. Do you have any evidence that in your case seq scan is a bad choice? try this experiment: psql mydb =# select * from foo; -- this will prime the db and put the table in memory if it will fit =# \timing =# set enable_seqscan=off; =# select bar from foo order by bar desc limit 20; =# set enable_seqscan=on; =# select bar from foo order by bar desc limit 20; and compare the times each takes. run each way a few times to be sure you're not getting random variance. On my reporting db with somewhere around 75 million tables, a similar query 0.894 mS and uses an index scan. Which is good, because a sequential scan on that table takes about 15 to 30 minutes.