If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Merge vs Update

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-16-10, 01:26
sethumk007 sethumk007 is offline
Registered User
 
Join Date: Sep 2010
Posts: 2
Merge vs Update

Hi all,
I m new to db2 and I am working with db2 9.1V. I experienced that Merge statement is faster when compared to update.
For updating 15 million records using merge took 17.23 mins and Update statement took 30.11 mins.

Can anyone explain me the reason, whats the strategy that makes Merge faster than Update, even though in Merge also we are using the Update statement only.
Also can any one tell how mergestatement works in the backend?

Thanks in Advance.
Reply With Quote
  #2 (permalink)  
Old 09-16-10, 08:19
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
How are you doing the UPDATE and MERGE statements? Can you post the statements you are using? It would also be useful to post any DDL including indexes on the affected tables. It is rather hard to answer your specific question with just a general knowledge of the problem.

Andy
Reply With Quote
  #3 (permalink)  
Old 09-17-10, 03:15
sethumk007 sethumk007 is offline
Registered User
 
Join Date: Sep 2010
Posts: 2
Merge vs Update

The following are the update and the merge statements. In both the tables the Index is created on the column ACCT_ID. The total number of records to be updated is 15,636,417

----- Merge statement

MERGE TABLE_A TA
USING (SELECT TB.ACCT_ID,
MAX(CAST(TB.BDT AS DATE)) BDATE
FROM TABLE_B TB
WHERE TB.AGE BETWEEN 18 AND 100
GROUP BY TB.ACCT_ID) TB1
ON TA.ACCT_ID = TB1.ACCT_ID
AND TA.DOB IS NULL
WHEN MATCHED THEN
UPDATE SET TA.DOB = TB1.BDATE;


----- Update statement

UPDATE TABLE_A TA
SET BD.DOB = (SELECT MAX(CAST(TB.BDT AS DATE)) BDATE
FROM TABLE_B TB
WHERE TB.AGE BETWEEN 18 AND 100
AND TA.ACCT_ID = TB.ACCT_ID
GROUP BY TA.ACCT_ID)
WHERE TA.DOB IS NULL;

--------------
Reply With Quote
  #4 (permalink)  
Old 09-17-10, 08:09
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
My guess as to why the merge goes faster is because the subselect in the merge is only done once for the entire process and in the update statement it is done for every row. Look at the access plans for both statements and see if you can find the difference.

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On