Re: selecting random rows - Mailing list pgsql-general

From Dennis Gearon
Subject Re: selecting random rows
Date
Msg-id 3F611469.5070606@fireserve.net
Whole thread Raw
In response to selecting random rows  (Joseph Shraibman <jks@selectacast.net>)
Responses Re: selecting random rows
List pgsql-general
If you have a nice small Primary key on the table, you can so something
like this:

SELECT field_list
FROM table
WHERE primary_key IN(
     SELECT primary_key,
     FROM table
     ORDER by RANDOM()
     LIMIT your_limit);

This may not be the exact sequence, and there is some workarounds for
some slowness in the IN() keyword. Others may comment at will, and
polish this up?

Joseph Shraibman wrote:

> Is there a way to get random rows besides ORDER BY random()?  The
> problem with ORDER BY random() is that is has to get all the rows from
> the table before the results are returned.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if
> your
>      joining column's datatypes do not match
>


pgsql-general by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: selecting random rows
Next
From: Joseph Shraibman
Date:
Subject: Re: selecting random rows