douzi4724 2009-02-17 21:06
浏览 44
已采纳

从MySQL中选择可变数量的随机记录

I want to show a random record from the database. I would like to be able to show X number of random records if I choose. Therefore I need to select the top X records from a randomly selected list of IDs

(There will never be more than 500 records involved to choose from, unless the earth dramatically increases in size. Currently there are 66 possibles.)

This function works, but how can I make it better?

/***************************************************/
/* RandomSite */
//****************/
//  Returns an array of random site IDs or NULL
/***************************************************/   
function RandomSite($intNumberofSites = 1) {
    $arrOutput = NULL;
    //open the database
    GetDatabaseConnection('dev');

    //inefficient
    //$strSQL = "SELECT id FROM site_info WHERE major <> 0 ORDER BY RAND() LIMIT ".$intNumberofSites.";";

    //Not wonderfully random
    //$strSQL = "SELECT id FROM site_info WHERE major <> 0 AND id >= (SELECT FLOOR( COUNT(*) * RAND()) FROM site_info ) ORDER BY id LIMIT ".$intNumberofSites.";";

    //Manual selection from available pool of candidates  ?? Can I do this better ??
    $strSQL = "SELECT id FROM site_info WHERE major <> 0;";

    if (is_numeric($intNumberofSites))
    {
        //excute my query
        $result = @mysql_query($strSQL);
        $i=-1;

        //create an array I can work with  ?? Can I do this better ??
        while ($row = mysql_fetch_array($result, MYSQL_NUM))
        {
            $arrResult[$i++] = $row[0];
        }

        //mix them up
        shuffle($arrResult);

        //take the first X number of results  ?? Can I do this better ??
        for ($i=0;$i<$intNumberofSites;$i++)
        {
            $arrOutput[$i] = $arrResult[$i];
        }
    }   

    return $arrOutput;
    }

UPDATE QUESTION: I know about the ORDER BY RAND(), I just don't want to use it because there are rumors it isn't the best at scaling and performance. I am being overly critical of my code. What I have works, ORDER BY RAND() works, but can I make it better?

MORE UPDATE There are holes in the IDs. There is not a ton of churn, but any churn that happens needs to be approved by our team, and therefore could handled to dump any caching.

Thanks for the replies!

  • 写回答

8条回答 默认 最新

  • dpa55065 2009-02-17 21:14
    关注

    Why not use the Rand Function in an orderby in your database query? Then you don't have to get into randomizing etc in code...

    Something like (I don't know if this is legal)

    Select *
    from site_info
    Order by Rand()
    LIMIT N
    

    where N is the number of records you want...

    EDIT
    Have you profiled your code vs. the query solution? I think you're just pre-optimizing here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端