试试下面的代码:
MySQL select 10 random rows from 600K rows fastORDER BY RAND()
首先程序产生一个随机数,然后大于这个值的取10条SELECT * FROM tablename WHERE id>xxx LIMIT 10
如果想分开的,那就多产生几个随机数x1 x2 x3
SELECT * FROM tablename WHERE id>x1 LIMIT 10
SELECT * FROM tablename WHERE id>x2 LIMIT 10
SELECT * FROM tablename WHERE id>x2 LIMIT 10
然后得到的记录再选10条
如果有其他索引的话也可以随机一个大于或小于这个
select * from tablename where uid>x1 and id>xxx limit 10
'SELECT * FROM (SELECT * FROM tb ORDER BY id DESC LIMIT 1000)X ORDER BY RAND() LIMIT 10意思是在tb表内取1000条,再在其范围内取10条随机数组,避免了全表扫描,效率比单纯的ORDER BY RAND()快很多。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)