3

美好的一天,我的问题是关于优化 sql 查询。以下查询很慢:

SELECT id, name,
, (SELECT rank_time FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1)::date AS rank_time
, (SELECT host_c    FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS host_c
, (SELECT index_pa  FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS index_pa
, (SELECT links_pa  FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS links_pa
, (SELECT index_pb  FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS index_pb
, (SELECT links_pb  FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS links_pb
FROM stage
ORDER BY name;

我认为这主要是因为重复选择 from stage_rank,是否有可能使这个选择完成一次,并在一次点击中获得所有字段?

还有任何 postgresql 特定的功能在这里可能有帮助吗?

4

1 回答 1

9

PostgreSQL中,您可以选择整条记录作为字段并稍后展开:

SELECT  id, name, (sr).*
FROM    (
        SELECT  id, name,
                (
                SELECT  stage_rank
                FROM    stage_rank
                WHERE   stage = stage.id
                ORDER BY
                        rank_time DESC
                LIMIT 1
                ) sr
        FROM    stage
        ) q
ORDER BY
        name

或重写查询:

SELECT  DISTINCT ON (s.name, s.id, sr.rank_time, sr.id)
        s.id, s.name, sr.*
FROM    stage s
JOIN    stage_rank sr
ON      sr.stage = s.id
ORDER BY
        s.name, s.id, sr.rank_time DESC, sr.id DESC

或以另一种方式重写:

SELECT  id, name, (sr).*
FROM    (
        SELECT  s.id, s.name, sr, ROW_NUMBER() OVER (PARTITION BY s.id ORDER BY sr.rank_time DESC, sr.id DESC) rn
        FROM    stage
        JOIN    stage_rank sr
        ON      sr.stage = s.id
        ) q
WHERE   rn = 1
ORDER BY
        name
于 2012-05-18T10:59:04.250 回答