pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Ken Geis <kgeis@speakeasy.org>
To: pgsql-performance@postgresql.org
Subject: Re: plan problem
Date: Wed, 7 Apr 2004 09:38:11 +0100
Message-ID: <200404070938.11482.dev@archonet.com> (raw)
In-Reply-To: <40731252.2030002@speakeasy.org>
References: <40731252.2030002@speakeasy.org>

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



view thread (5+ messages)  latest in thread

Message-ID: <200404070938.11482.dev@archonet.com>
Permalink:  ../200404070938.11482.dev@archonet.com/
Also on:    postgresql.org/message-id/200404070938.11482.dev@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: dev@archonet.com, kgeis@speakeasy.org
  Subject: Re: plan problem
  In-Reply-To: <200404070938.11482.dev@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox