使用Ado.net执行SP很慢,而用SSMS执行很快

今天遇到一个问题,有用户反应,在site上打开报表,一直loading,出不来结果。

遇到这种问题,我立刻simulate用户使用Filter Condition,问题repro,看来不是偶然事件,通过SQL Server Profile Capture 到执行的查询语句。

Step1,奇怪的是,在SSMS中执行,SQL Server 很快返回结果,初步结论是问题可能不是由于SQL Server 造成的(事实证明,我错了)。

我的第一反应:是不是network的问题,点击F12,查看Network Tab,全部处于Pending状态,于是,立即发mail联系Fore-End 同事查看。

Leader 看到Mail后,说,这种情况时有发生,通过 sys.dm_exec_requests 能够看到SP正在执行,不可能是Fore-End 和Network 的问题,对SP 进行recompile 后,这个问题就不会出现了。

Step2,于是,我仔细查看 sys.dm_exec_requests,发现这个SP停留在一个大表的Join查询上,logic Read 和 physical read的数量很大,还在持续增加,说明这个SP正在SQL Server上运行,排除了network的问题。尝试对SP进行重新编译,使用以下语句,每次调用sp时都recompile。修改完成之后,果然,报表数据很快就显示出来了。

alter procedure dbo.sp_name
@para int...
with recompile
as ....

--RECOMPILE   
--Indicates that the Database Engine does not cache a plan for this procedure and the procedure is recompiled at run time.

Step3,SQL Server 为什么会重用拙劣的执行计划?

SQL Server 重用拙劣的执行计划,这就是Parameter Sniffing,Sql server 使用某一个参数生成执行计划,由于该参数不具有代表性,当其他参数重用该执行计划时,其执行效率变得特别慢。

在生成执行计划时,SQL Server根据当前的参数,生成一个适合当前参数的执行计划,该执行计划不一定是最优的,但一定是适合当前参数的,比较优秀的执行计划。由于生成执行计划,比较耗费CPU资源,对于相似的查询语句(query with same shape),SQL Server 会重用执行计划。在重用执行计划时,SQL Server 不会去检查已存的执行计划是否适合当前参数,这就是Parameter Sniffing问题的根源。

 

可能1,Fore-End调用SqlCommand的Prepare()方法, 创建Prepared Statement,查看DMV:sys.dm_exec_cached_plans,没有发现objtype=Prepared 的Plan cache,说明Fore-End 没有使用预读功能,这其实在一定程度上避免了Parameter Sniffing。

可能2,SQL Server 生成的执行计划是Adhoc Query。查看DMV:sys.dm_exec_cached_plans,发现存在objtype=Adhoc的Plan cache。

结论:SQL Server生成了一个拙劣的执行计划导致这个问题。

在很多关于performance tuning的书籍上都会提到Parameter Sniffing,我也不止一次地对别人说过Parameter Sniffing,终于在Production Environment中遇到Parameter Sniffing问题,还是很兴奋的。遗憾的是,当Parameter Sniffing问题真正出现时,我却没有第一时间识别它,有一丝失落。

 

Step4,对SP 使用with recompile 选项,太粗暴,推荐的做法是使用 Option Clause

OPTION (OPTIMIZE FOR (@Parameter_Name = N'Typical_Parameter_Value'))

 

Option Clause

Specifies that the indicated query hint should be used throughout the entire query. Each query hint can be specified only one time, although multiple query hints are permitted. Only one OPTION clause can be specified with the statement.

This clause can be specified in the SELECT, DELETE, UPDATE and MERGE statements.

[ OPTION ( <query_hint> [ ,...n ] ) ] 

query_hint  

Keywords that indicate which optimizer hints are used to customize the way the Database Engine processes the statement. For more information, see Query Hints (Transact-SQL).

 

参考Doc:

I Smell a Parameter!

sys.dm_exec_cached_plans (Transact-SQL)

OPTION Clause (Transact-SQL)

Parameter Sniffing

Optimize Parameter Driven Queries with SQL Server OPTIMIZE FOR Hint

posted @ 2016-06-14 19:20  悦光阴  阅读(1181)  评论(0编辑  收藏  举报