with t as (
select 1 id
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
)
select
*
,id % 2 as id2
from t
distribute by rand()
sort by rand()
LIMIT 5
-- order by rand() 随机效率较低,上述效率较高
本文讨论了如何通过在SQL查询中使用withtas和分发排序算法(如distributebyrand()和sortbyrand()),结合UNIONALL操作,来提升从表中随机获取数据的效率,特别指出orderbyrand()效率较低,而上述方法更优。





