SQL带条件的全外连接

3

我有两个相同数据集的版本,需要进行全连接以查找其中一个缺失的记录,两个版本都有一些缺失的记录。我已经想出了两种方法来做到这一点,但是两种方法都有缺点。我的数据集大小和过滤条件非常大。

解决方案1的缺点是使用CTE,这将分割过滤器并使代码更难阅读,我希望只有一个查询:

create table #temp (id int, vers nvarchar(1))
insert into #temp select 1,'a' union select 2,'a' union select 3,'a'
              union select 1,'b' union select 2,'b' union select 100,'b'

;WITH vers_a as (SELECT * FROM #temp WHERE vers = 'a')
,vers_b as (SELECT * FROM #temp WHERE vers = 'b')

SELECT ta.id, tb.id, ta.vers, tb.vers
FROM vers_a ta
FULL JOIN vers_b tb on ta.id = tb.id
WHERE ta.id is null or tb.id is null

drop table #temp

解决方案2复制了过滤器,执行计划更大:
create table #temp (id int, vers nvarchar(1))
insert into #temp select 1,'a' union select 2,'a' union select 3,'a'
              union select 1,'b' union select 2,'b' union select 100,'b'

SELECT ta.id, tb.id, ta.vers, tb.vers
FROM #temp ta
FULL JOIN #temp tb on ta.id = tb.id and ta.vers = 'a' and tb.vers = 'b'
WHERE (ta.id is null or tb.id is null) and (ta.vers = 'a' or tb.vers = 'b')

drop table #temp

所以我的问题是,是否可能像解决方案2一样,但没有双重条件定义,并且执行计划比解决方案1更小?
编辑:在单个查询中运行两个解决方案时,我发现解决方案2的成本为26%,而解决方案1的成本为45%,尽管它具有更小的执行计划。如果可能的话,我希望使用更快的解决方案(不一定要像我在问题中说的那样具有更小的执行计划),而无需代码重复。
编辑2:很抱歉误导了第一个编辑版本,我不擅长优化:)我在约1.5百万行数据集上进行了测试,结果表明解决方案1更快。要获取数据集,请使用以下内容:
create table #temp (id int, vers nvarchar(1))
insert into #temp select 1,'a' union select 2,'a' union select 3,'a'
              union select 1,'b' union select 2,'b' union select 100,'b'
while (select count(*) from #temp) < 1000000
begin
    insert into #temp select id+ABS(CHECKSUM(NewId()))%10000, vers from #temp
end

你可以尝试在这两个查询上使用EXCEPT,看看你期望得到的输出是什么? - NickyvV
预期输出是任何解决方案的结果,除了我理解为两个查询(一个完整的-要排除的内容),这太慢了。 - Alex_404
@AndrewDeighton 在 OP 给出的示例中,id 不是唯一的。 - Thinkeye
@thinkeye,谢谢,我现在明白它的作用了。 - Cato
2个回答

1
这应该有一个好的计划。在vers上建立索引可能会有帮助。
SELECT ta.id, tb.id, ta.vers, tb.vers
FROM (SELECT * FROM #temp WHERE vers = 'a') ta
FULL JOIN (SELECT * FROM #temp WHERE vers = 'b') tb on ta.id = tb.id 
WHERE (ta.id is null or tb.id is null) 

编辑 做了一些测试。上述查询具有比其他两个版本更好的CPU。

-- SETUP
drop table temp;
go

create table temp (
   id int 
  ,vers nvarchar(1));

insert temp(id,vers)
select top(100000)
    row_number() over(order by (select null)) / 2  
  , case ABS(CHECKSUM(NewId())) % 2 when 0 then 'a' else 'b' end
from sys.all_objects t, sys.all_objects t1 ;


create index idx_temp_vers on temp(vers) include(id)
 with
  fillfactor=90;

select top(50) *
from temp;

-- TEST RUNS 
SET STATISTICS TIME ON;

print ' 1 index query 1 '
SELECT ta.id, tb.id, ta.vers, tb.vers
FROM (SELECT * FROM temp WHERE vers = 'a') ta
FULL JOIN (SELECT * FROM temp WHERE vers = 'b') tb on ta.id = tb.id 
WHERE (ta.id is null or tb.id is null)
;
print ' 1 index query 2 '
SELECT ta.id, tb.id, ta.vers, tb.vers
FROM temp ta
FULL JOIN temp tb on ta.id = tb.id and ta.vers = 'a' and tb.vers = 'b'
WHERE (ta.id is null or tb.id is null) and (ta.vers = 'a' or tb.vers = 'b')
;
print ' 1 index query 3 '
SELECT ta.id, TA.vers 
  from temp ta 
  where ta.vers = 'a' 
         and TA.id NOT IN(SELECT tb.id FROM temp tb WHERE tb.vers = 'b')
UNION ALL 
SELECT tb.id, Tb.vers 
  from temp tb 
  where tb.vers = 'b' 
         and Tb.id NOT IN(SELECT ta.id FROM temp ta WHERE ta.vers = 'a')

-- One more index
create index idx_temp_id on temp(id)
 with
  fillfactor=90;


print ' 2 indexes query 1 '
SELECT ta.id, tb.id, ta.vers, tb.vers
FROM (SELECT * FROM temp WHERE vers = 'a') ta
FULL JOIN (SELECT * FROM temp WHERE vers = 'b') tb on ta.id = tb.id 
WHERE (ta.id is null or tb.id is null)
;
print ' 2 indexes query 2 '
SELECT ta.id, tb.id, ta.vers, tb.vers
FROM temp ta
FULL JOIN temp tb on ta.id = tb.id and ta.vers = 'a' and tb.vers = 'b'
WHERE (ta.id is null or tb.id is null) and (ta.vers = 'a' or tb.vers = 'b')
;
print ' 2 indexes query 3 '
SELECT ta.id, TA.vers 
  from temp ta 
  where ta.vers = 'a' 
         and TA.id NOT IN(SELECT tb.id FROM temp tb WHERE tb.vers = 'b')
UNION ALL 
SELECT tb.id, Tb.vers 
  from temp tb 
  where tb.vers = 'b' 
         and Tb.id NOT IN(SELECT ta.id FROM temp ta WHERE ta.vers = 'a')


SET STATISTICS TIME OFF;

结果
 1 index query 1 
(49898 row(s) affected)
 SQL Server Execution Times:
   CPU time = 156 ms,  elapsed time = 3825 ms.

 1 index query 2 
(49898 row(s) affected)
 SQL Server Execution Times:
   CPU time = 281 ms,  elapsed time = 2962 ms.

 1 index query 3 
(49898 row(s) affected)
 SQL Server Execution Times:
   CPU time = 422 ms,  elapsed time = 2508 ms.

 2 indexes query 1 
(49898 row(s) affected)
 SQL Server Execution Times:
   CPU time = 172 ms,  elapsed time = 2679 ms.

 2 indexes query 2 
(49898 row(s) affected)
 SQL Server Execution Times:
   CPU time = 406 ms,  elapsed time = 3468 ms.

 2 indexes query 3 
(49898 row(s) affected)
 SQL Server Execution Times:
   CPU time = 407 ms,  elapsed time = 3728 ms.

我已经用解决方案2运行了它,对于这个问题,我得到了45%的查询成本(与解决方案1相同,基本上是将CTE移动到查询中的相同查询),并且对于解决方案2得到了26%。 我不知道为什么 :( - Alex_404
查看测试结果 - Serg
谢谢,我也得到了差不多的结果,我猜我只能为了更快的查询而忍受阅读困难的代码。 - Alex_404

0

这样怎么样,也避免返回包含空值的列

SELECT ta.id, TA.vers from #temp ta 
                    where ta.vers = 'a' 
                            and TA.id NOT IN(SELECT tb.id FROM #temp tb WHERE tb.vers = 'b')
UNION ALL 
SELECT tb.id, Tb.vers from #temp tb 
                    where tb.vers = 'b' 
                            and Tb.id NOT IN(SELECT ta.id FROM #temp ta WHERE ta.vers = 'a')

这对小数据集来说还好,但是对于大数据集,NOT IN (<query>) 非常低效。我测试了约 150 万行数据,在运行 5 分钟后取消了测试。 - Alex_404

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接