Catwoman Catwoman - 2 months ago 22
SQL Question

Is Merge and Merge join same in SQL Server?

What is the difference between Merge and a Merge Join in SQL Server?


MERGE is a DML statement (data manipulation language).
Also called UPSERT (Update-Insert).
It tries to match source (table / view / query) to a target (table / updatable view) based on your defined conditions and then based on the matching results it insert/update/delete rows to/in/of the target table.
MERGE (Transact-SQL)

create table src (i int, j int);
create table trg (i int, j int);

insert into src values (1,1),(2,2),(3,3);
insert into trg values (2,20),(3,30),(4,40);

merge into  trg
using       src
on          src.i = trg.i
when not matched by target then insert (i,j) values (src.i,src.j)
when not matched by source then update set trg.j = -1
when matched then update set trg.j = trg.j + src.j

select * from trg order by i

| i | j  |
| 1 | 1  |
| 2 | 22 |
| 3 | 33 |
| 4 | -1 |

MERGE JOIN is a join algorithm (e.g. HASH JOIN or NESTED LOOPS).
It is based on first sorting both datasets according to the join conditions (maybe already sorted due to index existent) and then traversing through the sorted datasets and finding matches.

create table t1 (i int)
create table t2 (i int)

select * from t1 join t2 on t1.i = t2.i option (merge join)

enter image description here

create table t1 (i int primary key)
create table t2 (i int primary key)

select * from t1 join t2 on t1.i = t2.i option (merge join)

In SQL Server a primary key implies clustered index structure which means the table is stored as a B-Tree, sorted by the primary key.

enter image description here

Understanding Merge Joins