orderby特别地,如果未按by排序的表达式索引,则an总是会很昂贵。所以不要点。相反
count(),在查询中的中做一个随机偏移,但要一次全部完成。
with t as ( select * from products p inner join images i using (productid) where prodtype = $sometype)select *from toffset floor(random() * (select count(*) from t))limit 1
这个版本可能更快
with t as ( select *, count(*) over() total from products p inner join images i using (productid) where prodtype = $sometype)select *from toffset floor(random() * (select total from t limit 1))limit 1



