DBMS_XPLAN.DISPLAY_CURSOR 与解释计划(如果不使用 Gather_plan_statistics 提示)

发布于 2025-01-07 20:57:32 字数 562 浏览 0 评论 0原文

只是要求澄清两者之间的区别。据我了解,EXPLAIN PLAN 为您提供理论执行计划,而 DBMS_XPLAN.DISPLAY_CURSOR 为您提供实际执行计划报表的统计数据。

EXPLAIN PLAN 将此数据存储在 PLAN_TABLE 中,而 DBMS_XPLAN 使用 V$SQL_PLAN、V$SQL_PLAN_STATISTICS 和 V$SQL_PLAN_STATISTICS_ALL 视图来获取其信息。

但是,为了让 DISPLAY_CURSOR 收集该语句的实际运行时统计信息,需要设置 /*+ Gather_plan_statistics */ 提示。否则,仅填充 V$SQL_PLAN,这只会为您提供执行计划,但不会提供实际的执行统计信息。仅在 /*+ Gather_plan_statistics */ 中填充 V$SQL_PLAN_STATISTICS。

所以我的问题是,如果我不使用 Gather_plan_statistics 提示,EXPLAIN PLAN 和 DISPLAY_CURSOR 是否总是给我相同的执行计划(对于相同的语句)?

Just requesting some clarification on the difference between the 2. From what I understand, EXPLAIN PLAN gives you the theoretical execution plan while DBMS_XPLAN.DISPLAY_CURSOR gives you the actual execution plan with execution statistics for the statement.

EXPLAIN PLAN stores this data in a PLAN_TABLE while DBMS_XPLAN uses the V$SQL_PLAN, V$SQL_PLAN_STATISTICS and V$SQL_PLAN_STATISTICS_ALL views for its information.

However, for DISPLAY_CURSOR to collect the actual runtime statistics for that statment, one needs to set the /*+ gather_plan_statistics */ hint. Otherwise, only V$SQL_PLAN is filled which will only give you the execution plan but not the actual execution statistics. It is only with the /*+ gather_plan_statistics */ where V$SQL_PLAN_STATISTICS is filled.

So my question is, if I do not use the gather_plan_statistics hint, will EXPLAIN PLAN and DISPLAY_CURSOR always give me the same execution plan (for the same statement)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

亣腦蒛氧 2025-01-14 20:57:33

差异不是很微妙,而是很大。

正如您正确提到的,解释计划将其数据存储在 plan_table 中,并且从该表中查询计划。这意味着不执行 SQL,仅要求优化器提供计划。在该设置中,计划在很大程度上取决于运行解释计划的会话的优化器环境。

使用 DBMS_XPLAN.DISPLAY_CURSOR,您可以获得之前执行过的计划。该计划不是通过发出 DBMS_XPLAN.DISPLAY_CURSOR 来存储的;它存储在 v$ 结构中,因为它已被执行。

在会话中,您可以运行

select * from dual;
select * from table(dbms_xplan.display_cursor);

查询在“从双重中选择”中执行,这也会导致创建计划并将其存储在 v$ 结构中。 display_cursor 只是找到最后执行的光标并显示它遵循的计划。通过此设置 /*+ Gather_plan_statistics */ 没有附加值,因为计划及其统计信息已经存在于共享池中。

您的另一个问题是,计划是否始终相同取决于许多因素。变量是否相同?,您是否使用自适应游标共享,您是否使用 SQL 计划稳定性...

您的问题:给explain计划和display_cursor相同的计划?我不会依赖于此,因为对于解释计划,该计划取决于您的会话优化器环境。 display_cursor 是更好的方法,并且最好使用由应用程序创建的命名光标。如果不使用 SQL 计划稳定性,则当优化器统计信息更改时计划可能会更改。如果您使用自适应游标共享,则计划可能会随着变量的变化而变化。

可以在 Jonathan Lewis 找到一些关于采样开销的好读物 博客。同样来自乔纳森:gather_plan_statistics 我通常更聪明地使用statistics_level设置'all'用于调试而不是使用 /*+ Gather_plan_statistics */ 提示。该提示更改代码并产生新的 sql_id。

我希望这有帮助。

The differences are not very subtle, they are huge.

As you correctly mentioned, explain plan stores it's data in the plan_table and the plan is queried from that table. This means that the sql is NOT executed, only the optimizer is asked to deliver a plan. In that setup the plan depends heavily on the optimizer environment of your session in which you run the explain plan.

With DBMS_XPLAN.DISPLAY_CURSOR you get the plan as it has been executed before. The plan is not stored by issuing the DBMS_XPLAN.DISPLAY_CURSOR; it stored in the v$ structures because it has been executed.

In a session you can run

select * from dual;
select * from table(dbms_xplan.display_cursor);

The query is executed in the 'select from dual', this also results in the creation of a plan and that i stored in the v$ structures. the display_cursor just finds the last executed cursor and displays the plan it followed. With this setup /*+ gather_plan_statistics */ has no added value because the plan and it's statistics are already present in the shared_pool.

Your other question, whether or not the plan is always the same depends on many factors. Are the variables the same?, are you using Adaptive Cursor Sharing, are you using SQL Plan Stability ...

Your question: give explain plan and display_cursor the same plan? I would not rely on that because with explain plan, the plan depends from your sessions optimizer environment. display_cursor is the better way, and preferably using a named cursor that is created by the application. If you don't use SQL Plan Stability, the plan can change when the optimizer statistics change. If you use Adaptive Cursor Sharing, the plan can change when the variables change.

A bit of nice reading about the overhead of the sampling can be found at Jonathan Lewis blog. Also from Jonathan: gather_plan_statistics I is often smarter to use statistics_level setting 'all' for debugging as opposed to using the /*+ gather_plan_statistics */ hint. The hint changes code and causes a new sql_id.

I hope this helps.

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