Molet

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

Molet apache 1970-01-01 983浏览 0

从 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秒,速%

发表评论