加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql的order by rand方法实际使用

发布时间:2023-09-01 15:26:19 所属栏目:MySql教程 来源:
导读:从一次查询中随机返回一条数据,一般使用mysql的order by rand() 方法来实现

例如: 从20万用户中随机抽取1个用户

mysql> select * from user order by rand() limit 1;

+-------+------------+---------
从一次查询中随机返回一条数据,一般使用mysql的order by rand() 方法来实现
 
例如: 从20万用户中随机抽取1个用户
 
mysql> select * from user order by rand() limit 1;
 
+-------+------------+----------------------------------+----------+--------------+-----------+| id    | phone      | password                         | salt     | country_code | ip        |
 
+-------+------------+----------------------------------+----------+--------------+-----------+| 15160 | 6549721306 | e4f302120c006880a247b652ad0e42f2 | 40343586 | 86           | 127.0.0.1 |
 
+-------+------------+----------------------------------+----------+--------------+-----------+1 row in set (0.25 sec)mysql> explain select * from user order by rand() limit 1;
 
+----+-------------+-------+------+---------------+------+---------+------+--------+---------------------------------+| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows   | Extra                           |
 
+----+-------------+-------+------+---------------+------+---------+------+--------+---------------------------------+|  1 | SIMPLE      | user  | ALL  | NULL          | NULL | NULL    | NULL | 200303 | Using temporary; Using filesort |
 
+----+-------------+-------+------+---------------+------+---------+------+--------+---------------------------------+1 row in set (0.00 sec)
 
根据分析结果,运行需要0.25秒,order by rand() 需要使用临时表(Using temporary),需要使用文件排序(Using filesort),效率低下。
 
改进方法
 
mysql的order by rand方法实际运用
 
1.首先获取查询的总记录条数total
 
2.在总记录条数中随机偏移N条(N=0~total-1)
 
3.使用limit N,1 获取记录
 
代码如下:
 
<?php// 获取总记录数$sqlstr = 'select count(*) as recount from user';$query = mysql_query($sqlstr) or die(mysql_error());$stat = mysql_fetch_assoc($query);$total = $stat['recount'];// 随机偏移$offset = mt_rand(0, $total-1);// 偏移后查询$sqlstr = 'select * from user limit '.$offset.',1';$query = mysql_query($sqlstr) or die(mysql_error());$result = mysql_fetch_assoc($query);
 
print_r($result);?>
 
分析:
 
mysql> select * from user limit 23541,1;
 
+-------+------------+----------------------------------+----------+--------------+-----------+| id    | phone      | password                         | salt     | country_code | ip        |
 
+-------+------------+----------------------------------+----------+--------------+-----------+| 23542 | 3740507464 | c8bc1890de179538d8a49cc211859a46 | 93863419 | 86           | 127.0.0.1 |
 
+-------+------------+----------------------------------+----------+--------------+-----------+1 row in set (0.01 sec)mysql> explain select * from user limit 23541,1;
 
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows   | Extra |
 
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+|  1 | SIMPLE      | user  | ALL  | NULL          | NULL | NULL    | NULL | 200303 | NULL  |
 
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+1 row in set (0.00 sec)
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章