Quote official website explanation
1. Databases other than MySQL are not supported;
2. The data volume is tens of thousands, which is extremely slow.
Therefore, unless special circumstances exist, zblog blog will no longer allow applications with code containing rand() directly in the database to pass the audit in the future. Of course, it does not include rand() or mt_rand() functions in PHP, nor Math.random().
Why is it slow? I'm trying to use non computer terms to explain.
We compare a book to a database. General database queries, such as looking up a record with ID 1, usually get results directly based on the index. The use of an index is equivalent to the content of a book. If you look up the directory directly to get results, it will naturally be faster than turning from page to page. However, what about ORDER BY RAND()? It will first write a random number for each page of the book, which will turn over each page. Then, compare the order with the size, and sort the numbers on each page, which will turn the book over again. Then take the records with the lowest number. So that's why it's slow. (Of course, I haven't seen the internal implementation of MySQL. These two tasks are not necessarily independent of each other.) For professional explanations, please refer to the official documentation of MySQL:
You cannot use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times. However, you can retrieve rows in random order like this:
mysql> SELECT * FROM tbl_name ORDER BY RAND(); ORDER BY RAND() combined with LIMIT is useful for selecting a random sample from a set of rows:
mysql> SELECT * FROM table1, table2 WHERE a=b AND c<d -> ORDER BY RAND() LIMIT 1000; RAND() is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.
Bird random display code
The code is as follows
Function Template ID_Theme_Get_Rand(){ global $zbp,$str,$order; $str=''; $sql = $zbp->db->sql->Select( $zbp->table['Post'], array("MIN(log_ID)","MAX(log_ID)"), array( array('=', 'log_Type', '0'), array('=', 'log_Status', '0'), ), array('log_PostTime' => 'ASC'), null, null ); $array = $zbp->db->Query($sql); $i=mt_rand($array[0]["MIN(log_ID)"],$array[0]["MAX(log_ID)"]); $order = ''; $where = array( array('=','log_Status','0'), array('>','log_ID',$i) ); $array = $zbp->GetArticleList(array('*'),$where,$order,array(5),''); foreach ($array as $key=>$article) { if($key>5){ break; } $str .='< li><a href="'.$article->Url.'">'.$ article->Title.'</ a></li>'; } return $str; }
Random principle of flying birds
The code is as follows
Function template ID_rand ($num){ global $zbp,$str,$order;$ i; $i = $num; $str = ''; $arr = array(); $arand = array(); $order = array('log_ViewNums'=>'ASC'); $where = array(array('=','log_Status','0')); $array = $zbp->GetArticleList(array('*'),$where,$order,array(20),'');$ arr = array_rand($array,$i); for($j=0;$j<$i;$j++){$arand[]=$array[$arr[$j]];} foreach ($arand as $related) { $str .= "<li><a href=\"{$related->Url}\" title=\"{$related->Title}\" target=\"_blank\">{$related->Title}</a></li>"; } return $str; }