Sql server 条件太多时查询速度慢

Sql server 条件太多时查询速度慢,sql-server,azure-sql-server,Sql Server,Azure Sql Server,我有一个简单的疑问: SELECT distinct top 100 A.[Number] FROM [Section] AS A LEFT JOIN [Customers] AS c ON c.ARef = A.Number LEFT JOIN [Guides] AS G ON G.CustomerId = c.CustomerId LEFT JOIN [Telephones] AS T ON T.CustomerId = c.CustomerId LE

我有一个简单的疑问:

SELECT distinct top 100  A.[Number]
FROM [Section] AS A
    LEFT JOIN [Customers] AS c  ON c.ARef = A.Number
    LEFT JOIN [Guides] AS G  ON G.CustomerId = c.CustomerId
    LEFT JOIN [Telephones] AS T  ON T.CustomerId = c.CustomerId
    LEFT JOIN [Epts] AS E  ON E.CustomerId = c.CustomerId
    LEFT JOIN [Emails] AS Em  ON Em.CustomerId = c.CustomerId
    LEFT JOIN [Addresses] AS Ad ON Ad.CustomerId=C.CustomerId
WHERE 
 A.SaloonId= 400
 AND (         
    A.Number= @term OR c.Surname = @term OR c.FirstName = @term 
    ----
    OR Ad.Postcode = @term
    OR G.CategoryRef= @term 
    OR T.PhoneNumber = @term 
    OR E.Code= @term 
    OR Em.EmailAddress = @term 
 ) 
where部分中包含的所有字段都具有非常低的碎片百分比的索引


如果我们对一个术语执行查询,需要20秒以上,但是如果我在后面的最后一节中删除任何随机行,则需要不到1秒的时间

我会尝试将这些条件移动到它们各自的联接中,然后在WHERE中将它们替换为或Ad.CustomerId不为NULL之类的条件。这将减少连接匹配,并减少必须根据这些连接条件进行计算的中间结果的数量

另一方面,由于大多数表中的数据实际上没有返回到结果中,所以我会考虑替换像

之类的东西。
...
LEFT JOIN [Guides] AS G  ON G.CustomerId = c.CustomerId 
... 
WHERE 
... 
OR G.CategoryRef = @term
使用或c.CustomerId从指南中选择CustomerId作为G,其中G.CategoryRef=@term


如果A上的标准显著减少了C的数量,我甚至会考虑使用从GuangyRef= @术语和G.CuuleIdID=CuCuuldID

< P>的相关子查询,比如“存在”或“选择”*作为G,我将尝试将这些条件移到它们各自的连接上,然后将它们替换在条件类似或Ad.CustomerId不为NULL的地方。这将减少连接匹配,并减少必须根据这些连接条件进行计算的中间结果的数量

另一方面,由于大多数表中的数据实际上没有返回到结果中,所以我会考虑替换像

之类的东西。
...
LEFT JOIN [Guides] AS G  ON G.CustomerId = c.CustomerId 
... 
WHERE 
... 
OR G.CategoryRef = @term
使用或c.CustomerId从指南中选择CustomerId作为G,其中G.CategoryRef=@term


如果A上的标准显著减少了C的数量,我甚至会考虑使用相关的子查询,比如从GuestyRef= @术语和G.CuuleIDID=CuCuCuIDID

,从GueSythyReF=@ Type和G.CuuleIDID

,当然,您已经查看了执行计划,并且您有适当的索引……当然,您已经查看了执行计划,并且您有适当的索引…