zoukankan      html  css  js  c++  java
  • Merge:解析on子句和when not match子句的陷阱

    在细节上,体现编程的修养。每一位大师,master,其基础必定夯实。废话不多说,直接上干货,Merge子句用于对两个数据表执行数据同步,On子句指定匹配(when matched)条件,When子句指定额外的过滤条件和数据更新逻辑。源表(Source Table)和靶表(Targe Table)的数据行能够匹配成功,这意味着on子句和when match条件都被满足,进入到when matched子句定义的更新代码中,执行数据同步操作;如果不满足on子句,那么必须深入理解不匹配(when not matched)子句的条件,否则,很容易出错。首先查看MSDN对On子句的定义:

    ON <merge_search_condition>  Specifies the conditions on which source_table_ is joined with target_table to determine where they match.

    也就是说,如果两个数据行满足on子句条件,那么数据处理程序跳转到when matched子句;如果两个数据行不满足on子句,那么数据处理程序跳转到when not matched子句。如果在on子句中只指定源表列和靶表列之间的匹配关系,那么同步操作一般不会出现“意外"的问题,意外是指符合设计者的预期。一旦在on子句中试图过滤靶表或源表的数据行,那么,再执行数据同步可能出现异常结果,出现不符合设计者预期的行为。实际上,MSDN已经明确给出提示,不要忽略这个提示,不然,你很可能已经挖了坑而不自知:

    It is important to specify only the columns from the target table that are used for matching purposes. That is, specify columns from the target table that are compared to the corresponding column of the source table. Do not attempt to improve query performance by filtering out rows in the target table in the ON clause, such as by specifying AND NOT target_table.column_x = value. Doing so may return unexpected and incorrect results.

    在开始测试when not matched子句的陷进之前,使用以下脚本创建示例数据:

    create table dbo.dt_source
    (
        ID int,
        Code int
    )
    go
    create table dbo.dt_target
    (
        ID int,
        Code int
    )
    go
    insert into dbo.dt_source
    (
    ID,
    Code
    )
    values(1,1),(2,1),(3,2),(4,2),(5,0)
    GO
    insert into dbo.dt_target
    (
    ID,
    Code
    )
    values(1,1),(6,4)
    GO
    View Code

    一,在on子句中过滤源表

    1,在Merge的On子句中,使用额外的筛选条件(s.Code>0)对SourceTable进行过滤

    对源表进行过滤,初衷是为了将SourceTable中Code>0的数据作为数据源同步到TargetTable,但是,在Merge命令的On子句中,s.Code>0只是一个匹配条件,用于when matched子句;然而,对于when not matched子句,是指不满足on条件:t.id=s.id and s.Code>0 ,这意味着when not matched匹配的查询条件是: t.id<>s.id or s.ID<=0,表达的逻辑是:s.id 和任意一个 t.id 都不相等, 或 s.ID<=0,这使得源表dbo.dt_source中Code<=0的数据行都满足when not matched子句的条件,被插入到dbo.dt_target中。

    ;merge dbo.dt_target as t
    using dbo.dt_source as s
        on t.id=s.id and s.Code>0
    when matched
    then update
    set t.code=s.code
    when not matched
    then insert
    (
    ID,
    Code
    )
    values
    (
    s.ID,
    s.Code
    );

    查看TargetTable,Code=0的数据被插入到TargeTable表中,靶表的数据如下:

    2,正确的写法:不要试图在on子句中过滤源表

    在使用Merge命令同步数据时, 如果要过滤源表,正确的做法是把筛选条件放在所有的when子句中,包括When matched子句和when not matched子句。对on子句添加对源表的过滤条件,在when matched子句中,正常过滤源表,而在when not matched子句,会出现异常。

    ;merge dbo.dt_target as t
    using dbo.dt_source as s
        on t.id=s.id 
    when matched and s.Code>0 then update set t.code=s.code when not matched and s.Code>0 then insert ( ID, Code ) values ( s.ID, s.Code );

    二,在on子句中过滤靶表(Target Table)

    清空测试数据表,插入测试数据

    insert into dbo.dt_source
    (
    ID,
    Code
    )
    values(1,-1),(2,1),(3,2),(4,2),(5,0),(6,7)
    GO
    insert into dbo.dt_target
    (
    ID,
    Code
    )
    values(1,1),(6,4)
    GO
    View Code

    1,在on子句中对靶表进行过滤
    在on子句中指定匹配条件:on t.id=s.id and t.Code<4,指定的时when matched的匹配条件,对于when not matched子句,匹配条件是:t.id<>s.id or t.Code>=4,对于源表数据行Row(6,7),不满足t.id<>s.id,因为存在TargetTableRow(6,4),但是满足 or 的另外一个条件  t.Code>=4, 所以,when not matched语句逻辑结果是true,执行insert语句。

    ;merge dbo.dt_target as t
    using dbo.dt_source as s
        on t.id=s.id and t.Code<4
    when matched 
    then update
    set t.code=s.code
    when not matched 
    then insert
    (
    ID,
    Code
    )
    values
    (
    s.ID,
    s.Code
    );

    TargetTable的结果集如下图,包括(6,7)

    2,分析陷进

    这或许是你想要的结果,或许,你的本意是不希望 t.Code>=4的数据行插入到靶表中,如果merge子句要实现的业务逻辑是后者,那么数据同步将出现异常,所以一定要深刻理解when not matched子句的匹配条件,推荐的做法是:不要试图在on子句中过滤源表或靶表,如果必须要过滤数据行,那么请在每个when子句(when matched和when not matched)中,添加额外的and 过滤条件。

  • 相关阅读:
    Tomcat的配置
    读《大道至简》总结
    manjaro安装minishift
    minishift 部署mysql持久化
    从大神到小白之路minishift 命令补全
    Typora 完美结合 PicGo,写作体验更佳!
    40 个笑到抽筋的神回复,绝了!
    如何写出让同事好维护的代码?
    Google的面试题长啥样?看完被吊打
    Mac 超详细入门指南,备用!
  • 原文地址:https://www.cnblogs.com/ljhdo/p/5033612.html
Copyright © 2011-2022 走看看