X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 4EE24D1B459 for ; Wed, 7 Apr 2004 05:38:16 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 82267-01 for ; Wed, 7 Apr 2004 05:38:16 -0300 (ADT) Received: from anchor-post-34.mail.demon.net (anchor-post-34.mail.demon.net [194.217.242.92]) by svr1.postgresql.org (Postfix) with ESMTP id EF093D1C4C4 for ; Wed, 7 Apr 2004 05:38:13 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-34.mail.demon.net with esmtp (Exim 3.35 #1) id 1BB8Zb-000CTT-0Y; Wed, 07 Apr 2004 09:38:15 +0100 Received: by mainbox.archonet.com (Postfix, from userid 529) id 1A17A1643F; Wed, 7 Apr 2004 09:38:14 +0100 (BST) Received: from client17.archonet.com (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 19E3A1643F; Wed, 7 Apr 2004 09:38:12 +0100 (BST) From: Richard Huxton To: Ken Geis , pgsql-performance@postgresql.org Subject: Re: plan problem Date: Wed, 7 Apr 2004 09:38:11 +0100 User-Agent: KMail/1.5 References: <40731252.2030002@speakeasy.org> In-Reply-To: <40731252.2030002@speakeasy.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200404070938.11482.dev@archonet.com> X-Bogosity: No, tests=bogofilter, spamicity=0.000000, version=0.15.3 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/76 X-Sequence-Number: 6376 On Tuesday 06 April 2004 21:25, Ken Geis wrote: > I am trying to find an efficient way to draw a random sample from a > complex query. I also want it to be easy to use within my application. > > So I've defined a view that encapsulates the query. The id in the > "driving" table is exposed, and I run a query like: > > select * from stats_record_view > where id in (select id from driver_stats > order by random() > limit 30000); How about a join? SELECT s.* FROM stats_record_view s JOIN (SELECT id FROM driver_stats ORDER BY random() LIMIT 30000) AS r ON s.id = r.id; Or, what about a cursor and fetch forward (or back?) a random number of rows before each fetch. That's probably not going to be so random though. Also worth checking the various list archives - this has come up in the past, but some time ago. -- Richard Huxton Archonet Ltd