mysql的order by rand方法实际运用
发表于:2024-09-25 作者:千家信息网编辑
千家信息网最后更新 2024年09月25日,下文内容主要给大家带来mysql的order by rand方法实际运用,这里所讲到的知识,与书籍略有不同,都是专业技术人员在与用户接触过程中,总结出来的,具有一定的经验分享价值,希望给广大读者带来帮
千家信息网最后更新 2024年09月25日mysql的order by rand方法实际运用
下文内容主要给大家带来mysql的order by rand方法实际运用,这里所讲到的知识,与书籍略有不同,都是专业技术人员在与用户接触过程中,总结出来的,具有一定的经验分享价值,希望给广大读者带来帮助。
从一次查询中随机返回一条数据,一般使用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 获取记录
代码如下:
分析:
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的PDO连接数据库的相关内容
讲解PHP面向对象,PHP继承相关代码
在PHP中使用魔术方法__CLASS__来获取类名的相关操作
以上就是mysql的order by rand方法实际运用详细内容,更多请关注其它相关文章!
方法
内容
用户
实际
代码
效率
数据
更多
分析
查询
不同
低下
下文
专业
书籍
人员
价值
对象
就是
技术
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
惠普服务器怎么换电池
计算机应用与网络技术
金小六软件开发
东北网络安全设施
合肥正规网络技术市场报价
杨浦区网络技术服务模式
永济无线网络技术
WPS怎么建一个数据库表格
进入同一台服务器的不同端口
sql数据库模糊查询
数据库安全培训ppt
股票直播软件开发 公司
游戏平台服务器维护中
命令看服务器所有信息
软件开发项目合作协议范本
数据库除了建立表还能建立什么
电脑必须使用代理服务器才能上网
c 里的数据库文件夹里
数据库ai
电信域名服务器地址
融业网络技术
丹东市网络安全
遴选数据库
网络安全中国高校排名
北京爱彼罗网络技术有限公司
win10嵌入式软件开发
爱尔兰 软件开发 薪水
关于网络安全宣传周标语
数据库ai
中学生网络安全班会图片