SQL查询需要一个多小时才能执行200k行

wou*_*ong 12 sql sql-server performance ssms

我有两个表,每个表有大约200,000行.我已经运行了下面的查询,运行一个多小时后仍然没有完成.对此有什么解释?

SELECT 
    dbo.[new].[colom1],
    dbo.[new].[colom2],
    dbo.[new].[colom3],
    dbo.[new].[colom4],  
    dbo.[new].[Value] as 'nieuwe Value',
    dbo.[old].[Value] as 'oude Value'
FROM dbo.[new]
JOIN dbo.[old] 
    ON dbo.[new].[colom1] = dbo.[old].[colom1] 
    and dbo.[new].[colom2] = dbo.[old].[colom2] 
    and dbo.[new].[colom3] = dbo.[old].[colom3] 
    and dbo.[new].[colom4] = dbo.[old].[colom4] 
where dbo.[new].[Value] <> dbo.[old].[Value]
Run Code Online (Sandbox Code Playgroud)

来自评论;

执行计划

表结构

Dav*_*itz 7

似乎对于单个列上的相等连接,正在过滤掉连接键中具有NULL值的行,但对于多个列上的连接不是这种情况.
结果,散列连接复杂度从O(N)变为O(N ^ 2).

================================================== ====================

在这种情况下,我想推荐一篇由Paul White撰写的关于类似问题的精彩文章- Hash Joins on Nullable Columns

================================================== ====================

我已经生成了这个用例的小模拟,我鼓励您测试您的解决方案.

create table mytab1 (c1 int null,c2 int null)
create table mytab2 (c1 int null,c2 int null)

;with t(n) as (select 1 union all select n+1 from t where n < 10)
insert into mytab1 select null,null from t t0,t t1,t t2,t t3,t t4

insert into mytab2 select null,null from mytab1

insert into mytab1 values (111,222);
insert into mytab2 values (111,222);
Run Code Online (Sandbox Code Playgroud)
select * from mytab1 t1 join mytab2 t2 on t1.c1 = t2.c1 and t1.c2 = t2.c2 
Run Code Online (Sandbox Code Playgroud)

对于OP查询,我们应该在任何连接键列中删除具有NULL值的行.

SELECT 
    dbo.[new].[colom1],
    dbo.[new].[colom2],
    dbo.[new].[colom3],
    dbo.[new].[colom4],  
    dbo.[new].[Value] as 'nieuwe Value',
    dbo.[old].[Value] as 'oude Value'
FROM dbo.[new]
JOIN dbo.[old] 
    ON dbo.[new].[colom1] = dbo.[old].[colom1] 
    and dbo.[new].[colom2] = dbo.[old].[colom2] 
    and dbo.[new].[colom3] = dbo.[old].[colom3] 
    and dbo.[new].[colom4] = dbo.[old].[colom4] 
where dbo.[new].[Value] <> dbo.[old].[Value]
    and dbo.[new].[colom1]  is not null
    and dbo.[new].[colom2]  is not null
    and dbo.[new].[colom3]  is not null
    and dbo.[new].[colom4]  is not null
    and dbo.[old].[colom1]  is not null
    and dbo.[old].[colom2]  is not null
    and dbo.[old].[colom3]  is not null
    and dbo.[old].[colom4]  is not null
Run Code Online (Sandbox Code Playgroud)