在SQL Server中,合并和合并联接有什么不同?
发布于 2016-11-21 14:45:27
DML 是一种语句(数据操作语言)。
也称为UPSERT (更新-插入)。
它尝试根据您定义的条件将源(表/视图/查询)与目标(表/可更新视图)进行匹配,然后根据匹配结果向目标表的/in//插入/更新/删除行。
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 |
+---+----+合并连接是一种连接算法(例如,散列连接或嵌套循环)。
它的基础是首先根据连接条件对两个数据集进行排序(可能由于存在索引而已经排序),然后遍历排序的数据集并找到匹配项。
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)在SQL Server中,主键意味着聚集索引结构,这意味着表存储为B-Tree,按主键排序。

https://stackoverflow.com/questions/40713883
复制相似问题