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

时间:2016-12-23 13:11:40

标签: sql sql-server performance ssms

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

<ScrollSync>
  {({ clientHeight, clientWidth, onScroll, scrollHeight, scrollLeft, scrollTop, scrollWidth }) => {
    <AutoSizer onResize={this.onResize}>{
      ({ width, height }) => (
        <Grid {...alltheprops} onScroll={onScroll}/>
      )
    }</AutoSizer>
  }
</ScrollSync>

来自评论;

Execution plan

Table structure

2 个答案:

答案 0 :(得分: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);
select * from mytab1 t1 join mytab2 t2 on t1.c1 = t2.c1 and t1.c2 = t2.c2 

对于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

答案 1 :(得分:-1)

使用EXCEPT join,您只需要对那些已更改的值进行更大的HASH连接,速度更快:

/*
create table [new] ( colom1  int, colom2  int, colom3  int, colom4  int, [value]  int)
create table [old] ( colom1  int, colom2  int, colom3  int, colom4  int, [value]  int)

insert old values (1,2,3,4,10)
insert old values (1,2,3,5,10)
insert old values (1,2,3,6,10)
insert old values (1,2,3,7,10)
insert old values (1,2,3,8,10)
insert old values (1,2,3,9,10)


insert new values (1,2,3,4,11)
insert new values (1,2,3,5,10)
insert new values (1,2,3,6,11)
insert new values (1,2,3,7,10)
insert new values (1,2,3,8,10)
insert new values (1,2,3,9,11)
*/

select n.colom1, n.colom2 , n.colom3, n.colom4, n.[value] as newvalue, o.value as oldvalue
from new n
inner join [old] o on n.colom1=o.colom1 and n.colom2=o.colom2 and n.colom3=o.colom3 and n.colom4=o.colom4
inner join 
(
select colom1, colom2 , colom3, colom4, [value] from new
except
select colom1, colom2 , colom3, colom4, [value] from old
) i on n.colom1=i.colom1 and n.colom2=i.colom2 and n.colom3=i.colom3 and n.colom4=i.colom4