1、按比例随机抽样
- 样本总量M, rand()<=(N/M+μ)
例如:如果抽10%的话
select * from table where rand()<=0.1;
特别注意的是:抽样迁先确认表的总记录数
反例:select * from table order by rand() limit 43659307;
2、精确取N条
- 样本总量M, 你可以先 rand()<=(N/M+μ), 取多一些随机样本
- 然后再在这些随机样本里随机取N条
这样一定是随机取出N条. 且 能够 去掉最耗时间耗资源的全局排序
例1:select * from table where rand()<=0.15 cluster by rand() limit N; ( 数据较大时 用 cluster by)
例2:select * from table where rand()<=0.15 order by rand() limit N; ( 数据较小时 用 order by)