从Mysql某一表中随机读取n条数据的SQL查询语句

  1. 云栖社区>
  2. 博客>
  3. 正文

从Mysql某一表中随机读取n条数据的SQL查询语句

云栖希望。 2017-12-17 21:17:00 浏览810
展开阅读全文

从 Mysql 表中随机读取数据不难,方法还挺多的,但是如果要考虑效率,得到一个快速的高效率的方法,那就不是一件简单的事情了(至少对我来说不简单)。

语句简单,速度慢的方法
SELECT * FROM table_name ORDER BY rand() LIMIT 5;

语句复杂,速度快的方法
SELECT * FROM table_name AS r1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM table_name)) AS id) AS r2 WHERE r1.id >= r2.id ORDER BY r1.id ASC LIMIT 5;


cjc注: ... AS id) AS t2 这里的 id, 也许该换成id2, 后面 WHERE t1.id >= t2.id 改成  WHERE t1.id >= t2.id2

摘自: http://www.dayanmei.com/blog.php/ID_203.htm

******************************************************************************************


Discuz中,随机读取论坛的几个连续主题:

PHP里调用:
$s = "SELECT tid, fid, subject FROM {$tablepre}threads AS r1 JOIN (SELECT ROUND(RAND()*(SELECT MAX(tid) FROM {$tablepre}threads)) AS id) AS r2 WHERE (fid=13) and (r1.tid >= r2.id) ORDER BY r1.tid LIMIT 6";

$query = $db->query($s);
       

phpMyAdmin 中测试语句:
SELECT tid, fid, subject FROM cdb_threads AS r1 JOIN (SELECT ROUND(RAND()*(SELECT MAX(tid) FROM cdb_threads)) AS id) AS r2 WHERE (fid=13) and (r1.tid >= r2.id) ORDER BY r1.tid LIMIT 6

注意: 不可删除 order by r1.tid, 否则总会固定出现最初的2行数据 


******************************************************************************************

另一文章里使用了MIN()的版本:

通用语句:

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;


cjc注: ... AS id) AS t2 这里的 id, 也许该换成id2, 后面 WHERE t1.id >= t2.id 改成  WHERE t1.id >= t2.id2

Discuz 语句:

SELECT tid, fid,subject
FROM `cdb_threads` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(tid) FROM `cdb_threads`)-(SELECT MIN(tid) FROM `cdb_threads`))+(SELECT MIN(tid) FROM `cdb_threads`)) AS id) AS t2
WHERE t1.tid >= t2.id and fid=13 ORDER BY t1.tid LIMIT 5;


or

$s = "SELECT * FROM {$tablepre}threads AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(tid) FROM {$tablepre}threads)-(SELECT MIN(tid) FROM {$tablepre}threads))+(SELECT MIN(tid) FROM {$tablepre}threads)) AS id) AS t2 WHERE (t1.tid >= t2.id) and (fid=13) ORDER BY t1.tid LIMIT 6";

$query = $db->query($s);
       

cjc注: MIN(tid) 得到的通常是1, 所以加上MIN()的计算,不一定有必要.



***************************  全文转贴如下 ******************************************************
原文网址: http://jnote.cn/blog/mysql/mysql-rand-efficiency.html

mysql使用rand随机查询记录效率测试 - 八月 8, 2007 | Posted by 老蒋

一直以为mysql随机查询几条数据,就用

SELECT * FROM `table` ORDER BY RAND() LIMIT 5

就可以了。
但是真正测试一下才发现这样效率非常低。一个15万余条的库,查询5条数据,居然要8秒以上

查看官方手册,也说rand()放在ORDER BY 子句中会被执行多次,自然效率及很低。

You cannot use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times.


搜索Google,网上基本上都是查询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,mysql的论坛上有人使用

SELECT *
FROM `table`
WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` )
ORDER BY id LIMIT 1;
我测试了一下,需要0.5秒,速度也不错,但是跟上面的语句还是有很大差距。总觉有什么地方不正常。

于是我把语句改写了一下。

SELECT * FROM `table`
WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table`)))  
ORDER BY id LIMIT 1;
这下,效率又提高了,查询时间只有0.01秒

最后,再把语句完善一下,加上MIN(id)的判断。我在最开始测试的时候,就是因为没有加上MIN(id)的判断,结果有一半的时间总是查询到表中的前面几行。
完整查询语句是:

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;

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;


最后在php中对这两个语句进行分别查询10次,
前者花费时间 0.147433 秒
后者花费时间 0.015130 秒
看来采用JOIN的语法比直接在WHERE中使用函数效率还要高很多。
此方法limit如果大于1,返回的结果会是连续的记录。

本文转自博客园知识天地的博客,原文链接:从Mysql某一表中随机读取n条数据的SQL查询语句,如需转载请自行联系原博主。

网友评论

登录后评论
0/500
评论
云栖希望。
+ 关注