How can i optimize MySQL's ORDER BY RAND() function?

前端 未结 8 2286
天命终不由人
天命终不由人 2020-11-22 00:58

I\'d like to optimize my queries so I look into mysql-slow.log.

Most of my slow queries contains ORDER BY RAND(). I cannot find a real solu

8条回答
  •  花落未央
    2020-11-22 01:18

    function getRandomRow(){
        $id = rand(0,NUM_OF_ROWS_OR_CLOSE_TO_IT);
        $res = getRowById($id);
        if(!empty($res))
        return $res;
        return getRandomRow();
    }
    
    //rowid is a key on table
    function getRowById($rowid=false){
    
       return db select from table where rowid = $rowid; 
    }
    

提交回复
热议问题