2012-03-04 16 views
0

グループuidによってグループ化され、これらのuidselect uid from user_table where type='1'と同じです。私のクエリは2つの結果しか返しません。問題はどこだ?uidがuidと一致するuidが別のクエリと一致する

select * from article_table where image!='' 
order by rand() 
group by uid 
in (select uid from user_table where type='1') 
limit 10 

答えて

2

私は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があります。

1

この次のクエリはそれを行うだろう、

SELECT * 
FROM article_table 
WHERE image!='' 
     AND uid IN (SELECT DISTINCT uid 
        FROM user_table 
        WHERE TYPE = '1' 
        LIMIT 10) 
GROUP BY uid 
ORDER BY Rand() 
LIMIT 10 
関連する問題