2014-12-02 123 views
0

存儲過程超時,因爲太慢,可能是因爲它使用outer full joinSQL Server的連接 - 超時

錯誤

System.Data .SqlClient.SqlException(0x80131904):超時已過期。在操作完成之前已經過去了 超時時間,或者 服務器沒有響應。 ---> System.ComponentModel.Win32Exception (0X80004005):等待操作在 System.Data.SqlClient.SqlConnection.OnError(SqlException異常,布爾 breakConnection

根據我的研究超時我認爲增加超時可能會解決錯誤問題,但我想減少執行所需的時間,任何改進都會受到歡迎。如果需要,我會發布表結構,但這樣做會很多。另外,我不確定是否增加時間out是最好的解決方案

/* Stored procedure to get fruits checks that the user has access to*/ 
ALTER procedure [dbo].[Fruits_GetChecksForUser] 
(
    @UserID VARCHAR(200), 
    @CheckUrlFilter varchar(256) 
) 
AS 

SELECT Distinct 
    Fruits_Checks.*, 
    ManagementCommittees.Title As CommitteeName, 
    FruitCategories.ID As CategoryID, 
    FruitCategories.Title As CategoryName, 
    dbo.IsUserInGroup(@UserID, FruitCategories.FruitOfficerGroupID) AS IsUserFruitOfficer, 
    dbo.IsUserInGroup(@UserID, Fruits_Checks.AllocatedGroup) AS IsUserCheckResponsible, 
    Policies.CompanyID, 
    Policies.ID As PolicyID 

FROM Groups_UserAccess 
INNER JOIN FruitCategories_GroupAccess ON FruitCategories_GroupAccess.GroupID = Groups_UserAccess.GroupID 
INNER JOIN FruitCategories ON FruitCategories.ID = FruitCategories_GroupAccess.FruitCategoryID 
INNER JOIN Policies ON Policies.ID = FruitCategories.PolicyID 
LEFT JOIN Policies_AppSettings ON Policies_AppSettings.PolicyID = Policies.ID 
LEFT JOIN Clients_AppSettings ON Clients_AppSettings.ID = Policies.CompanyID 
FULL OUTER JOIN Fruits ON Fruits.CategoryID = FruitCategories.ID 
INNER JOIN Fruits_Checks ON Fruits_Checks.FruitID = Fruits.ID 
INNER JOIN ManagementCommittees on Fruits_Checks.CommitteeID = ManagementCommittees.ID 

WHERE 
    Fruits.ID IS NOT NULL 
    AND Fruits_Checks.URL LIKE '%' + @CheckUrlFilter + '%' 

ORDER BY Fruits_Checks.EndDate 

回答

0

增加時間肯定不是 一個辦法。它看起來像是必要/可避免的完全外部連接,不同的指令和可能的內聯函數調用(可以很好地查看它們)緩慢地避免查詢drammaticaly。 至少需要執行計劃才能爲您提供更詳細的答案。

+0

我改變了執行計劃,它似乎幫助我,謝謝:),一旦我對測試滿意,我也會讚揚你 – Mathematics 2014-12-02 11:50:44