具有多个连接的水果检查的SQL Server存储过程 - 超时

时间:2014-12-02 10:16:24

标签: sql performance sql-server-2008 stored-procedures

存储过程由于速度过慢而超时,可能是因为它使用outer full join

错误

  

System.Data.SqlClient.SqlException(0x80131904):超时已过期。该   在完成操作或之前经过的超时时间   服务器没有响应。 ---> System.ComponentModel.Win32Exception   (0x80004005):等待操作超时   System.Data.SqlClient.SqlConnection.OnError(SqlException异常,   Boolean breakConnection

根据我的研究,我认为增加时间可能会解决错误问题,但我希望减少执行所需的时间,任何改进都会受到欢迎。如果需要,我会发布表结构,但这将是很多。此外,我不确定增加超时是否是最佳解决方案

/* 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

1 个答案:

答案 0 :(得分:0)

增加时间肯定不是解决方案。它看起来像不必要/可避免的完全外连接,不同的指令和可能的内联函数调用(也可以很好地查看它们)慢慢地让你的查询drammaticaly。 至少需要一个执行计划来为您提供更详细的答案。