我正在尝试更新 FROM 子句中使用的表上的记录。当我执行它时,它需要更多的时间来执行(将近5小时)。 下面附上详细信息。
更新声明
UPDATE schema_name.tbl_bllng upd
SET
rsv_clm = Mc.c_fullform
, counter = tbll.counter + 1
FROM
schema_name.tbl_bllng tbll
INNER JOIN schema_name.tbl_ihdr ihdr
ON(
ihdr.b_id = tbll.b_id
AND ihdr.Icnt = 0
AND ihdr.id = 1
AND ihdr.active_flg = False)
LEFT OUTER JOIN public.schema_name2.master_table Mc
ON(
ihdr.rsv_clm IS NOT NULL
AND Mc.ic_id = ihdr.rsv_clm
AND Mc.id = 1
AND Mc.cd = 1
AND Mc.active_flg = False)
WHERE
tbll.tseq_id = upd.tseq_id
AND tbll.id = 1
AND tbll.active_flg = False;
QUERY PLAN
Update on tbl_bllng upd (cost=1.14..932050009.01 rows=53247247974 width=8053)
-> Merge Join (cost=1.14..932050009.01 rows=53247247974 width=8053)
Merge Cond: ((tbll.b_id)::text = (ihdr.b_id)::text)
-> Nested Loop (cost=0.71..108174.20 rows=171730 width=7628)
-> Index Scan using idx_bllng_id on tbl_bllng tbll (cost=0.29..13970.46 rows=171730 width=23)
Index Cond: ((id = 1) AND (active_flg = false))
-> Index Scan using bllng_pkc on tbl_bllng upd (cost=0.42..0.55 rows=1 width=7613)
Index Cond: (tseq_id = tbll.tseq_id)
-> Materialize (cost=0.43..117666.47 rows=1240212 width=95)
-> Nested Loop Left Join (cost=0.43..114565.94 rows=1240212 width=95)
Join Filter: ((ihdr.rsv_clm IS NOT NULL) AND ((Mc.ic_id)::text = (ihdr.rsv_clm)::text))
-> Index Scan using idx_tbl_bll on tbl_ihdr ihdr (cost=0.43..95961.70 rows=1240212 width=429)
Index Cond: ((Icnt = 0) AND (id = 1) AND (active_flg = false))
-> Materialize (cost=0.00..1.06 rows=1 width=162)
-> Seq Scan on "schema_name2.master_table" Mc (cost=0.00..1.06 rows=1 width=162)
Filter: ((NOT active_flg) AND (id = 1) AND (cd = 1))
Table names and Record counts
schema_name2.master_table this table 4 record only
schema_name.tbl_bllng - 171010
schema_name.tbl_ihdr -1240212
Table datatype:
tbl_ihdr
b_id-----(string)
Icnt---integer
rsv_clm --> string
id-----int
active_flg---Boolean
tbl_bllng
b_id--------> string
tseq_id -- Int
id---int
active_flg---boolean
我是查询调优的新手,请帮助解决此问题。
您不小心进行了
schema_name.tbl_bllng
与自身的自连接。它已经隐式加入为 update
target,因此您无需再次将其列为更新 source。
将指定目标如何与第一个源关联的连接条件下移至
where
部分。您可以保留其他连接不变:
UPDATE schema_name.tbl_bllng upd
SET
rsv_clm = Mc.c_fullform
, counter = upd.counter + 1
FROM schema_name.tbl_ihdr ihdr
LEFT OUTER JOIN public.schema_name2.master_table Mc
ON(
ihdr.rsv_clm IS NOT NULL
AND Mc.ic_id = ihdr.rsv_clm
AND Mc.id = 1
AND Mc.cd = 1
AND Mc.active_flg = False)
WHERE
(ihdr.b_id = upd.b_id
AND ihdr.Icnt = 0
AND ihdr.id = 1
AND ihdr.active_flg = False)
AND upd.tseq_id = upd.tseq_id
AND upd.id = 1
AND upd.active_flg = False;