0

以下のUNIONクエリでは、サブクエリ#2と#4は同一であり、同じデータを2回フェッチすることは避けたいと思います。私の知る限り、一部のデータベースWITHではクエリの先頭に句を記述できますが、SQLiteはそれをサポートしていません。したがって、問題は、以下のクエリを最適化する方法です。

 SELECT disliked, recommended, weight
 FROM
   (SELECT movie_id * 10 AS disliked
      FROM ratings
     WHERE rating IN (1,2)
       AND user_id = #{id}
   ) AS input,
   (SELECT movie_id AS recommended, rating AS weight
      FROM ratings
     WHERE rating IN (4,5)
       AND user_id = #{id}
   ) AS output
 UNION
 SELECT liked, recommended, weight
 FROM
   (SELECT movie_id, movie_id * 10 + 1 AS liked
      FROM ratings
     WHERE rating IN (4,5)
       AND user_id = #{id}
   ) AS input,
   (SELECT movie_id AS recommended, rating AS weight
      FROM ratings
     WHERE rating IN (4,5)
       AND user_id = #{id}
   ) AS output
 WHERE input.movie_id != recommended
4

1 に答える 1

1

私はあなたをここまで早く読んだかどうか完全には確信が持てませんが、おそらく次のようなものです。

 SELECT
     10*r1.movie_id+(r1.rating IN (4,5)) AS likedisliked,
     r2.movie_id AS recommended,
     r2.rating AS weight
 FROM ratings AS r1, ratings AS r2 
 WHERE r1.user_id=#{id} AND r2.user_id=#{id}
       AND r1.rating IN (1,2,4,5) AND r2.rating IN (4,5)
       AND r1.movie_id!=r2.movie_id; 
于 2012-09-26T21:10:53.780 に答える