From: | "gnari" <gnari(at)simnet(dot)is> |
---|---|
To: | <pgsql-general(at)postgresql(dot)org> |
Cc: | "Kari Lavikka" <tuner(at)bdb(dot)fi> |
Subject: | Re: Selecting a random row |
Date: | 2004-11-04 17:17:59 |
Message-ID: | 002301c4c292$3beb32e0$0100000a@wp2000 |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
From: "Kari Lavikka" <tuner(at)bdb(dot)fi>
>
> Actually I found an answer. If a I wrap the split point selection to
> subquery then the range of results is from 0 to maximum value (~120k in
> this case)
>
> galleria=> SELECT u.uid FROM users u WHERE u.status = 'a' AND uid >=
> (select cast(cast((SELECT uid FROM users WHERE status = 'a' ORDER BY uid
> DESC LIMIT 1) - 1 AS FLOAT) * random() AS INTEGER)) ORDER BY uid ASC LIMIT
1;
> uid
> -------
> 91937
> (1 row)
Tthe problem with this is that this is not very random.
If the uids 30000 to 39999 have been missing, but
the uids are more or less contiguous apart from that,
the uid 40000 would be 10000 times more likely to be selected
than average.
Maybe using an OFFSET of (count(*) * random()) and a LIMIT 1
could be practical.
gnari
From | Date | Subject | |
---|---|---|---|
Next Message | Pierre-Frédéric Caillaud | 2004-11-04 17:25:16 | Re: Restricting Postgres |
Previous Message | Andy | 2004-11-04 17:17:20 | RFD: comp.databases.postgresql.general |