简体   繁体   English

在存储过程中使用if else语句时,为什么查询需要更多时间执行?

[英]Why query takes more time to execute when I use if else statement in stored procedure?

I have used below query in my stored procedure. 我在存储过程中使用了以下查询。 When I am using if else statement in the stored procedure query takes more time to execute. 当我在存储过程查询中使用if else语句时,将花费更多时间来执行。 When I remove if else statement query takes less time to return result. 当我删除if else语句查询时,只需较少的时间即可返回结果。

If @P_Make = 'ALL' Or IsNull(@P_Make, '') = ''
Begin
    Set @P_Make = '%';
End
Else
Begin
    Set @P_Make = @P_Make + '%';
End 
If @P_Model = 'ALL' Or IsNull(@P_Model, '') = ''
Begin
    Set @P_Model = '%';
End
Else
Begin
    Set @P_Model = @P_Model + '%';
End     
If @P_Location = 'ALL' Or IsNull(@P_Location, '') = ''
Begin
    Set @P_Location = '%';
End
Else
Begin
    Set @P_Location = @P_Location + '%';
End
If @P_City = 'ALL' Or IsNull(@P_City, '') = ''
Begin
    Set @P_City = '%';
End
Else
Begin
    Set @P_City = @P_City + '%';
End
If @P_Category = 'ALL' Or IsNull(@P_Category, '') = ''
Begin
    Set @P_Category = '%';
End
Else
Begin
   Set @P_Category = Case When @P_Category = 'Bikes & Scooters' Then '2W%'
                             When @P_Category = '3 Wheelers' Then '3W%'
                             When @P_Category = 'Cars & SUVs' Then '4W%'
                             When @P_Category = 'Trucks' Then 'CV%'
                             When @P_Category = 'Farm Equipments' Then 'FE%'
                             When @P_Category = 'Industrial Equipments' Then 'IE%'
                             When @P_Category = 'Construction Equipments' Then 'CE%'
                             Else @P_Category + '%'
                        End
    End
If @P_Service = 'ALL' Or IsNull(@P_Service, '') = ''
Begin
    Set @P_Service = ''
End

Select Count(C.Sal_Pk_Id) 
From dbo.Auction (NoLock) A
Inner Join dbo.PLACE (NoLock) B On A.Auc_Place_Fk_Id = B.Place_Pk_Id
Inner Join dbo.SALES_DETAILS (NoLock) C On A.Auc_Code = C.Sal_Auc_Code
Inner Join dbo.AUCTIONSERVICES (NoLock) D On C.Sal_Regno = D.Auc_Service_Regno And C.Sal_Auc_Code = D.Auc_Service_Auctioncode
Inner Join dbo.LOT (NoLock) E On C.Sal_Lot_No = E.Lot_Lot_No And C.Sal_Auc_Code = E.Lot_Auc_Code 
Inner Join dbo.INVENTORY (NoLock) F On C.Sal_Regno = F.Inv_H_Reg_No
Where C.[Status] = 'L' And E.Lot_Sold_Status = 'S'
And IsNull(B.Place_City, '') Like @P_City And IsNull(B.Place_State, '') Like @P_Location
And IsNull(F.Inv_H_Category, '') Like @P_Category And IsNull(F.Inv_H_Mfg_Name, '') Like @P_Make
And IsNull(F.Inv_H_Model, '') Like @P_Model

Query without if else statement: 没有if语句的查询:

Select Count(C.Sal_Pk_Id) 
From dbo.Auction (NoLock) A
Inner Join dbo.PLACE (NoLock) B On A.Auc_Place_Fk_Id = B.Place_Pk_Id
Inner Join dbo.SALES_DETAILS (NoLock) C On A.Auc_Code = C.Sal_Auc_Code
Inner Join dbo.AUCTIONSERVICES (NoLock) D On C.Sal_Regno = D.Auc_Service_Regno And C.Sal_Auc_Code = D.Auc_Service_Auctioncode
Inner Join dbo.LOT (NoLock) E On C.Sal_Lot_No = E.Lot_Lot_No And C.Sal_Auc_Code = E.Lot_Auc_Code 
Inner Join dbo.INVENTORY (NoLock) F On C.Sal_Regno = F.Inv_H_Reg_No
Where C.[Status] = 'L' And E.Lot_Sold_Status = 'S'
And (@P_City = '' Or IsNull(B.Place_City, '') = @P_City)
And (@P_Location= '' Or IsNull(B.Place_State, '') = @P_Location)
And (@P_Category = '' Or IsNull(F.Inv_H_Category, '') = @P_Category)
And (@P_Make = '' Or IsNull(F.Inv_H_Mfg_Name, '') = @P_Make)
And (@P_Model = '' Or IsNull(F.Inv_H_Model, '') = @P_Model)

Why the query output takes less time when I remove if else statement in the stored procedure? 当我删除存储过程中的if else语句时,为什么查询输出需要更少的时间?

There's two main reasons - like and execution plan. 主要有两个原因- like和执行计划。

like '%something' is the slowest possible thing you can filter by - it means going row by row, reading the whole data in the column and doing a comparison. like '%something'是您可以过滤的最慢的东西-意味着逐行,读取列中的全部数据并进行比较。 It can't use any index seeks, only scans. 它不能使用任何索引查找,而只能使用扫描。

Second, you only get one execution plan, based on the first way the procedure is called. 其次,根据调用过程的第一种方式,您只会得到一个执行计划。 This pretty much guarantees that for any other input data, the performance will suffer. 这几乎可以保证,对于任何其他输入数据,性能都会受到影响。 In your second example, while the plan is again sub-optimal and dependent on the initial inputs, it doesn't completely ignore all the possible filters - it just optimizes based on statistics. 在您的第二个示例中,虽然计划再次不是最佳计划并且取决于初始输入,但它并未完全忽略所有可能的过滤器-它只是根据统计信息进行优化。

Dynamic filters aren't something nobody tried to solve before. 动态过滤器不是以前没有人尝试解决的问题。 Learn from the best: http://www.sommarskog.se/dyn-search.html 向最好的人学习: http : //www.sommarskog.se/dyn-search.html

I think the query without if else is faster because it does not have a Like operator 我认为没有其他查询的查询会更快,因为它没有Like运算符

Where C.[Status] = 'L' And E.Lot_Sold_Status = 'S'
And IsNull(B.Place_City, '') Like @P_City And IsNull(B.Place_State, '') Like @P_Location
And IsNull(F.Inv_H_Category, '') Like @P_Category And IsNull(F.Inv_H_Mfg_Name, '') Like @P_Make
And IsNull(F.Inv_H_Model, '') Like @P_Model

and I suppose that the second query can use indexes more efficiently. 并且我认为第二个查询可以更有效地使用索引。 The first and second queries are different, please look at Actual Execution Plan and you will realize why it happens. 第一个查询和第二个查询不同,请查看实际执行计划,您将了解为什么会这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM