gpt4 book ai didi

SQL 完全连接表的条件

转载 作者:行者123 更新时间:2023-12-02 03:12:28 25 4
gpt4 key购买 nike

我有同一个数据集的两个版本,我需要做一个完整的连接来找到其中一个丢失的记录,两个版本都有一些丢失的记录。我设法想出了两种方法,但都有缺点。我的数据集大小和过滤条件都非常大。

解决方案 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%,尽管它的执行计划较小。如果可能的话,我想要没有代码重复的更快的解决方案(不一定像我在问题中所说的那样具有更小的执行计划)。

Edit2:抱歉误导了第一次编辑,我不擅长优化 :) 我在大约 150 万行集上测试了这个,解决方案 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

最佳答案

这应该有一个很好的计划。 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)

编辑做了一些测试。上面的查询比其他 2 个版本有更好的 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.

关于SQL 完全连接表的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39141061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com