Първа стъпка:Можете да премахнете повече колони, които не се изискват във вашата заявка за избор, където трябва да преброите само общия брой редове. Например:
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_
Втора стъпка:Можете да избегнете ляво свързване, което няма значение за получаване на броя на редовете. Например:
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_
Трета стъпка:Можете да използвате PgAdmin Graphical Explain Plans за анализиране на заявката и избягване на ненужно изпълнение на други.