Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 3566B2E32D6 for ; Wed, 28 Nov 2007 10:56:44 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 01226-04 for ; Wed, 28 Nov 2007 10:56:38 -0400 (AST) Received: from mail.emolecules.com (66-226-76-106.dedicated.abac.net [66.226.76.106]) by postgresql.org (Postfix) with ESMTP id C45042E32E0 for ; Wed, 28 Nov 2007 10:56:37 -0400 (AST) Received: (qmail 29170 invoked by uid 2020); 28 Nov 2007 07:10:48 -0800 Received: from wsip-68-224-174-233.sd.sd.cox.net (HELO lisa-james-computer.local) (68.224.174.233) by 192.168.0.4 with SMTP; 28 Nov 2007 07:10:48 -0800 Message-ID: <474D81A1.20301@emolecules.com> Date: Wed, 28 Nov 2007 06:56:33 -0800 From: Craig James User-Agent: Thunderbird 2.0.0.9 (Macintosh/20071031) MIME-Version: 1.0 To: tmp , pgsql-performance@postgresql.org Subject: Re: Query only slow on first run References: <20071127165543.GY11978@crankycanuck.ca> <23999.1196184368@sss.pgh.pa.us> <056501c83128$55d58a90$8e00a8c0@tridecap.com> <20071127233833.GA24031@uio.no> <15029.1196209554@sss.pgh.pa.us> <474CBF98.6070508@amossen.dk> In-Reply-To: <474CBF98.6070508@amossen.dk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/349 X-Sequence-Number: 27764 tmp wrote: >> what exactly is that >> "random_number" column > > A random float that is initialized when the row is created and never > modified afterwards. The physical row ordering will clearly not match > the random_number ordering. However, other queries uses a row ordering > by the primary key so I don't think it would make much sense to make the > index on random_number a clustering index just in order to speed up this > single query. > >> and why are you desirous of ordering by it? > > In order to simulate a random pick of K rows. See [1]. A trick that I used is to sample the random column once, and create a much smaller table of the first N rows, where N is the sample size you want, and use that. If you need a different N samples each time, you can create a temporary table, put your random N rows into that, do an ANALYZE, and then join to this smaller table. The overall performance can be MUCH faster even though you're creating and populating a whole table, than the plan that Postgres comes up with. This seems wrong-headed (why shouldn't Postgres be able to be as efficient on its own?), but it works. Craig