📜  mysql order by rand limit 1 真的很慢 - SQL 代码示例

📅  最后修改于: 2022-03-11 15:05:14.329000             🧑  作者: Mango

代码示例1
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;
}