Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select random row - rand() performance [duplicate]

Tags:

php

mysql

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; 
like image 935
Biker John Avatar asked Apr 29 '13 22:04

Biker John


1 Answers

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.

like image 114
bestprogrammerintheworld Avatar answered Oct 31 '22 06:10

bestprogrammerintheworld