SQL Server 中是否缓存了函数的执行计划?
任何机构都可以帮助我了解 SQL Server 中是否缓存了函数的执行计划吗?
有这方面的在线资源吗?
Can any body help me in understanding if the execution plan for functions cached in SQL server?
Any online resource for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接受的答案不准确/具有误导性,主要是由于引用的引用对于术语“用户定义函数”过于模糊。
Microsoft SQL Server 中有多种不同类型的用户定义函数,它们的处理方式也不同:
多语句 TVF:
它们被视为存储过程。执行它们的查询仅显示对其名称的引用,而不显示对其任何定义的引用。它们出现在
sys.dm_exec_cached_plans
中,cacheobjtype
为“Compiled Plan”,objtype
为“Proc”。任何输入参数值也与计划一起存储,因此多语句 TVF 会受到参数嗅探问题的影响。内联 TVF (iTVF):
这些被视为视图。执行它们的查询包含它们的定义。它们出现在
sys.dm_exec_cached_plans
中,cacheobjtype
为“Parse Tree”,objtype
为“View”。输入参数值不与计划一起存储,因此内联TVF不会受到参数嗅探问题的影响。标量 UDF:
它们被视为存储过程。执行它们的查询仅显示对其名称的引用,而不显示对其任何定义的引用。它们出现在
sys.dm_exec_cached_plans
中,cacheobjtype
为“Compiled Plan”,objtype
为“Proc”。任何输入参数值也与计划一起存储,因此标量 UDF 会受到参数嗅探问题的影响。此外,与上面提到的两种类型的 TVF 不同,但与常规存储过程一样,您可以在通过EXEC[UTE]
WITH RECOMPILE 选项强制重新编译执行计划> 而不是SELECT
或SET
。SQLCLR 对象:
这些更像是客户端/应用程序代码。执行它们的查询仅显示对其名称的引用,而不显示对其任何定义的引用。它们出现在 sys.dm_exec_cached_plans 中,
cacheobjtype
为“CLR Compiled Func”或“CLR Compiled Proc”,objtype
为“Proc” ”。但是,与多语句 TVF 和标量 UDF 不同,它们没有定义,因此没有关联的查询计划。但是,它们执行的任何即席查询(不是存储过程调用)都会显示在sys.dm_exec_cached_plans
中,其中cacheobjtype
为“Compiled Plan”,objtype
代码>“准备好”。任何这些即席查询,如果参数化,都应该将初始输入参数值与准备好的计划一起存储,因此会受到参数嗅探问题的影响。有关对象缓存的更多详细信息,请参阅 MSDN 页面上的缓存机制 。
The accepted answer is inaccurate / misleading, primarily due to the referenced quote being too vague with regards to the term "user-defined functions".
There are several different types of User-Defined Functions in Microsoft SQL Server, and they are treated differently:
Multi-statement TVFs:
These are treated like Stored Procedures. The query that executes them only shows the reference to their name, not to any of their definition. They show up in
sys.dm_exec_cached_plans
with acacheobjtype
of "Compiled Plan" and anobjtype
of "Proc". Any input parameter values are also stored with the plan, hence Multi-statement TVFs are subject to parameter-sniffing issues.Inline TVFs (iTVFs):
These are treated like Views. The query that executes them incorporates their definition. They show up in
sys.dm_exec_cached_plans
with acacheobjtype
of "Parse Tree" and anobjtype
of "View". Input parameter values are not stored with the plan, hence Inline TVFs are not subject to parameter-sniffing issues.Scalar UDFs:
These are treated like Stored Procedures. The query that executes them only shows the reference to their name, not to any of their definition. They show up in
sys.dm_exec_cached_plans
with acacheobjtype
of "Compiled Plan" and anobjtype
of "Proc". Any input parameter values are also stored with the plan, hence Scalar UDFs are subject to parameter-sniffing issues. Also, unlike the two types of TVFs noted above, but like regular Stored Procedures, you can force recompilation of the execution plan using theWITH RECOMPILE
option when executing viaEXEC[UTE]
instead ofSELECT
orSET
.SQLCLR objects:
These are treated more like client / app code. The query that executes them only shows the reference to their name, not to any of their definition. They show up in
sys.dm_exec_cached_plans
with acacheobjtype
of "CLR Compiled Func" or "CLR Compiled Proc", and anobjtype
of "Proc". But, unlike Multi-statement TVFs and Scalar UDFs, they do not have a definition and so do not have an associated query plan. However, any adhoc queries (not stored procedure calls) that they execute show up insys.dm_exec_cached_plans
with acacheobjtype
of "Compiled Plan" and anobjtype
of "Prepared". Any of these adhoc queries, if parameterized, should be storing the initial input parameter values with the prepared plan, and would hence be subject to parameter-sniffing issues.For more details on object caching, please see the MSDN page on Caching Mechanisms.
是的,它们确实进入执行计划缓存。
sys.dm_exec_query_plan DMV 将显示给定计划句柄的计划。引用那里:
Yes they do go in the execution plan cache.
The sys.dm_exec_query_plan DMV will show a plan for given plan handle. Quote from there:
是的,它们被考虑用于缓存。
http://msdn.microsoft.com/en-us/library/ms181055。 ASPX
Yes they are considered for caching.
http://msdn.microsoft.com/en-us/library/ms181055.aspx