Langkah pertama:Anda dapat menghapus lebih banyak kolom yang tidak diperlukan dalam kueri pemilihan Anda di mana Anda hanya perlu menghitung total baris. Contoh:
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_
Langkah Kedua:Anda dapat menghindari gabungan kiri yang tidak memiliki arti penting untuk mendapatkan jumlah baris. Contoh:
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_
Langkah Ketiga:Anda dapat menggunakan PgAdmin Graphical Explain Plans untuk menganalisis kueri dan menghindari overhead eksekusi yang tidak perlu lainnya.