SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


MERGE for INSERT alone


MERGE for INSERT alone

Author
Message
MuraliKrishnan1980
MuraliKrishnan1980
SSC Veteran
SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)

Group: General Forum Members
Points: 211 Visits: 1409
Hi Friends,

We are in the process of Migrating data from one DB to another DB. The tables in the source and target DBs are same with slight changes in the schema. The requirement is simple. If a record in source table does not exist in the corresponding table in target DB just insert it into the table in the target DB. If it is already there, do nothing. However, in case of insertion, we have to insert the identity field from the table in source and the identity value of the newly inserted record from the target into a mapping table.

At present, i do this by using MERGE statement along with the OUTPUT clause. This is one such query.


CREATE TABLE #UICasePDSHistory(SourcePDSHistoryId INT, TargetPDSHistoryId INT)
MERGE Migration_UI_501_Murali.dbo.UICasePDSHistory AS TGT
USING
(
SELECT A.PDSHistoryID, B.TargetLocalId, C.TargetPDS_Id, A.LastAccess
FROM
Migration_ACTUS_462.dbo.UICasePDSHistory A
JOIN MigrationConfig.dbo.NLPCaseMasterMapping B on A.LocalID = B.SourceLocalId
JOIN MigrationConfig.dbo.UIPluggableDataSourceMapping C on A.PDS_Id = C.SourcePDS_Id
) AS SRC
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT(Pds_ID, LocalID, LastAccess)
VALUES(SRC.TargetPDS_Id, SRC.TargetLocalId, SRC.LastAccess)
OUTPUT SRC.PDSHistoryID, INSERTED.PDSHistoryID INTO #UICasePDSHistory;


As you can see, I am retrieving the identity value from source (src.pdshistoryid) and from target(inserted.pdshistoryid) and store it into a temp table. However, i would like to know if there is any other better way to implement this logic or if MERGE is the only and the best way performance wise as well
GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24347 Visits: 9730
Merge with Output is a perfectly good way to do that.

Is there some specific reason you want to change to something else, or are you just trying to confirm that Merge is okay?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
sgmunson
sgmunson
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7221 Visits: 4382
While a MERGE is not the only way to accomplish the task, we have no information from you to tell us what other things we'd need to take into consideration as to performance or any other characteristic. For example, WHY are you asking? Is it that this doesn't perform well, or that it "sure would be nice if it performed better" ? Typically, the only way to find out is to test an alternate methodology. Based solely on your description, you appear to need to just find all the identity values and their source records that don't exist in the new table, and insert them, plus insert the ID values in another separate table.

These tasks could certainly be accomplished in separate query steps, and you could then encapsulate the entire query in a transaction to help solve potential problems from any breakage that might occur part-way through (e.g. running out of space in tempdb), if necessary. Can you elaborate?

Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
MuraliKrishnan1980
MuraliKrishnan1980
SSC Veteran
SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)

Group: General Forum Members
Points: 211 Visits: 1409
Thanks both of you for your responses. I haven't worked much with Data Migration. So I am not really sure about the performance aspects of MERGE for this purpose. Moreover, the tables might be having a few million records. So I wanted to know if MERGE would do well performance wise as well..
sgmunson
sgmunson
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7221 Visits: 4382
Then perhaps the larger concern is just the overall processing time to perform the task. If the migration is an ALL ABOARD scenario (meaning all the data, all at once), then unless there's a reason for this to take hours, there may not be too much reason to worry about performance, but as the environment hasn't been discussed, we have nothing to go on. Alternatively, you could be migrating in steps, and might have limited windows within which to operate, so you may need to then test such a process and see how long it takes so you can then judge how large the pieces of the migration need to be.

Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
MuraliKrishnan1980
MuraliKrishnan1980
SSC Veteran
SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)

Group: General Forum Members
Points: 211 Visits: 1409
Thanks Steve for your response. The Migration would not be a one time process. It is going to be a daily job and I guess MERGE should be able to deal this well.
GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24347 Visits: 9730
If Merge is too slow on this, then Insert will almost certainly be too slow as well. Those are the two main options.

With either one, the usual way to speed it up is to break the job down into smaller chunks. That will work with either Merge or Insert.

Merge has the advantage that you can use source-columns in the Output clause, which is needed in this case. There are ways to do that with Insert, but they are much more complex.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search