简体   繁体   中英

SQL Update take long time with First_Value function

I have two table KYCCUST and FINACLE_TEMP_in_kyc1. They are in different databases then I want to do some update.

UPDATE  KYCFBHK.KYCFBHK.KYCCUST
SET KYC_SP_08=
(SELECT DISTINCT FIRST_VALUE(KYC_SP_08) over (partition by CUSTNO order by DATE desc) FROM AMLETL.dbo.FINACLE_TEMP_in_kyc1 t1 

WHERE CUSTNO collate Latin1_General_BIN2 = KUNDNR AND HISTBIS ='9999')

WHERE HISTBIS = '9999'

The sub query "Select First_value" return result in 10 secs but when I do the update. It took 1hours and still not yet finish. Can we reduce time run? Our DBMS is MSSQL Server 2016. And in these tables, we also have index.

It worked with query below:

update T1
SET T1.KYC_SP_08=T2.Kyc_sp_08 
from KYCFBHK.KYCFBHK.KYCCUST T1 
Inner join AMLETL.dbo.FINACLE_TEMP_in_kyc1 T2
ON T2.CUSTNO  COLLATE Latin1_General_BIN2 = T1.KUNDNR
And T1.HISTBIS=9999
And T2.Date in (select max(date) from AMLETL.dbo.FINACLE_TEMP_in_kyc1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM