旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

mysql order by rand() 效力优化要领【MySQL教程】,mysql,order-by

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:38评论:0


导读:从一次查询中随机返回一条数据,平常运用mysql的orderbyrand()要领来完成比方:从20万用户中随机抽取1个用户mysql>select...
从一次查询中随机返回一条数据,平常运用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),效力低下。

革新要领

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)

本篇引见了mysql order by rand() 效力优化要领 ,更多相干内容请关注ki4网。

相干引荐:

解读php的PDO衔接数据库的相干内容

解说PHP面向对象,PHP继续相干代码

在PHP中运用把戏要领__CLASS__来猎取类名的相干操纵

以上就是mysql order by rand() 效力优化要领的细致内容,更多请关注ki4网别的相干文章!

标签:mysqlorder-by


欢迎 发表评论: