Re: select where id=random()*something returns two results

From: Jean-Luc Lachance <jllachan(at)nsd(dot)ca>
To: Rod Taylor <rbt(at)rbt(dot)ca>
Cc: Ulrich Meis <u(dot)meis(at)gmx(dot)de>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: select where id=random()*something returns two results
Date: 2003-09-19 14:43:52
Message-ID: 3F6B1628.9DA483BF@nsd.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Rod,

If the table has 100,000 tupples your query is generating 100,000 new
tupples...
Try:

select * from quotes where id = (
select int8( 1 + random() * (
select id from quotes order by id desc limit 1)));

JLL

Rod Taylor wrote:
>
> > select * from quotes where id=1+round(random()* cast ((select max(id)
> > from quotes) as double precision));
> > id | quote |
> > author
> > -----+-----------------------------------------------------------+------
> > -----------
> > 187 | Vergib Deinen Feinden, aber vergiss niemals ihre Namen. | John
> > F. Kennedy
> > 377 | Die Wirklichkeit ist nicht so oder so, sondern so und so. | Harry
> > Mulisch
> > (2 rows)
> >
> > I'm not really into databases, but this sounds wrong. Most of the time,
> > I actually get 0 results.
>
> Random is calculated per call (in this case per comparison). So, the
> value you compare against for 187 is not the same as 377.
>
> UPDATE table SET column = random(); will show the effect.
>
> If you wrap randon() in a subselect, it will cause it to be evaluated
> once:
>
> SELECT * from quotes where id = 1+round((SELECT random()) * cast(....).
>
> However, a much faster query for your purposes would be:
>
> SELECT * FROM quotes ORDER BY random() LIMIT 1;
>
> ------------------------------------------------------------------------
> Name: signature.asc
> signature.asc Type: application/pgp-signature
> Description: This is a digitally signed message part

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Stephan Szabo 2003-09-19 15:32:41 Re: Can't access table to describe, drop, or select, but it
Previous Message Hiroshi Saito 2003-09-19 14:13:26 The comment sentence of Primary-Key is lost.