bitscn.com
发现在sql语句里有一个 order by rand() 这样的一个语句,这个说是用着方便,但是效率实在是太低了,于是我用了以下的方法来优化,就是用join表的方法来达到这个取随机数据行的方法,你可以用 explain sql语句来分析一下以下两条sql语句的效率,当然,数据量至少上10万以上才能看出性能。
[1]普通方法, 效率太低
select * from table order by rand() limit 10;
[2] 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 10;
作者“威尔软件”
bitscn.com