首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并和合并联接在SQL Server中是相同的吗?

合并和合并联接在SQL Server中是相同的吗?
EN

Stack Overflow用户
提问于 2016-11-21 14:16:36
回答 1查看 12.5K关注 0票数 5

在SQL Server中,合并和合并联接有什么不同?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-21 14:45:27

DML 是一种语句(数据操作语言)。

也称为UPSERT (更新-插入)。

它尝试根据您定义的条件将源(表/视图/查询)与目标(表/可更新视图)进行匹配,然后根据匹配结果向目标表的/in//插入/更新/删除行。

MERGE (Transact-SQL)

代码语言:javascript
复制
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 |
+---+----+

合并连接是一种连接算法(例如,散列连接或嵌套循环)。

它的基础是首先根据连接条件对两个数据集进行排序(可能由于存在索引而已经排序),然后遍历排序的数据集并找到匹配项。

代码语言:javascript
复制
create table t1 (i int)
create table t2 (i int)

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

代码语言:javascript
复制
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,按主键排序。

Understanding Merge Joins

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40713883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档