You should change the update to a merge, which should get a much better plan:
MERGE INTO tmp AS tgt
USING
(
SELECT
col1,col2,col3, ST_DT,
COALESCE(MAX(ST_DT)
OVER (PARTITION BY col1
ORDER BY ST_DT ROWS
BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
, TIMESTAMP '9999-12-12 00:00:00') AS end_date
FROM tmp
) AS src
ON tgt.COL1 = src.COL1
AND tgt.COL2 = src.COL2
AND tgt.COL3 = src.COL3
AND tgt.ST_DT = src.ST_DT
AND tgt.end_dt = TIMESTAMP '9999-12-12 00:00:00'
WHEN MATCHED
THEN UPDATE
SET end_dt = src.end_date
Btw, it should be enough to specify the Primary Key column(s) plus the end_date in the ON clause.
Dieter
↧