若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1))。例如, 若要在7 到 12 的范围(包括7和12)内得到一个随机整数, 可使用以下语句:
SELECT FLOOR(7 + (RAND() * 6));
从 Mysql 表中随机读取数据不难,方法还挺多的,但是如果要考虑效率,得到一个快速的高效率的方法,那就不是一件简单的事情了(至少对我来说不简单)。
随机获得Mysql数据表的一条或多条记录有很多方法,下面我就以users(userId,userName,password……)表(有一百多万条记录)为例,对比讲解下几个方法效率问题:
- select * from users order by rand() LIMIT 1
执行该sql语句,老半天没有反应,最后被迫手动停止执行,怎个伤人了得啊!后来我查了一下MYSQL手册,里面针对RAND()的提示大概意思就是,在 ORDER BY从句里面不能使用RAND()函数,因为这样会导致数据列被多次扫描,导致效率相当相当的低!效率不行,切忌使用! - SELECT * FROM users WHERE userId >= ((SELECT MAX(userId) FROM users)-(SELECT MIN(userId) FROM users)) * RAND() + (SELECT MIN(userId) FROM users) LIMIT 1
执行该sql语句,用时0.039s,效率太给力了!接着我就把”LIMIT 1“改为了”LIMIT 10000“,用时0.063s。经过多次验证,结果肯定是随机的!
结论:随机取一条或多条记录,方法都不错! - 通过sql获得最大值和最小值,然后通过php的rand生成一个随机数randnum,再通过SELECT * FROM users WHERE userId >= randnum LIMIT 1,获得一条记录效率应该还可以,多条应该就不行了。
结论:方法1效率不行,切忌使用;随机获得一条记录,方法2也不错;随机获得多条记录,方法2没说的!
从Mysql某一表中随机读取n条数据的SQL查询语句其他相关资料
SQL语句先随机好ID序列,用 IN 查询(飘易推荐这个用法,IO开销小,速度最快):
$sql="SELECT MAX(id),MIN(id) FROM content";
$result=mysql_query($sql,$conn);
$yi=mysql_fetch_array($result);
$idmax=$yi[0];
$idmin=$yi[1];
$idlist='';
for($i=1;$i<=20;$i++){
if($i==1){ $idlist=mt_rand($idmin,$idmax); }
else{ $idlist=$idlist.','.mt_rand($idmin,$idmax); }
}
$idlist2="id,".$idlist;
$sql="select * from content where id in ($idlist) order by field($idlist2) LIMIT 0,12";
$result=mysql_query($sql,$conn);
$n=1;
$rnds='';
while($row=mysql_fetch_array($result)){
$rnds=$rnds.$n.". <a href='show".$row['id']."-".strtolower(trim($row['title']))."'>".$row['title']."</a><br />
";
$n++;
}800万数据随机取一条的牛方法
mysql> select FLOOR(id*rand()) from test_rand where id=(select MAX(id) from test
_rand);
+------------------+
| FLOOR(id*rand()) |
+------------------+
| 5225551 |
+------------------+
1 row in set (0.00 sec)
语句简单,速度慢的方法
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;
摘自: 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,返回的结果会是连续的记录。