SQL:控制返回的记录数

时间:2012-08-30 14:54:40

标签: sql database postgresql

我有一个电影数据库,里面有电影,演员和导演的表格。每部电影在电影表中占据一行。几乎所有的查询都需要连接到其他表,以便使用LIMIT 50,offset 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,这可能无关紧要。

3 个答案:

答案 0 :(得分: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。性能方面,这也应该快得多。

答案 1 :(得分: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;

答案 2 :(得分: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)
祝你好运。