tsql - Slow join on multiple conditions -
that join [docsvsys] [docsvsysreturn] killing query
3 condition join killing - turn loop join
take on 2 minutes
if take out or( [docsvsysreturn].[sparid] = [corecte].[sparid] , [docsvsysreturn].[sparid] = [docsvsysreturn].[sid] ) runs in 1 second s tried merge , hash join not allowed
columns involved indexed
[sid] pk , [sparid] required , fk [sid]
thoughts on how fix performance?
; corecte ( select [docsvsysauth].[sid], [docsvsysauth].[sparid] [docsvsys] [docsvsysauth] (nolock) join [docsvtext] [table] (nolock) on [table].[sid] = [docsvsysauth].[sid] , [docsvsysauth].[visibility] in (0) , [table].[fieldid] = '108' [table].[value] = 'andy wipper<andy.wipper@company.com>' ) , [ctefinal] ( select distinct [docsvsysreturn].[sid], [docsvsysreturn].[sparid] [corecte] join [docsvsys] [docsvsysreturn] (nolock) on ( [docsvsysreturn].[sid] = [corecte].[sid] or ( [docsvsysreturn].[sparid] = [corecte].[sparid] , [docsvsysreturn].[sparid] = [docsvsysreturn].[sid] ) ) , [docsvsysreturn].[visibility] in (0) ) select [ctefinal].[sid], [ctefinal].[sparid] [ctefinal] order [ctefinal].[sid], ctefinal.[sparid]
this helped bit
on ( [docsvsysreturn].[sid] = [corecte].[sid] or ( [docsvsysreturn].[sid] <> [corecte].[sid] , [docsvsysreturn].[sparid] = [corecte].[sparid] , [docsvsysreturn].[sparid] = [docsvsysreturn].[sid] ) )
i suggest try write query union
instead. try out.
select [docsvsysreturn].[sid], [docsvsysreturn].[sparid] [corecte] join [docsvsys] [docsvsysreturn] (nolock) on [docsvsysreturn].[sid] = [corecte].[sid] , [docsvsysreturn].[visibility] in (0) union select [docsvsysreturn].[sid], [docsvsysreturn].[sparid] [corecte] join [docsvsys] [docsvsysreturn] (nolock) on [docsvsysreturn].[sparid] = [corecte].[sparid] , [docsvsysreturn].[sparid] = [docsvsysreturn].[sid] , [docsvsysreturn].[visibility] in (0)