Vijayanand Premnath Vijayanand Premnath - 6 months ago 13
PHP Question

Is there a better way to get Random record with limit 1

Is there a better way to get a random record with limit 1

I am using the below Query

$r = mysql_query("SELECT username FROM user ORDER BY RAND() LIMIT 1");


but this creates a terrible bottleneck to me (my system slows down very much almost it gets hanged). Is there a better option to do this.

Answer

This should do this work:

SELECT username FROM user where ID =
  (SELECT ID FROM user ORDER BY RAND() LIMIT 1)