mysql order by rand() 效率优化方法


本文摘自PHP中文网,作者jacklove,侵删。

从一次查询中随机返回一条数据,一般使用mysql的order by rand() 方法来实现

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

1

2

3

4

5

6

7

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 获取记录
代码如下:

1

2

<?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);?>

分析:

1

2

3

4

5

6

7

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() 效率优化方法 ,更多相关内容请关注php中文网。

相关推荐:

解读php的PDO连接数据库的相关内容

讲解PHP面向对象,PHP继承相关代码

在PHP中使用魔术方法__CLASS__来获取类名的相关操作

以上就是mysql order by rand() 效率优化方法的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

mysql怎么去掉表中重复的字段

mysql索引命中规则讲解

mysql 的 join 功能弱爆了?

mysql查询结果顺序按in()中id的顺序排列的实例分析

mysql所支持的数据类型与表字段约束类型的学习教程

mysql 如何合并两个表

navicat如何给mysql还原数据库

mysql 如何获取两个集合的交集差集并集

mysql connectors是什么

mysql myisamchk小工具使用手册

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...