mysql随机抽取数据,效率

因为mysql数据的最大值和最小值的获取几乎是0消耗的,数据库表数据与自身主键ID的集合做join查询,是非常快速的。

1:select * from users order by rand() LIMIT 10000

在ORDER BY从句里面不能使用RAND()函数,
因为这样会导致数据列被多次扫描,导致效率相当相当的低!效率不行,切忌使用!

2:SELECT * FROM users  AS t1  JOIN

(

SELECT ROUND

(RAND() * ((SELECT MAX(userId) FROM `users`)-(SELECT MIN(userId) FROM users))+(SELECT MIN(userId) FROM users))

AS userId

)

AS t2

WHERE t1.userId >= t2.userId

ORDER BY t1.userId

LIMIT 10000

ROUND()四舍五入,0<=RAND()<1 随机数
3:SELECT * FROM users WHERE userId >= ((SELECT MAX(userId) FROM users)-(SELECT MIN(userId) FROM users)) * RAND() + (SELECT MIN(userId) FROM users)  LIMIT 10000

你可能感兴趣的:(mysql随机抽取数据,效率)