Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

MERGE for INSERT alone Expand / Collapse
Author
Message
Posted Monday, December 10, 2012 3:53 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 10:20 AM
Points: 121, Visits: 1,388
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
Post #1394524
Posted Monday, December 10, 2012 7:00 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
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
Post #1394596
Posted Monday, December 10, 2012 7:04 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 4:26 PM
Points: 1,616, Visits: 2,119
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)

Internet ATM Machine
Post #1394600
Posted Monday, December 10, 2012 9:38 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 10:20 AM
Points: 121, Visits: 1,388
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..
Post #1394876
Posted Monday, December 10, 2012 10:40 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 4:26 PM
Points: 1,616, Visits: 2,119
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)

Internet ATM Machine
Post #1394891
Posted Monday, December 10, 2012 10:46 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 10:20 AM
Points: 121, Visits: 1,388
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.
Post #1394893
Posted Tuesday, December 11, 2012 6:13 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
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
Post #1395079
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse