SQL Server 中是否缓存了函数的执行计划?

发布于 2024-09-06 16:47:02 字数 66 浏览 11 评论 0原文

任何机构都可以帮助我了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-09-13 16:47:02

接受的答案不准确/具有误导性,主要是由于引用的引用对于术语“用户定义函数”过于模糊。

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 选项强制重新编译执行计划> 而不是 SELECTSET

  • 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 a cacheobjtype of "Compiled Plan" and an objtype 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 a cacheobjtype of "Parse Tree" and an objtype 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 a cacheobjtype of "Compiled Plan" and an objtype 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 the WITH RECOMPILE option when executing via EXEC[UTE] instead of SELECT or SET.

  • 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 a cacheobjtype of "CLR Compiled Func" or "CLR Compiled Proc", and an objtype 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 in sys.dm_exec_cached_plans with a cacheobjtype of "Compiled Plan" and an objtype 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.

酒浓于脸红 2024-09-13 16:47:02

是的,它们确实进入执行计划缓存。

sys.dm_exec_query_plan DMV 将显示给定计划句柄的计划。引用那里:

各种类型的查询计划
Transact-SQL 批处理,例如 ad hoc
批处理、存储过程和
用户定义的函数,缓存在
称为计划的内存区域
缓存。每个缓存的查询计划是
由唯一标识符标识
称为计划句柄。您可以指定
该计划处理
sys.dm_exec_query_plan 动态
管理视图来检索
特定的执行计划
Transact-SQL 查询或批处理。

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:

Query plans for various types of
Transact-SQL batches, such as ad hoc
batches, stored procedures, and
user-defined functions, are cached in
an area of memory called the plan
cache. Each cached query plan is
identified by a unique identifier
called a plan handle. You can specify
this plan handle with the
sys.dm_exec_query_plan dynamic
management view to retrieve the
execution plan for a particular
Transact-SQL query or batch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文