Is there a scenario where it's faster for synchronizing the data from TableA to TableB by using a MERGE query as opposed to dropping TableB and doing a SELECT * INTO TableB FROM TableA?
Sql-server – Will a MERGE between two tables ever be faster than a DROP and SELECT INTO on the second table
data synchronizationmergeperformancequery-performancesql serversql-server-2016
Related Question
- Sql-server – Pull valid row from table even when joining table has no valid data
- Sql-server – Merge/Intersection between table and dataset – How to achieve
- Mysql – Would querying from a partitioned table be faster than from multiple tables with the same table definition
- Sql-server – Update table with data from table in a separate database
- Sql-server – Select rows from two joined table that not in a third table
- Sql-server – Optimise XOR search on int list
Best Answer
I think, in general, I'd say probably not. But it depends on a lot of factors.
Bulk Loading (minimal logging)
SELECT...INTO
will benefit from bulk load optimizations. TheINSERT
portion of theMERGE
query might benefit from these optimizations, but theUPDATE
portion won't. So theMERGE
statement will likely result in more transaction log writes, making it slower.DROP vs DELETE
SELECT...INTO
will also benefit from the fact thatDROP TABLE
is a metadata operation, and thus will "remove" all the rows very quickly. TheDELETE
portion of theMERGE
query will be logged normally, resulting in more transaction log writes, making it slower.Concurrency
SELECT...INTO
will benefit from reduced locking / blocking / concurrency issues, because it will have exclusive access to the target table. TheMERGE
statement will potentially have to deal with lock escalation, blocking by other processes, etc, unless you provideTABLOCK
hints.Index Maintenance
The
MERGE
statement also has to deal with index maintenance (you mentioned there is a clustered index and a nonclustered index on the target table in this scenario), whereas theSELECT...INTO
does not. To mitigate this, you could potentially drop the indexes and recreate them after theMERGE
has run.Additional thoughts
All of this somewhat depends on what the breakdown (in terms of # of rows) is between
INSERT
s,UPDATE
s, andDELETE
s in theMERGE
statement, but in general I would expect theSELECT...INTO
case to be quicker unless theMERGE
statement has been designed very carefully.Some related information that might be interesting to you and this situation:
Sidebar: in a comment, you mentioned:
To compare "apples to apples" make sure to include the time it takes to create the two indexes you mentioned for the
SELECT...INTO
case: