|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:01 AM
Points: 111,
Visits: 1,333
|
|
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
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
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
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:20 AM
Points: 1,446,
Visits: 1,883
|
|
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)
   Weight Loss Tips
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:01 AM
Points: 111,
Visits: 1,333
|
|
| 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..
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:20 AM
Points: 1,446,
Visits: 1,883
|
|
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)
   Weight Loss Tips
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:01 AM
Points: 111,
Visits: 1,333
|
|
| 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.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
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
|
|
|
|