[mysql] 随机查询 效率比较
select
primary_count as primaryCount,
primary_score as primaryScore,
junior_count as juniorCount,
junior_score as juniorScore,
senior_count as seniorCount,
senoir_score as senoirScore,
total_score as totalScore,
pass_score as passScore
from pd_paper p
where p.is_valid = ''
order by RAND() limit 1 分析:
ORDER BY从句里面不能使用RAND()函数,因为这样会导致数据列被多次扫描。
测试发现这样效率非常低。一个15万余条的库,查询5条数据,要8秒以上。 You cannot use a column with RAND() values in an ORDER BY clause, because ORDER BY
would evaluate the column multiple times. 更高效的做法:查询max(id) * rand()来随机获取数据。 SELECT *
FROM `table` AS t1
JOIN (
SELECT ROUND(RAND()
* (SELECT MAX(id) FROM `table`)) AS id
) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id ASC LIMIT 5; 但是这样会产生连续的5条记录。解决办法只能是每次查询一条,查询5次。
即便如此也值得,因为15万条的表,查询只需要0.01秒不到。
采用join 语法 可以实现真正的随机。 SELECT *
FROM `table`
WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` )
ORDER BY id LIMIT 1; 把语句完善一下,加上MIN(id)的判断。
如果没有加上MIN(id)的判断,结果有一半的时间总是查询到表中的前面几行。
完整的语句: ① where 子句 SELECT * FROM `table`
WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `table`)-
(SELECT MIN(id) FROM `table`)) + (SELECT MIN(id) FROM `table`)))
ORDER BY id LIMIT 1; ② join SELECT *
FROM `table` AS t1
JOIN (
SELECT ROUND(RAND()
* ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))
+(SELECT MIN(id) FROM `table`)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id LIMIT 1; #随机查询一套考卷定义
SELECT
p.primary_count as primaryCount,
p.primary_score as primaryScore,
p.junior_count as juniorCount,
p.junior_score as juniorScore,
p.senior_count as seniorCount,
p.senoir_score as senoirScore,
p.total_score as totalScore,
p.pass_score as passScore FROM
pd_paper AS p
JOIN
(
SELECT ROUND(
RAND()
*((SELECT MAX(id) FROM pd_paper)-(SELECT MIN(id) FROM pd_paper))
+(SELECT MIN(id) FROM pd_paper)
) AS id
)
AS p2
WHERE p.id >= p2.id
ORDER BY p.id LIMIT 1; 最后在程序对这两个语句进行分别查询10次,
前者花费时间 0.147433 秒
后者花费时间 0.015130 秒
看来采用JOIN的语法比直接在WHERE中使用函数效率还要高很多。 附加一个复杂sql:按题目类型(三种)随机查询全部考题信息 SELECT * from (
select
p.id as id,
p.title as title,
p.question as question,
p.answer as answer,
p.crt_time as crtTime
from
pd_problem p
join
(SELECT ROUND(RAND()
* ((SELECT MAX(pp.id) FROM pd_problem pp)-(SELECT MIN(pp.id) FROM pd_problem pp))
+(SELECT MIN(pp.id) FROM pd_problem pp)) AS pid) AS p2
where p.id >= p2.pid
and p.is_valid = ''
and p.paper_type = ''
and p.paper_class = ''
order by p.id limit 5
) as t1
union all
SELECT * from (
select
p.id as id,
p.title as title,
p.question as question,
p.answer as answer,
p.crt_time as crtTime
from
pd_problem p
join
(SELECT ROUND(RAND()
* ((SELECT MAX(pp.id) FROM pd_problem pp)-(SELECT MIN(pp.id) FROM pd_problem pp))
+(SELECT MIN(pp.id) FROM pd_problem pp)) AS pid) AS p2
where p.id >= p2.pid
and p.is_valid = ''
and p.paper_type = ''
and p.paper_class = ''
order by p.id limit 5
) as t2
union all
SELECT * from (
select
p.id as id,
p.title as title,
p.question as question,
p.answer as answer,
p.crt_time as crtTime
from
pd_problem p
join
(SELECT ROUND(RAND()
* ((SELECT MAX(pp.id) FROM pd_problem pp)-(SELECT MIN(pp.id) FROM pd_problem pp))
+(SELECT MIN(pp.id) FROM pd_problem pp)) AS pid) AS p2
where p.id >= p2.pid
and p.is_valid = ''
and p.paper_type = ''
and p.paper_class = ''
order by p.id limit 5
) as t3
[mysql] 随机查询 效率比较的更多相关文章
- 分享:mysql 随机查询数据
在mysql中查询5条不重复的数据,使用以下: 1 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了.但是真正测试一下才发现这样效率非常低.一个1 ...
- 提高MySQL数据库查询效率的几个技巧(转载)
[size=5][color=Red]提高MySQL数据库查询效率的几个技巧(转)[/color][/size] MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我 ...
- 如何实现MySQL随机查询数据与MySQL随机更新数据?
以下的文章主要介绍的是MySQL随机选取数据,对实现MySQ随机查询数据与MySQ随机更新数据的实际操作步骤的描述,以及对其实际操作中所要用到的语句的描述,以下就是对其具体操作步骤的描述. MySQL ...
- mysql随机查询记录的高效率方法
mysql使用rand随机查询记录的高效率方法 一直以为mysql随机查询几条数据,就用 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了. 但是真 ...
- mysql随机查询符合条件的几条记录
随机查询,方法可以有很多种.比如,查询出所有记录,然后随机从列表中取n条记录.使用程序便可实现.可是程序实现必须查询出所有符合条件的记录(至少是所有符合条件的记录id),然后再随机取出n个id,查询数 ...
- mysql 随机查询 记录集
有时候需求需要随机从数据库查询若干条记录集,网上搜了一下,几篇博文都是些重复的.....不知道他们谁抄的谁的,这里除了介绍提供一种笔者自己想到的方法,本质都是利用mysql 的rand() 第一种方法 ...
- 如何提高Mysql的查询效率???
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- Mysql 随机查询10条数据效率最快的查询方法
1)使用join 和 rand() 耗时 0.009 SELECT * FROM `t_topic` AS t1 JOIN ( SELECT ROUND( RAND() * ( (SELECT MAX ...
- mysql随机查询若干条数据
条不重复的数据,使用以下: 秒以上 搜索Google,网上基本上都是查询max(id) * rand()来随机获取数据. SELECT * FROM `table` AS t1 JOIN (SELE ...
随机推荐
- 如何生成KeyStore
介绍如何生成keystore cmd下: 进入到jdk的bin目录,这样的话,android.keystore文件就会生成在这个目录下,签名的时候我们需要这个文件. C:\Program Files\ ...
- Python监控Windows下的文件变化
windows下监控文件系统的变化.用python非常方便.实例代码例如以下,非常easy.也不多说了. import os import win32file import win32con ACTI ...
- winform dataGridView DataGridViewComboBoxColumn 下拉框事件
有一个dataGridView ,有一列是DataGridViewComboBoxColumn .用动态绑定,在绑定数据的时候.我们也给这一列绑定数据 在dataGridView的RowsAdded事 ...
- Aixs2 使用总结,持续更新中 ...
参考博客:http://zhangjunhd.blog.51cto.com/113473/23692 消息交换模式. 目前Axis2支持三种模式:In-Only.Robust-In和In-Ou ...
- MySQL中 如何查询表名中包含某字段的表 ,查询MySql数据库架构信息:数据库,表,表字段
--查询tablename 数据库中 以"_copy" 结尾的表 select table_name from information_schema.tables where ta ...
- MySQL 数据库 分页查询
在使用MySQL 进行数据库分页查询的时候最主要是使用LIMIT子句进行查询: 首先来看一下LIMIT: LIMIT子句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数,如果给出两 ...
- MarkDown的vim插件安装
作用:可以使markdown语法高亮.1.安装.使用pathogen插件管理. cd ~/.vim/bundle git clone https://github.com/plasticb ...
- 【Android】4.0 Android项目的基本结构
分类:C#.Android.VS2015: 创建日期:2016-02-06: 修改日期:2016-02-27 一.简介 第3章虽然通过百度地图应用展示了你可能感兴趣的内容,但是,如果你是一个初学者,一 ...
- java jvm perf
http://www.oracle.com/technetwork/java/performance-138178.html http://www.oracle.com/technetwork/jav ...
- vim环境配置
ctags ctags对浏览代码非常的方便,可以在函数.变量之间跳来跳去等等. 下载 下载路径:http://ctags.sourceforge.net/ 我已经下载过了,路径:/home1/fuju ...