将[docSVsys]加入[docSVsysReturn]会查询查询
三个条件连接正在杀死它 - 它变成一个循环连接
这需要2分钟以上
如果我取出或([docSVsysReturn]。[sParID] = [coreCTE]。[sParID]和[docSVsysReturn]。[sParID] = [docSVsysReturn]。[sID])它在1秒内运行
我尝试了合并和散列连接,但不允许使用它们
涉及的列被编入索引
[sID]是PK,[sParID]是必需的,FK是[sID]
有关如何修复性能的任何想法?
; WITH coreCTE AS (
select [docSVsysAuth].[sID], [docSVsysAuth].[sParID]
from [docSVsys] as [docSVsysAuth] with (nolock)
join [docSVtext] as [Table] with (nolock)
on [Table].[sID] = [docSVsysAuth].[sID]
and [docSVsysAuth].[visibility] in (0)
and [Table].[fieldID] = '108'
where [table].[value] = 'Andy Wipper<andy.wipper@company.com>'
)
, [cteFinal] AS (
select distinct [docSVsysReturn].[sID], [docSVsysReturn].[sParID]
from [coreCTE]
join [docSVsys] as [docSVsysReturn] with (nolock)
on ( [docSVsysReturn].[sID] = [coreCTE].[sID]
or
( [docSVsysReturn].[sParID] = [coreCTE].[sParID] and [docSVsysReturn].[sParID] = [docSVsysReturn].[sID] )
)
and [docSVsysReturn].[visibility] in (0)
)
select [cteFinal].[sID], [cteFinal].[sParID]
from [cteFinal]
order by [cteFinal].[sID], cteFinal.[sParID]
这有点帮助
on ( [docSVsysReturn].[sID] = [coreCTE].[sID]
or
( [docSVsysReturn].[sID] <> [coreCTE].[sID] and [docSVsysReturn].[sParID] = [coreCTE].[sParID] and [docSVsysReturn].[sParID] = [docSVsysReturn].[sID] )
)
答案 0 :(得分:1)
我建议您尝试使用UNION
编写查询。尝试一下。
select [docSVsysReturn].[sID], [docSVsysReturn].[sParID]
from [coreCTE]
join [docSVsys] as [docSVsysReturn] with (nolock)
on [docSVsysReturn].[sID] = [coreCTE].[sID]
and [docSVsysReturn].[visibility] in (0)
UNION
select [docSVsysReturn].[sID], [docSVsysReturn].[sParID]
from [coreCTE]
join [docSVsys] as [docSVsysReturn] with (nolock)
on [docSVsysReturn].[sParID] = [coreCTE].[sParID]
and [docSVsysReturn].[sParID] = [docSVsysReturn].[sID]
and [docSVsysReturn].[visibility] in (0)
答案 1 :(得分:0)
从不同的角度来看,固定它 基本上把OR放在游戏后面
; WITH coreCTE AS (
select distinct [docSVsysAuth].[sID], [docSVsysAuth].[sParID]
from [docSVsys] as [docSVsysAuth] with (nolock)
join [docSVtext] as [Table] with (nolock)
on [Table].[sID] = [docSVsysAuth].[sID]
and [docSVsysAuth].[visibility] in (0)
and [Table].[fieldID] = '108'
where (( [table].[value] = 'Andy Wipper<andy.wipper@company.com>' )
)
)
, [cteFinal] AS (
select distinct [docSVsysReturn].[sID], [docSVsysReturn].[sParID]
from [coreCTE]
join [docSVsys] as [docSVsysReturn] with (nolock)
on ( [docSVsysReturn].[sParID] = [coreCTE].[sParID]
and
( [docSVsysReturn].[sID] = [coreCTE].[sID] -- the document
or ( [docSVsysReturn].[sParID] = [docSVsysReturn].[sID] -- it is a parent
and [coreCTE].[sParID] <> [coreCTE].[sID] -- already have the parent
and [docSVsysReturn].[visibility] in (0)
) -- or the parent
)
)
)
select [cteFinal].[sID], [cteFinal].[sParID] -- ,bulkUpField
from [cteFinal]
order by [cteFinal].[sID], cteFinal.[sParID]