1

我有一个电影数据库,其中包含电影、演员和导演的表格。每部电影在电影表中占据一行。几乎我所有的查询都需要连接到其他表,这样一个 LIMIT 50、偏移量 0 的查询会返回大约 4 部电影的完整数据。下面是一个示例查询。我该如何修改它以确保获取正好 10 部电影的数据?

SELECT movie.id, movie.title, star.name, star.name_url, dir.name, 
       dir.name_url, genre.name, genre.name_url 
FROM movie 
        LEFT JOIN actor 
             ON (movie.id = actor.movie_id) 
        LEFT JOIN person AS star 
             ON (actor.person_id = star.id) 
        LEFT JOIN director 
             ON (movie.id = director.movie_id) 
        LEFT JOIN person AS dir 
             ON (director.person_id = dir.id) 
        LEFT JOIN genre_classification 
             ON (movie.id = genre_classification.movie_id) 
        LEFT JOIN genre 
             ON (genre_classification.genre_id = genre.id)
WHERE (movie.id > 0) 
ORDER BY movie.id 
LIMIT 50 OFFSET 0;

我正在使用可能无关紧要的 PostgresSQL。

4

3 回答 3

3

你去(未经测试):

SELECT movie.id, movie.title, star.name, star.name_url, dir.name, 
       dir.name_url, genre.name, genre.name_url 
FROM 
        (SELECT * FROM movie WHERE movie.id > 0 ORDER BY movie.id LIMIT 10) movie
        LEFT JOIN actor 
             ON (movie.id = actor.movie_id) 
        LEFT JOIN person AS star 
             ON (actor.person_id = star.id) 
        LEFT JOIN director 
             ON (movie.id = director.movie_id) 
        LEFT JOIN person AS dir 
             ON (director.person_id = dir.id) 
        LEFT JOIN genre_classification 
             ON (movie.id = genre_classification.movie_id) 
        LEFT JOIN genre 
             ON (genre_classification.genre_id = genre.id)

编辑:通过将所有条件放入子选择中,您无法控制源表中的哪些数据movie将用于 JOIN。性能方面,这也应该更快。

于 2012-08-30T15:01:54.950 回答
1

仅供参考,下面的这个更聪明:它不使用SELECT *并且优化了性能(即使你没有问题):

SELECT movie.id, movie.title, star.name, star.name_url, dir.name, 
       dir.name_url, genre.name, genre.name_url 
FROM movie 
        LEFT JOIN actor          ON (movie.id = actor.movie_id) 
        LEFT JOIN person AS star ON (actor.person_id = star.id) 
        LEFT JOIN director       ON (movie.id = director.movie_id) 
        LEFT JOIN person AS dir  ON (director.person_id = dir.id) 
        LEFT JOIN genre_classification ON (movie.id = genre_classification.movie_id) 
        LEFT JOIN genre          ON (genre_classification.genre_id = genre.id)
        JOIN (
            SELECT id
            FROM movie
            WHERE (id > 0) 
            ORDER BY id
            LIMIT 10
        ) AS sel ON (sel.id=movie.id)
ORDER BY movie.id;
于 2012-08-30T15:46:52.133 回答
0

仍然未经测试。性能不如 tdk 给出的答案,但更具可读性,可以使用 WITH 语句编写子选择:

WITH 
  sub_movie AS (
    SELECT * FROM movie WHERE movie.id > 0 ORDER BY movie.id LIMIT 10
   )
SELECT
  sm.id,
  sm.title,
  star.name,
  star.name_url,
  dir.name,
  dir.name_url,
  genre.name,
  genre.name_url
FROM
  sub_movie sm
    LEFT JOIN actor 
         ON (movie.id = actor.movie_id) 
    LEFT JOIN person AS star 
         ON (actor.person_id = star.id) 
    LEFT JOIN director 
         ON (movie.id = director.movie_id) 
    LEFT JOIN person AS dir 
         ON (director.person_id = dir.id) 
    LEFT JOIN genre_classification 
         ON (movie.id = genre_classification.movie_id) 
    LEFT JOIN genre 
         ON (genre_classification.genre_id = genre.id)

我建议不要调用 PK 列id,而是my_table_id使用智能连接语法,例如

    LEFT JOIN director USING (director_id)

祝你好运。

于 2012-08-30T15:54:03.220 回答