Parameter Sniffing

What is the Parameter Sniffing

•       Parametersniffing is the process whereby  SQL Server creates an optimal planfor a stored procedure by using the calling parameters that are passed thefirst time a stored procedure is executed. 

•       Everysubsequent call to the same store procedure with the same parameters will alsoget an optimal plan, whereas calls with different parameter values maynot always get an optimal plan.  

Solve the problem of one case

A stored procedure usp_LMS_Reports_ListExceptionsByUsertakes long time to complete. Can you check why?

1.    Search the procedure name in the trace file.

·        First

·        Second



·        Third


PS: There is a misleading pointthat what we should check is the RPC:Completed line, not the blue linein the pictures above, although their data are similar.

So, we can find that the durationof the third one is quite longer than the others. This may be a typical parametersniffing problem.

Further study.


Then, we can discover which statements cost so much time.

2.    Check the statistics profile of the statement

Insert into #tmp_exceptiondata
                  Select EmpFK ,URAER.FLPercentage 
                  FROM @tmpviewableusers t 
                  Inner join TBL_TMX_USERREQUIREDACTER  URAER on URAER.EmpFK = t.ViewEmpId  
                  Inner join TBL_TMX_Activity Act on Act.Activity_PK = URAER.RootActivityFK   
                  where Act.Private=0   
                  AND URAER.LevelVal = 1  
                  UNION ALL 
                  Select EmpFK ,URAER.FLPercentage 
                  FROM @tmpviewableusers t 
                  Inner join TBL_TMX_USERREQUIREDACTER  URAER on URAER.EmpFK = t.ViewEmpId  
                  Inner join TBL_TMX_Activity Act on Act.Activity_PK = URAER.RootActivityFK   
                  where Act.Private=0   
                  AND  (URAER.LevelVal = 0    
                  AND URAER.ActivityFK NOT IN (SELECT PrntActFK FROM TBL_TMX_UserRequiredActER WHERE PRNTACTFK IS NOT NULL))




Comparing the actual rows and the estimate rows, we can find that the statistics is not so accurate. So the third one has used an unsuitable plan inherited from the first one, although the first one is really the optimal for itself.

Furthermore, a table variable is created inmemory, and so performs slightly better than #temp tables.

However, the system will not generate automatic statistics on table variables. Likewise, you cannot manually createstatistics (statistics are used to help the optimizer pick the best possible query plan). 

3.     Solution

1.      Query Hint

·   Recompile

·   Optimizefor

·   Planguide

2.      Construct a non-clustered index

3.      Temp table instead of table variable

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值