我想从SecondTable更新FirstTable,但是有很多麻烦。 这是主要查询:
UPDATE x.FirstTable Table1
set Table1.UpdatedColumn = (
SELECT Table2.V_PROD_CODE
FROM y.SecondTable Table2
WHERE Table2.XX_V_ACCOUNT_ID1 = Table1.XX_V_ACCOUNT_ID1
AND Table2.XX_V_ACCOUNT_ID2 = Table1.XX_V_ACCOUNT_ID2
AND Table2.XX_V_ACCOUNT_ID3 = Table1.XX_V_ACCOUNT_ID3
AND Table2.c_Date between '17-Apr-2018' and '27-Apr-2018'
AND Table2.c_DATE = Table1.c_date)
WHERE Table1.c_date between '17-Apr-2018' and '27-Apr-2018'
AND length(Table1.xx_v_account_id1) = 12;
花费的时间太长,所以我考虑创建一个游标:
create or replace procedure wco as
cursor UpdateCursor is
SELECT Table2.V_PROD_CODE
FROM y.SecondTable Table2
INNER JOIN x.FirstTable Table1 on SUBSTR(Table1.V_CAST_REF_CODE, 6, 8) = SUBSTR(Table2.V_CAST_REF_CODE, 6, 8)
WHERE Table2.XX_PRODUCT_CODECCOUNT_ID1 = Table1.XX_PRODUCT_CODECCOUNT_ID1
AND Table2.XX_PRODUCT_CODECCOUNT_ID2 = Table1.XX_PRODUCT_CODECCOUNT_ID2
AND Table2.XX_PRODUCT_CODECCOUNT_ID3 = Table1.XX_PRODUCT_CODECCOUNT_ID3
AND Table2.fic_mis_date between '17-Apr-2018' and '27-Apr-2018'
AND Table2.c_DATE = Table1.c_date
for update;
v_PRODUCT_CODE Table2.V_PROD_CODE%type;
begin
open UpdateCursor;
loop
fetch UpdateCursor into v_PRODUCT_CODE;
exit when UpdateCursor%notfound;
update XXBADWH.xxba_dwh_instrument_master INST
set INST.v_product_code = v_PRODUCT_CODE
WHERE current of UpdateCursor
AND INST.fic_mis_date between '17-Apr-2018' and '27-Apr-2018'
AND length (INST.xx_v_account_id1) = 12;
end loop;
close UpdateCursor;
end;
exec wco;
drop procedure wco;
查询出了什么问题?以及其他更好的做法?
答案 0 :(得分:1)
对于此查询:
UPDATE x.FirstTable Table1
SET Table1.UpdatedColumn = (
SELECT Table2.V_PROD_CODE
FROM y.SecondTable Table2
WHERE Table2.XX_V_ACCOUNT_ID1 = Table1.XX_V_ACCOUNT_ID1 AND
Table2.XX_V_ACCOUNT_ID2 = Table1.XX_V_ACCOUNT_ID2 AND
Table2.XX_V_ACCOUNT_ID3 = Table1.XX_V_ACCOUNT_ID3 AND
Table2.c_Date between DATE '2018-04-17' and DATE '2018-04-27' AND
Table2.c_DATE = Table1.c_date
)
WHERE Table1.c_date between DATE '2018-04-17' and DATE '2018-04-27' AND
length(Table1.xx_v_account_id1) = 12;
您要在Table1(length(xx_v_account_id1), c_date)
和Table2(XX_V_ACCOUNT_ID1, XX_V_ACCOUNT_ID2, XX_V_ACCOUNT_ID3, c_DATE)
上建立索引。
我将从索引开始(在select
select语句上进行测试)。游标很少是获得更好性能的途径。