积极答复者
同样的查询+参数+每次清空计划缓存,直接写成T-SQL和写存储过程执行计划不一致的原因

问题
答案
全部回复
-
You let me test your problem MYSELF?
no,no,no,U misunderstand me
I mean that's not parameter sniff problem,I will test myself.
thank u very much
in addition,that's true local variable problem,another question:why this " (CustomerId = @p_CustomerId or @p_CustomerId is null)" way use index scan plan rather than index seek?
- 已编辑 X_PICK 2017年1月8日 3:34 addition
-
我建议分开写
if @p_CustomerId is null
select ...
else
select ...
你这种写法确实可能影响性能的。编译值确定,也只是计算执行成本,不会用这个值去判断逻辑表达式的。
想不想时已是想,不如不想都不想。
这种catch all queries的查询条件的解决方法还是有的,主要是我问的那种方式,为什么抑制了索引,没有找到一个完美的解释
后面也找到了,参考这个http://www.sommarskog.se/dyn-search-2008.html#BasicStaticSQL。
里面The History of Forced Recompilation 这一段有非常清楚的解释
因为研究这个问题我延伸了一下WITH RECOMPILE和OPTION(RECOMPILE)
http://www.cnblogs.com/wy123/p/6262800.html
谢谢大版主!
- 已编辑 X_PICK 2017年1月10日 9:46 修改