Re: plan problem

From: Richard Huxton <dev(at)archonet(dot)com>
To: Ken Geis <kgeis(at)speakeasy(dot)org>, pgsql-performance(at)postgresql(dot)org
Subject: Re: plan problem
Date: 2004-04-07 08:38:11
Message-ID: 200404070938.11482.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

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

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message huang yaqin 2004-04-07 08:56:56 Re: good pc but bad performance,why?
Previous Message Richard Huxton 2004-04-07 08:33:12 Re: good pc but bad performance,why?