前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >细致入微:Oracle中执行计划在Shared Pool中的存储位置探秘

细致入微:Oracle中执行计划在Shared Pool中的存储位置探秘

作者头像
数据和云
发布2018-03-06 14:41:21
7720
发布2018-03-06 14:41:21
举报
文章被收录于专栏:数据和云数据和云

这两天我一直在想一个问题,那就是 Oracle 的执行计划到底存储在什么地儿?它会是一种什么样的格式?

这里我试图对这个问题做一点我自己认为的解释,这个解释可能是有问题的。

朋友们在看这篇文章之前,应该首先熟悉如下的这张图:

怎样才叫熟悉这张图呢?我认为验证的方法就是看你是否能够仅仅看着这张图,在1个小时的时间内把这张图的内容解释清楚。

这张图也许能够用来衡量你对 library cache 的了解程度。

我们现在做的测试其实就来源于上面这张图:

首先执行一下下述的 sql:

SQL_testdb>select * from scott.emp;

接着查一下上述 sql 在 library cache中的library cache object handle 的地址,一下两种方式都可以获取 SQL 语句父游标地址:

SQL_testdb>select address,sql_text from v$sqlarea where sql_text like ‘select * from scott.emp%’;

然后我们 dump 一下 library cache,注意这里level一定要大于等于8,否则看不到heap 0的内容:

SQL_testdb>alter session set events ‘immediate trace name library_cache level 11’; Session altered. SQL_testdb>oradebug setmypid Statement processed. SQL_testdb>oradebug tracefile_name /cadrasu01/app/oracle/admin/testdb/udump/testdb_ora_4128918.trc

从上述trace文件中我们以 070000001CAE2C70 为关键字去查询,查询结果如下:

注意看上述 library cache object 的类型是 cursor,名字是 select * from scott.emp,它有一个子 cursor,其 library cache object handle 的地址是 70000001cae15e0。我们现在就以这个地址继续搜索上述 trace 文件,搜到的内容如下:

从上述内容中我们可以看到,子 cursor 是没有名字的,这个其实很正常——因为 Oracle 是通过先访问其 parent cursor 后才会来访问它。

另外,这个子 cursor 只有两个 data block,分别是 data block 0 和 data block 6,对应的就是上图中的 heap 0 和 heap 6。

我首先排除掉 heap 0,理由如下:

The data block structure for a heap, stored in heap 0, contains a pointer to the first data block that is allocated for the heap, a status indicator, the pin under which the heap is loaded, and so on.

那么剩下的就只有一种可能,就是 Oracle 把 sql 的执行计划存储在了 heap 6里。

接下来我们 dump一下 heap 6 的内容:

SQL_testdb>oradebug setmypid Statement processed. SQL_testdb>alter session set events ‘immediate trace name heapdump_addr level 2, addr0x70000001cae1328′; Session altered. SQL_testdb>oradebug tracefile_name /cadrasu01/app/oracle/admin/testdb/udump/testdb_ora_2859106.trc

从上述 trace文件中我们可以看到如下的内容:

上述 trace 文件的内容告诉我们 heap 6 实际上就是 sql area(这个和上图中描述的 heap 6是 sql context 不尽相同),剩下的内容除了我们能看出包含了表 scott.emp 的各个字段的内容之外,其他的就看不懂了。

我猜测 Oracle 把 sql 的执行计划存在了这个 sql 的子 cursor 的 heap 6(也就是 sql area)中,只不过存储的形式是编译好的二进制格式。

感谢 MOS,让我找到了如下的论据,可以在某种程度上让我自圆其说:

Parsing a cursor builds four different library cache structures, if they do not already exist, within the library cache: 1、parent cursor handle 2、parent cursor object, containing the child dependency list 3、child cursor handle, inserted in the child dependency list of the parent object 4、child cursor object, containing the compilation and run-time execution plan for the compiled SQL statement.

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-07-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据和云 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档