我想优化此查询:
WITH t AS (
SELECT c.pl_number, c.def
FROM rac_import i JOIN (
SELECT
t.pl_number,
(CASE WHEN t.self_bk_lists_id IS NOT NULL THEN 1
WHEN t.ad_hoc_parcels_id IS NOT NULL AND t.service IN('105', '138', '107', '139') THEN 2
ELSE 3 END) AS def
FROM dblink('hostaddr=x.x.x.x port=xxx dbname=xxxx user=xxxx password=xxxx'::text, '
SELECT tr.pl_number, tr.self_bk_lists_id, tr.ad_hoc_parcels_id, s.service
FROM cashmodul.transmission_register tr
LEFT OUTER JOIN scans.scandata05 s ON tr.pl_number = s.parcelno'::text)
t(pl_number character varying(14), self_bk_lists_id integer, ad_hoc_parcels_id integer, service character varying)
) AS c ON i.parcelno = c.pl_number
WHERE i.akcija = 3
)
UPDATE rac_import
SET def = t.def
FROM t
WHERE parcelno = t.pl_number
当前执行时间大约是4-5秒,我希望大约0.5秒。
编辑:
UPDATE rac_import AS i SET def = (CASE WHEN t.self_bk_lists_id IS NOT NULL THEN 1
WHEN t.ad_hoc_parcels_id IS NOT NULL AND t.service IN('105', '138', '107', '139') THEN 2
ELSE 3 END)
FROM (
SELECT
t.pl_number,
t.self_bk_lists_id,
t.ad_hoc_parcels_id,
t.service
FROM dblink('hostaddr=x.x.x.x port=xxxx dbname=xxxx user=xxxx password=xxxx'::text, '
SELECT tr.pl_number, tr.self_bk_lists_id, tr.ad_hoc_parcels_id, s.service
FROM cashmodul.transmission_register tr
LEFT OUTER JOIN scans.scandata05 s ON tr.pl_number = s.parcelno'::text)
t(pl_number character varying(14), self_bk_lists_id integer, ad_hoc_parcels_id integer, service character varying)
) AS t
WHERE i.parcelno = t.pl_number AND i.akcija = 3
使用此代码管理减少1秒。