【问题标题】:Concatenating in sub select statement slows down query performance significantly . SQL 2012在子选择语句中连接会显着降低查询性能。 SQL 2012
【发布时间】:2017-08-24 18:12:41
【问题描述】:

我有一个简单的选择语句,它给我大约一秒钟的结果。但是,如果我添加生成列'Sent to UW on' 的子选择语句,那么性能会显着降低。 我认为原因就在这句话中:

OR L.Action = CONCAT('Conrol #',Q.ControlNo, '. Change Quote Status Reason from ''None'' to ''Under Review''') 

还有其他方法可以重写这个select 语句吗?

SELECT   
    ControlNo , 
   PolicyNumber , 
   l.LineName , 
   InsuredPolicyName , 
   DisplayStatus , 
   U.UserName as Underwriter , 
   u.EmailAddress as 'Underwriter Email' , 
   rtrim(ltrim(PC.[FName])) + ' ' + rtrim(ltrim(PC.[LName])) as Broker , 
   PL.Name , 
   q.DateBound , 
   q.EffectiveDate , 
   (  
    SELECT TOP 1 L.ActionDate  
    FROM tblLog L  
    WHERE L.IndentifierGuid = q.QuoteGUID  
    AND L.Action = 'Reason for Quote status change: Under Review' 
--The line below slows down the performance  
    OR L.Action = CONCAT('Conrol #',Q.ControlNo, '. Change Quote Status Reason from ''None'' to ''Under Review''')  
   ORDER BY L.ActionDate asc  
   ) as 'Sent to UW on'  
FROM        tblQuotes Q  WITH  ( nolock )  
JOIN        tblUsers U  WITH  ( nolock )  ON underwriteruserguid = u.userGUID  
LEFT JOIN   lstlines L  WITH  ( nolock )  ON L.LineGUID = Q.lineguid  
LEFT JOIN   lstQuoteStatusReasons QSR  WITH  ( nolock )  ON QSR.ID = Q.QuoteStatusReasonID  
LEFT JOIN   [MEJAMES].[dbo].[tblProducerContacts] PC  WITH  ( nolock )  ON pc.ProducerContactGUID = q.ProducerContactGuid  
LEFT JOIN   MEJAMES.DBO.tblProducerLocations PL  WITH  ( nolock )  ON PC.ProducerLocationGUID = PL.ProducerLocationGUID  
LEFT JOIN   [MEJAMES].[dbo].[tblProducerContacts] PC_Asst  WITH  ( nolock )  ON PC_Asst.ProducerContactGUID = q.SecProducerContactGuid  
WHERE       q.lineGUID in( 'D4983D4A-1D12-461D-8837-6092DC74325B', 'CF144437-F128-4B77-AC19-877247347D02' , 'E05E7F4A-07C4-4981-BD13-2461D4EE4BF3')  
   /* EQ and Wind and Terrorism LOBs  */  
    AND q.OriginalQuoteGUID is null  
    AND Q.QuoteStatusID = 3  
    AND Q.EffectiveDate > '5-1-2017'  
ORDER BY q.ControlNo  

我的预计执行计划:

【问题讨论】:

  • 您是否缺少括号? OR 谓词独立于前面的 L.IndentifierGuid = q.QuoteGUID
  • 您还有两个“L”别名......一个在子选择中,一个作为左连接......这充其量是令人困惑的。但基本上,那个连接不能使用索引,所以它必须去聚集的主键做测试。是的,您似乎缺少括号(在 AND 之后打开,在 ORDER 之前关闭)。
  • @Dan Guzman 你是对的。括号完成了这项工作。非常感谢!

标签: sql-server tsql sql-server-2012


【解决方案1】:

我发现这里有一些问题 - 解决它们应该会对您有所帮助。
丹·古兹曼 (Dan Guzman) 的评论中存在(假设的)缺少括号的问题。
你有(编号我的):

WHERE L.IndentifierGuid = q.QuoteGUID   --1
    AND L.Action = 'Reason for Quote status change: Under Review'  --2
--The line below slows down the performance  
    OR L.Action = CONCAT('Conrol #',Q.ControlNo, '. Change Quote Status Reason from ''None'' to ''Under Review''')   --3

其内容如下:返回条件 1 AND 2 为真或条件 3 为真的所有行。虽然您可能打算选择条件 1 为真,以及条件 2 OR 3。如果这是您要表达的意思,则需要将条件 2 和 3 封装在括号中。如果这是正确的,您可能会得到不同的查询计划。

您的查询中有NOLOCK 提示,这告诉我您可能需要对这些表进行更多索引。

我会检查实际计划,看看估算值是否与实际值一致。

【讨论】:

  • 括号是罪魁祸首。非常感谢您的解释@Eli
猜你喜欢
  • 1970-01-01
  • 2014-01-12
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 2012-04-18
  • 1970-01-01
相关资源
最近更新 更多