Mysql中rand()的实现方式
Database changed
mysql> select count(*) from test;
+----------+
| count(*) |
+----------+
| 2097152 |
+----------+
1 row in set (0.00 sec)
+--------+------+
| a | b |
+--------+------+
| 765909 | |
+--------+------+
1 row in set (8.78 sec) --用了将近9s
You cannot use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times.也就是 order by rand()会去扫描多次,造成性能的下降。
先取这个表的最大值*rand(),取出这个随机值后,在进行比对。
mysql> select * from test
-> where a >= (select floor(rand() * (select max(a) from test))) order by a limit 1;
+-------+------+
| a | b |
+-------+------+
| 99275 | |
+-------+------+
1 row in set (0.00 sec) --在0.00s左右
建议继续学习:
扫一扫订阅我的微信号:IT技术博客大学习
- 作者:Incessant 来源: Incessant
- 标签: rand
- 发布时间:2009-10-11 22:35:22
-
[780] WordPress插件开发 -- 在插件使用 -
[61] cookie窃取和session劫持 -
[60] Java将Object对象转换为String -
[59] 学习:一个并发的Cache -
[58] 你必须了解的Session的本质 -
[54] 再谈“我是怎么招聘程序员的” -
[53] Linux如何统计进程的CPU利用率 -
[50] 解读iPhone平台的一些优秀设计思路 -
[50] 我对技术方向的一些反思 -
[48] 一句话crontab实现防ssh暴力破解
