最初のステップ:選択クエリでは不要な列をさらに削除できますが、必要なのは合計行数だけです。例:
select count(*) from ( SELECT
HD.URINO
FROM
TV_HD HD
LEFT JOIN TV_SNAPSHOT T ON (HD.URINO = T.URINO AND HD.TCODE = T.TCODE AND T.DELFLG = 0 AND T.SYUBETSU = 1)
LEFT JOIN TV_SNAPSHOT T_SQ ON (HD.URINO = T_SQ.URINO AND HD.SQCODE = T_SQ.TCODE AND T_SQ.DELFLG = 0 AND T_SQ.SYUBETSU = 3)
LEFT JOIN (SELECT
N.URINO
FROM
TD_MAKKA N
WHERE
N.UPDATETIME IN (
SELECT
MIN (NMIN.UPDATETIME)
FROM
TD_MAKKA NMIN
WHERE
N.URINO = NMIN.URINO
AND
NMIN.TORIKESHIFLG <> -1
)
) NYUMIN
ON (HD.URINO = NYUMIN.URINO)
LEFT JOIN
(
SELECT
NSUM.URINO
,SUM (COALESCE(NSUM.NYUKIN ,0)) NYUKIN
,SUM (COALESCE(NSUM.NYUKIN ,0)) + SUM (COALESCE(NSUM.TESU ,0)) + SUM (COALESCE(NSUM.SOTA ,0)) SUMNYUKIN
FROM
TD_MAKKA NSUM
GROUP BY
URINO
) NYUSUM
ON (HD.URINO = NYUSUM.URINO)
LEFT JOIN
(
SELECT
N.URINO
FROM
TD_MAKKA N
WHERE
UPDATETIME = (
SELECT
MAX (UPDATETIME)
FROM
TD_MAKKA NMAX
WHERE
N.URINO = NMAX.URINO
AND
NMAX.TORIKESHIFLG <> -1
)
) NYUMAX
ON (HD.URINO = NYUMAX.URINO)
WHERE ( (HD.URIBRUI <> '1') OR ( HD.URIBRUI = '1' AND T_SQ.NYUKOBEFLG = '-1' ) )
ORDER BY
HD.URINO DESC
) COUNT_
2番目のステップ:行数を取得するのに重要ではない左結合を回避できます。例:
select count(*) from ( SELECT
HD.URINO
FROM
TV_HD HD
LEFT JOIN TV_SNAPSHOT T ON (HD.URINO = T.URINO AND HD.TCODE = T.TCODE AND T.DELFLG = 0 AND T.SYUBETSU = 1)
LEFT JOIN TV_SNAPSHOT T_SQ ON (HD.URINO = T_SQ.URINO AND HD.SQCODE = T_SQ.TCODE AND T_SQ.DELFLG = 0 AND T_SQ.SYUBETSU = 3)
LEFT JOIN (SELECT
N.URINO
FROM
TD_MAKKA N
WHERE
N.UPDATETIME IN (
SELECT
MIN (NMIN.UPDATETIME)
FROM
TD_MAKKA NMIN
WHERE
N.URINO = NMIN.URINO
AND
NMIN.TORIKESHIFLG <> -1
)
) NYUMIN
ON (HD.URINO = NYUMIN.URINO)
LEFT JOIN
(
SELECT
N.URINO
FROM
TD_MAKKA N
WHERE
UPDATETIME = (
SELECT
MAX (UPDATETIME)
FROM
TD_MAKKA NMAX
WHERE
N.URINO = NMAX.URINO
AND
NMAX.TORIKESHIFLG <> -1
)
) NYUMAX
ON (HD.URINO = NYUMAX.URINO)
WHERE ( (HD.URIBRUI <> '1') OR ( HD.URIBRUI = '1' AND T_SQ.NYUKOBEFLG = '-1' ) )
) COUNT_
3番目のステップ: PgAdmin Graphical Explain Plansを使用できます クエリを分析し、他の不要な実行オーバーヘッドを回避します。