![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
\__/| ( oo ) Kari Lavikka - tuner (AT) bdb (DOT) fi |
#2
| |||
| |||
|
|
Is there any explanation for this strange behavior or are there better ways to select a random row? |
#3
| |||
| |||
|
|
\__/| ( oo ) Kari Lavikka - tuner (AT) bdb (DOT) fi |
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 04 November 2004 12:36, Kari Lavikka wrote: Is there any explanation for this strange behavior or are there better ways to select a random row? How about SELECT ...whatever... ORDER BY random() LIMIT 1; Mit freundlichem Gruß / With kind regards Holger Klawitter - -- lists <at> klawitter <dot> de -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFBiibF1Xdt0HKSwgYRAlJXAJ4nUpDfKBKCigPVMt8WpK G4gZmt4wCcD/ZC KHBlBl1+5FZ4pgqkZlyzWQA= =MrrE -----END PGP SIGNATURE----- ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org |
#4
| |||
| |||
|
|
Works but is too slooow. Shuffling whole table and selecting the first row is not the way to go in this case. Limit (cost=5340.74..5340.74 rows=1 width=4) -> Sort (cost=5340.74..5440.70 rows=39986 width=4) Sort Key: random() -> Seq Scan on users (cost=0.00..2284.37 rows=39986 width=4) Filter: (status = 'a'::bpchar) |\__/| ( oo ) Kari Lavikka - tuner (AT) bdb (DOT) fi __ooO( )Ooo_______ _____ ___ _ _ _ _ _ _ _ "" On Thu, 4 Nov 2004, Holger Klawitter wrote: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 04 November 2004 12:36, Kari Lavikka wrote: Is there any explanation for this strange behavior or are there better ways to select a random row? How about SELECT ...whatever... ORDER BY random() LIMIT 1; Mit freundlichem Gruß / With kind regards Holger Klawitter - -- lists <at> klawitter <dot> de -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFBiibF1Xdt0HKSwgYRAlJXAJ4nUpDfKBKCigPVMt8WpK G4gZmt4wCcD/ZC KHBlBl1+5FZ4pgqkZlyzWQA= =MrrE -----END PGP SIGNATURE----- ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org |
#5
| |||
| |||
|
|
= 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; |

|
\__/| ( oo ) Kari Lavikka - tuner (AT) bdb (DOT) fi - (050) 380 3808 |
#6
| |||
| |||
|
|
-- -- Choose a random point between 0 and max_uid and select the first -- value from the bigger part -- CREATE OR REPLACE FUNCTION random_uid() RETURNS int4 AS 'SELECT uid FROM users u WHERE u.status = ''a'' AND uid >= cast(cast(max_uid() - 1 AS FLOAT) * random() AS INTEGER) ORDER BY uid ASC LIMIT 1' LANGUAGE 'sql'; |
#7
| |||
| |||
|
|
-- -- Choose a random point between 0 and max_uid and select the first -- value from the bigger part -- CREATE OR REPLACE FUNCTION random_uid() RETURNS int4 AS 'SELECT uid FROM users u WHERE u.status = ''a'' AND uid >= cast(cast(max_uid() - 1 AS FLOAT) * random() AS INTEGER) ORDER BY uid ASC LIMIT 1' LANGUAGE 'sql'; |
#8
| |||
| |||
|
|
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) |
#9
| |||
| |||
|
|
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. |
#10
| |||
| |||
|
|
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. |
|
\__/| ( oo ) Kari Lavikka - tuner (AT) bdb (DOT) fi |
![]() |
| Thread Tools | |
| Display Modes | |
| |