我會用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