2017-08-24 83 views
0

我有簡單的選擇語句,給我的結果大約一秒鐘。但是,如果我添加生成列'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 

這件作品我估計執行計劃:

enter image description here

+2

你錯過了括號嗎? 'OR'謂詞獨立於前面的'L.IndentifierGuid = q.QuoteGUID'。 –

+0

你也有兩個「L」別名......一個在子選擇中,另一個在左邊加一個左連接......這最多是令人困惑的。但基本上,該連接不能使用索引,所以它必須轉到聚集的主鍵進行測試。是的,你似乎缺少括號(在AND之後打開,並在ORDER之前關閉)。 – pmbAustin

+0

@丹Guzman你是對的。括號完成了這項工作。非常感謝你! – Oleg

回答

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 

其內容如下:返回WHERE條件1 2是真正的所有行,或條件3爲真。雖然您可能要選擇條件1爲真,以及條件2 3.如果這是您要表達的意思,您需要將條件2和3封裝在括號內。如果這是正確的,你可能會得到一個不同的查詢計劃。

您有NOLOCK提示滲透您的查詢,這告訴我,您可能需要更多的索引在這些表上。

我會檢查實際的計劃,看看估計是否與實際相符。

+0

括號是罪魁禍首。非常感謝你的解釋@Eli – Oleg