in reply to Select a random record
Then you could pick a random number from 0..$n-1 - call it $rand and using the 2 paramter form of LIMIT select it, egSELECT count(*) FROM table
This should be very quick and efficient. You should ORDER BY the primary key probably for maximum speed (where it says id above).SELECT * FROM table ORDER BY id LIMIT $rand, 1
This method has the advantage that it will work with a WHERE clause on the the SELECTs), eg WHERE setup=1 or something like that
This two parameter LIMIT might be MySQL specific I don't know (it doesn't say it is in the manual though)
Update: I tried this on a large MySQL table (with 100ish columns and 500,000ish rows). Leaving out ORDER BY made only a very small difference. The first SQL statement was instant. The second took 8 seconds varying by $rand so maybe it is doing a full table scan here :-(
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: Select a random record
by elwarren (Priest) on Sep 05, 2000 at 02:48 UTC | |
|
RE: Re: Select a random record
by Maclir (Curate) on Sep 05, 2000 at 00:57 UTC |