我会用join
代替
select *
from article_table at
join (select uid
from user_table
where type = '1') ut
on at.uid = ut.uid
where image != ''
group by at.uid
order by rand()
limit 10
做,或你可能想从您user_table
限制uid
的数目,以使其更快地首先:
select at.*
from article_table at
join (select uid
from user_table
where type = '1'
order by rand()
limit 10) ut
on at.uid = ut.uid
where image != ''
group by at.uid
order by rand()
limit 10
我假设这里有很多文章给每个用户。虽然它看起来更可怕,但内部select中的order by rand()
超过了一个较小的数据集,这会加快速度,外部select中的order by
只能处理较少数量的行。
要小心,按照随机值排序可能会导致显着的性能下降,因为您必须遍历与where子句匹配的整个表。有alternatives。
来源
2012-03-04 12:34:33
Ben