Is Merge and Merge join same in SQL Server?

我是研究僧i 提交于 2020-01-02 05:33:06

问题


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


回答1:


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)

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.

Understanding Merge Joins



来源:https://stackoverflow.com/questions/40713883/is-merge-and-merge-join-same-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!