我有一个简单的select语句,它给我的结果大约一秒钟。但是如果我添加生成列'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
我的预计执行计划:
答案 0 :(得分: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
个提示,告诉我您可能需要对这些表进行更多索引。
我会查看实际计划,看看估算是否与实际值相同。