Answers for "mysql order by rand limit 1 really slow"

SQL
0

mysql order by rand limit 1 really slow

function get_random_row($mytable,$conn){
	$table_max_id = $conn->query("SELECT id FROM $mytable ORDER BY id DESC LIMIT 0, 1")[0]["id"];
	$rand_id=rand(1,$table_max_id);
	$random_result = $conn->query("select * from $mytable where id = $rand_id");
  	
	#note: if you are missing some id's, we can call again until we get hit
  	if(!$random_result){
    	return get_random_row($mytable,$conn);
    }
  
  	return $random_result;
}
Posted by: Guest on January-28-2021

Code answers related to "SQL"

Browse Popular Code Answers by Language