SQL优化-不存在自连接的地方
请提供有关如何优化此sql过程的建议。。。where not exists子选择是我的主要问题。有人告诉我,我可以获得40%的性能提升SQL优化-不存在自连接的地方,sql,sql-server,query-optimization,Sql,Sql Server,Query Optimization,请提供有关如何优化此sql过程的建议。。。where not exists子选择是我的主要问题。有人告诉我,我可以获得40%的性能提升 insert into #tmpTable (intID3,intID4,intID5,intID6, StaticDate,NewDate, [Description],Change,intSort) select distinct '' as intID3,
insert into #tmpTable (intID3,intID4,intID5,intID6,
StaticDate,NewDate,
[Description],Change,intSort)
select distinct
'' as intID3,
intID4,
intID5,
intID6,
@dteDate1 as StaticDate,
'' as NewDate,
'16 character str' as [Description],
0 as Movement,
0 as intSort
from #tmpTable j
where not exists (
select
1
from #tmpTable x
where
x.intID1 = j.intID1
and x.intID2 = j.intID2
and x.[Description] = '16 character str'
)
尝试下面的查询
INSERT INTO #tmpTable2
(intID3,intID4,intID5,intID6,
StaticDate,NewDate,
[Description],Change,intSort)
SELECT DISTINCT
'' as intID3,
j.intID4,
j.intID5,
j.intID6,
@dteDate1 as StaticDate,
'' as NewDate,
'16 character str' as [Description],
0 as Movement,
0 as intSort
FROM #tmpTable j
LEFT JOIN #tmpTable x
ON x.intID1 = j.intID1
AND x.intID2 = j.intID2
AND x.[Description] = '16 character str'
WHERE x.intID1 IS NULL
请发布执行计划、涉及的表的架构和它们的计数为什么不使用
WHERE j.[Description]!='16个字符的str'
?因为x和j不是同一条记录。我们将表本身连接起来,以确保没有与特定id匹配的特定文本的记录您在tmpTable上有任何唯一的id吗?Genius。。。我喜欢这种观点。。。但有一个问题……这不应该是右联接吗?@Demian这取决于您。假设您在表1中输入数据,您正试图在不输入重复记录的情况下将该数据插入表2中,根据脚本,输入表表1应该位于联接的左侧(左联接)。