Berberber
lundi 24 janvier 2005 à 20:39
CITATION(Titag @ vendredi 01 octobre 2004, 11h32)
La syntaxe exacte est : ORDER BY RAND
()cette methode est moyenne
CITATION(http://dev.mysql.com/doc/mysql/en/select.html)
when selecting a single random row you have to use a query like this: SELECT ... FROM my_table ORDER BY RAND() LIMIT 1.
as explain shows, mysql optimizes this VERY badly (or may be better said, doens't optimize it at all): it uses an temporary table and an extra filesort.
couldn't this be optimized?!
la solution suivante est proposée :
CODE
LOCK TABLES foo READ;
SELECT FLOOR(RAND() * COUNT(*)) AS rand_row FROM foo;
SELECT * FROM foo LIMIT $rand_row, 1;
UNLOCK TABLES;
Vous avez une meilleure idée, avant que je change tous mes scripts ?