私は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
ここでは、各ユーザーに多くの記事があると仮定しています。それはもっと恐ろしいように見えますが、内部セレクトのorder by rand()
は小さいデータセットを超えているため、処理速度が向上し、外側セレクトのは少ない数のローを処理するだけです。
ランダム値で並べ替えると、where句に一致するテーブル全体を処理する必要があるため、パフォーマンスが大幅に低下することがあります。 alternativesがあります。
出典
2012-03-04 12:34:33
Ben