我有簡單的選擇語句,給我的結果大約一秒鐘。但是,如果我添加生成列'Sent to UW on'
的子選擇語句,那麼性能會顯着下降。 我想原因是正是這樣的說法:在sub select語句中連接會顯着降低查詢性能。 SQL 2012
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之前關閉)。 – pmbAustin
@丹Guzman你是對的。括號完成了這項工作。非常感謝你! – Oleg