I need to perform a daily update of a very large (300M records) and broad TABLE1
. The the source data for the updates is located in another table UTABLE>
Actually i've found out general recommendations for such a queries: Idea to use SQL Merge or Update is a very clever one but it fails when we need to update many records (i.e. 75M) in a big and wide table (i.e. 240M).
Looking at the query plan of the query below we can say that TABLE SCAN
of TABLE1 and final MERGE
are taking 90% of time.
MERGE TABLE1 as Target
USING UTABLE as source
ON Target.record_id = source.record_id
WHEN MATCHED AND (condition) THEN
UPDATE SET Target.columns=source.columns
So in order to use MERGE we need to:
UTABLE
smaller or specifying additional condition
that narrows part to-be-merged.TABLE1
twice less reduced my real query time from 11 hours to 40 minutes.As Mark mentioned you can use UPDATE
syntax and use WHERE
clause to narrow part to-be-merged - this will give same results. Also please avoid indexing TABLE1
as this will cause additional work to rebuild index during MERGE