Is it true that ORDER BY rand()
performance is very slow compared to other solutions? If yes, what are better ways to select random row(s) from the database?
My query:
SELECT sName FROM bpoint WHERE placeID=? ORDER BY rand() LIMIT 1;
Yes, ORDER BY RAND()
can be very slow in larger result-sets.
An option is to fetch resultset with this statement (into an array):
SELECT sName FROM bpoint WHERE placeID=?;
After that - use array_rand($resultset)
to get a randomized item from the $resultset
query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With