暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

崔华 2016-07-14
755


(题图来自Oracle VP , Sally Piao的摄影佳作,感谢摄影师授权)

编辑手记:感谢社区编辑李波协助,感谢崔华大神的精彩分享。在学习Oracle技术的过程中,总有一些时候你会对某个细节百思不得其解,而破解这些问题则成为进阶路上的苦乐丰碑。这篇文章介绍了执行计划在Shared Pool中的存储位置探秘。


这两天我一直在想一个问题,那就是 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.


如何加入"云和恩墨大讲堂"微信群

搜索 盖国强(Eygle) :eeygle,或者扫描下面二维码,备注:云和恩墨大讲堂,即可入群。每周与千人共享免费技术分享,与讲师在线讨论。

近期文章

七月盛放:云和恩墨大讲堂电子期刊第七期

风云再起:美500强Oracle利润率IT类第一

Oracle 12.2:Sharding 新特性揭秘

用SQL解一道数学题:Gauss和Poincare

Oracle 12c ASM 防火防盗新特性揭秘

DBA入门之路:学习与进阶之经验谈


文章转载自崔华,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论