I would like to select a random line in my database. I saw this solution on a website:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
This SQL query run but someone said me that it was a non performant query. Is there another solution ?
Thx
I would like to select a random line in my database. I saw this solution on a website:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
This SQL query run but someone said me that it was a non performant query. Is there another solution ?
Thx
It is. You have to count rows number with
SELECT COUNT(*) FROM `table`;
After this with php function mt_rand() get the random from 1 to $count and get it with query:
SELECT `column` FROM `table` LIMIT $rand, 1
There's a lot more discussion of this subject, including performance implications and strategies for different DBMSs, in this question.