`

Oracle Library cache 内部机制 说明

 
阅读更多

一. Library Cache 说明

LibraryCache 是Oracle SGA 中Shared pool 的组成部分。Shared Pool的说明,参考之前的blog:

Oracle Shared pool 详解

http://blog.csdn.net/tianlesoftware/article/details/6560956

这里重点看一下Library Cache的一个管理机制。 参考了如下2篇blog,重新进行了整理:

Librarycache内部机制详解

http://www.hellodb.net/2010/07/oracle-library-cache.html

Oracle中执行计划的存储位置

http://dbsnake.com/2011/05/exe-plan-stor-location.html

1. DSI 中对Library Cache的说明:

(1)An area in the shared pool thatmanages information about:

--Sharedcursors (SQL and PL/SQL objects)

--Databaseobjects (tables, indexes, and so on)

(2)Initially created to manage PL/SQLprograms and library units, therefore called library cache

(3)Scope was extended to includeshared cursors and information about other RDBMS objects.

2. Library Cache Objects

(1)The units of information that arestored in the library cache are called objects.

(2)There are two classes of objects:

1)Stored objects

--Created and dropped withexplicit SQL or PL/SQL commands

Examples: Tables, views, packages,functions

2)Transient objects

--Created at execution time and liveonly for the duration of the instance(or aged out)

Example: Shared and nonsharedcursors

3. Shared Cursors

(1)In-memory representation of anexecutable object:

SQLstatements

AnonymousPL/SQL block

PL/SQLstored procedures or functions

Javastored procedures

ObjectMethods

(2)Represented by two or more objects:

Aparent cursor that has a name

Oneor more child cursors containing the execution plan

4. Library Cache Architecture

(1)The library cache is a hash tablethat is accessible through an array of hash buckets.

(2)The library cache manager (KGL)controls the access and usage of library cache objects.

(3)Memory for the library cache isallocated from the shared pool.

5. Library cache需要解决三个问题:

(1).快速定位的问题:Library cache中对象众多,Oracle如何管理这些对象,以便服务进程可以迅速找到他们需要的信息。比如某个服务进程需要迅速定位某个SQL是否存在于Librarycache中。

(2).关系依赖的问题:Library cache中的对象存在复杂的依赖关系,当某个objec失效时,可以迅速将依赖其的对象也置为失效状态。比如某个表发生了结构变化,依赖其的SQL语句需要重新解析。

(3).并发控制的问题:Library cache中必须有一个并发控制的机构,比如锁机制,来管理大量共享对象的并发访问和修改的问题,比如某个SQL在重新编译的同时,其所依赖的对象不能被修改。

Oracle利用hash table结构来解决library cache中快速定位的问题,hash table就是很多hash bucket组成的数组。 先看DSI 405 里有几张相关的图片:

LibraryCache 保存了explicitSQL, PL/SQLcommands,shared 和 nonshared cursors。 这些对象都保存在Hash table里,Hash table 又由Hash Bucket组成。 Hash Bucket 由一些Object Handle List 组成,所以在Hash Bucket里查找某个对象,就是搜索这个Handle List。

6. Object Handle

在上图我们可以看到Object handle 保存的信息。 Library cache handle指向library cache object(LCO, heap 0),它包含了library object的名字,命名空间,时间

戳,引用列表,lock对象以及pin对象的列表信息等等。

关于Namespace,参考我的blog:

Oracle Namespace 说明

http://blog.csdn.net/tianlesoftware/article/details/6624122

所以对Library cache中所有对象的访问是通过利用library cache handle来实现的,也就是说我们想要访问library cache object,我们必须先找到library cache handle。

因为Object handle保存了lock 和pin 的信息,即记录哪个用户在这个这个handle上有lock,或者是哪个用户正在等待获得这个lock。那么这里我们也知道了library cache lock是发生在handle上的。

当一个进程请求library cache object, librarycache manager就会应用一个hash 算法,从而得到一个hash 值,根据相应的hash值到相应的hash bucket中去寻找。

如果library cache object在内存中,那么这个library cache handle就会被找到。有时候,当shared pool不够大,library cache handle会保留在内存中,然而library cache heap由于内存不足被age out,这个时候我们请求的object heap就会被重载。最坏的情况下,library cache handle在内存中没有找到,这个时候就必须分配一个新的library cachehandle,同时object heap也会被加载到内存中。

7. Library Cache Object(LCO: Heap 0)

它的结构信息如下图。 这个图需要认真的去理解。

DSI 的说明:

(1)Internally, most of the objectidentity is represented by structures of type kglob.

(2)These are thestructures stored in heap 0.

(3)Object structures have thefollowing components:

Type

Name

Flags

Tables

Datablocks

LibraryCache 存储SQL或者shared cursors 等。 这些信息就是通过Heap 0 这个LCO 来保存的。

7.1 Object Types

(1)Objects aregrouped in namespaces according to their type.

(2)Each object can only be of onetype.

(3)All the objects of the same typeare in the same namespace.

(4)A namespace may be used by morethan one type.

(5)The most important namespace iscalled cursor (CRSR) and houses the shared SQL cursors.

7.2 Object Names

(1)Library cache object names havethree parts:

Nameof schema

Nameof object

Nameof database link (remote objects only)

(2)The format used isSCHEMA.NAME@DBLINK.

Forexample, HR.EMPLOYEES@ACME.COM

7.3 Object Flags

(1)Public flags:

Arenot protected by pins or latches

Indicatein detail the type of the object

(2)Status flags:

Areprotected by pins

Indicatewhether the object is being created/dropped/altered/updated

(3)Special status flags:

Areprotected by the library cache latch

Arerelated to object validity and authorization

7.4 Object Tables

(1)Dependency table

(2)Child table

(3)Translation table

(4)Authorization table

(5)Access table

(6)Read-only dependency table

(7)Schema name table

Object Table 又分为以上7中类型。

7.4.1 dependency table

指向本对象所依赖的对象,比如:select * from emp这个cursor的对象,依赖emp这个表,这里指向了emp这个表的handle。

7.4.2.child table

指向本对象的子对象,比如某个游标的子游标。通俗点说,就是一条SQL 至少有一个parent cursor 和 child cursor。 可能一些SQL 由于某些原因无法共享childcursor,这样就会出现一个parentcursor 和 多个child cursor的情况。 即version count 很高。 那么这种情况下。 parent cursor 里对应的所有childcursor的指针都会保存在child table里面。 Oracle 是用C 写的,所以这里是指针。

注意一点,parent cursor和child cursor都是用library cache object handle 存储在Library Cache里的。即他们的结构完全一样。

这个结论可以通过library cache的dump 文件来证明。在后面我们会做一个测试。

Oracle 高 Version counts 问题说明

http://blog.csdn.net/tianlesoftware/article/details/6628232

7.4.3.authorization table

对象的授权信息。

7.5 Object Data Blocks

(1)The remainder ofan object’s data is stored in other independent data heaps.

(2)The object structure contains anarray of data block structures.

(3)The data blockstructures have a pointer to a different data heap.

(4)An object structure has room for 16data block structures but not all of them are in use.

Heap0 也仅仅保存是一个结构,它不保存实际的data。 而实际data 的存储Heap 的指针就存放在这个Data Blocks里。 这个也可以通过dump 来查看。这个Data Blocks指向的Heap 结构如下图:

这里要注意的,就是我们SQL的执行计划就是存放在这个Heap 6:SQL Context 中。

二. 测试

在上面的说明中,提到,对于多child cursor,所有的child cursor 都保存在child table里,并且SQL 的执行计划也保存在child cursor的Heap 6中。 下面进行相关的测试,证明以上结论。

SYS@anqing1(rac1)> create table lct asselect * from dba_objects;

Table created.

分别用SYSTEM和Dave 用户执行如下SQL:

/* Formatted on 2011/7/24 15:07:20(QP5 v5.163.1008.3004) */

DECLARE

x VARCHAR2 (20);

str VARCHAR2 (200);

BEGIN

x := 'I_PROXY_ROLE_DATA$_1';

str := 'select* from sys.lct where object_name=:x';

EXECUTE IMMEDIATE str USING x;

END;

/

查看SQL 的version count信息

SYS@anqing1(rac1)> selectsql_id,version_count from v$sqlarea where sql_text like 'select * from sys.lctwhere %';

SQL_ID VERSION_COUNT

------------- -------------

5d8tu19t1fug2 2

--查看parentcursor 和 child cursor的handle address:

SYS@anqing1(rac1)> selectaddress,child_address from v$sql where sql_id='5d8tu19t1fug2';

ADDRESSCHILD_AD

-------- --------

30A9DCDC 2CBA64AC

30A9DCDC 2A32C000

将library cache dump出来:

SQL> alter session set events 'immediatetrace name library_cache level 11';

Session altered.

SQL> oradebug setmypid

Statement processed.

SQL> oradebug tracefile_name

/oradata/XEZF/admin/udump/xezf_ora_14041.trc

--查看trace 文件:搜索30A9DCDC

BUCKET 92642:

LIBRARY OBJECT HANDLE:handle=30a9dcdc mtx=0x30a9dd90(2) cdp=2

--object handle 的内存地址

name=select * from sys.lct whereobject_name=:x

--object 的名称

hash=55555e74e494aa0356a33a0a721769e2 timestamp=07-24-2011 15:03:04

--hash值和timestamp

namespace=CRSRflags=RON/KGHP/TIM/PN0/SML/KST/DBN/MTX/[120100d0]

--namespace 和 flags

kkkk-dddd-llll=0000-0001-0001 lock=N pin=0latch#=1 hpc=0006 hlc=0006

--Heaps loaded and kept,lock, pin, and latch modes

lwt=0x30a9dd38[0x30a9dd38,0x30a9dd38]ltm=0x30a9dd40[0x30a9dd40,0x30a9dd40]

pwt=0x30a9dd1c[0x30a9dd1c,0x30a9dd1c]ptm=0x30a9dd24[0x30a9dd24,0x30a9dd24]

ref=0x30a9dd58[0x30a9dd58,0x30a9dd58]lnd=0x30a9dd64[0x30a9dd64,0x30a9dd64]

--以上是:Linklists of lock waiters, temporary locks, pin waiters, temporary pins andreferences

LOCK OWNERS:

lock user session count mode flags

-------- -------- -------- ----- ---- ------------------------

2d1a0cac 30efbd20 30efbd20 2N [00]

2d10501c 30f10e78 30f10e78 2N [00]

LIBRARY OBJECT: object=2aa6cf2c

--Memory address of theobject (heap 0)

type=CRSR flags=EXS[0001] pflags=[0000] status=VALD load=0

--Object type, flags, andstatus

CHILDREN: size=16

child# table reference handle

------ -------- --------- --------

02c08c08c 2c08bd5c 2cba64ac

1 2c08c08c 2c08bec8 2a32c000

--这个就是所有child cursor 的handle 的内存地址

DATA BLOCKS:

data# heap pointerstatus pins change whr

----- -------- -------- --------- ---- ------ ---

0 2b8839b4 2aa6cfc4 I/P/A/-/- 0NONE 00

--Object data structures(heap descriptors)

BUCKET 92642 total object count=1

查看其中一个child cursor 地址,在trace 里搜索:2cba64ac

LIBRARY OBJECT HANDLE: handle=2cba64acmtx=0x2cba6560(0) cdp=0

namespace=CRSR flags=RON/KGHP/PN0/EXP/[10010100]

kkkk-dddd-llll=0000-0001-0001 lock=N pin=0 latch#=1 hpc=0002 hlc=0002

lwt=0x2cba6508[0x2cba6508,0x2cba6508] ltm=0x2cba6510[0x2cba6510,0x2cba6510]

pwt=0x2cba64ec[0x2cba64ec,0x2cba64ec]ptm=0x2cba64f4[0x2cba64f4,0x2cba64f4]

ref=0x2cba6528[0x2c08bd5c,0x2c08bd5c]lnd=0x2cba6534[0x2cba6534,0x2cba6534]

CHILD REFERENCES:

reference latch flags

--------- ----- -------------------

2c08bd5c 0 CHL[02]

LOCK OWNERS:

lock user session count mode flags

-------- -------- -------- ----- ---- ------------------------

2d345160 30efbd20 30efbd20 1N [00]

2d1a0bdc 30f10e78 30f10e78 1N [00]

LIBRARY OBJECT: object=2c1a3280

type=CRSR flags=EXS/RIV[0201] pflags=[0000] status=VALD load=0

DEPENDENCIES: count=1 size=16

dependency# tablereference handle position flags

----------- -------- --------- -------- -------- -------------------

0 2db7d838 2db7d770 2bb25644 18 DEP[01]

AUTHORIZATIONS: count=1 size=16 minimum entrysize=16

00000000 30000000 00020000 00000000

ACCESSES: count=1 size=16

dependency# types

----------- -----

0 0009

SCHEMA: count=1 size=262144

0000003d

DATA BLOCKS:

data# heap pointerstatus pins change whr

----- -------- -------- --------- ---- ------ ---

0 2a078574 2c1a3318 I/P/A/-/- 0NONE 00

6 2aa6d2542c4f9cf0 I/-/A/-/E 0 NONE 00

这个结构和Heap 0的差不多。 但是这个child cursor 没有name了。 因为我们访问时先通过parent cursor。 在到child cursor。所以parent cursor 有name 就可以了。

这里的Data blocks 有2个: 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, astatus indicator, the pin under which the heap is loaded, and so on.

执行计划保存在Heap 6里,我们把Heap 6 dump 出来,看一下:

SYS@anqing1(rac1)> alter session setevents 'immediate trace name heapdump_addr level 2,addr 0x2aa6d254';

Session altered.

--注意,这里的addr前面加了0x。

SYS@anqing1(rac1)> oradebug setmypid

Statement processed.

SYS@anqing1(rac1)> oradebugtracefile_name

/u01/app/oracle/admin/anqing/udump/anqing1_ora_1533.trc

在trace 里搜索:0x2aa6d254

HEAP DUMP heapname="sql area"desc=0x2aa6d254

extent sz=0xff4 alt=32767 het=156 rec=0 flg=2opc=2

parent=0x2000002c owner=0x2aa6d1c8 nex=(nil)xsz=0xff4

EXTENT 0 addr=0x2a73d5e8

Chunk 2a73d5f0 sz= 788 free" "

Dump of memory from 0x2A73D5F0 to0x2A73D904

2A73D5F0 C0000315 00000000 2AA6D2B42AA6D2B4 [...........*...*]

2A73D600 2B10E1FC C0000051 000000002C49E55C [...+Q.......\.I,]

2A73D610 2AEA8820 00314E00 0000800400010035 [ ..*.N1.....5...]

2A73D620 00930024 00020001 0019003A00020074 [$.......:...t...]

2A73D630 001A0019 000200BF 007400B000500004 [..........t...P.]

2A73D640 001A0024 00BF0033 0003001200010022 [$...3......."...]

2A73D650 62000006 0000024D 2A73D6040C9D040C [...bM.....s*....]

2A73D660 00000000 2A73D8A0 0035000000240001 [......s*..5...$.]

2A73D670 00010093 003A0002 0074001900190002 [......:...t.....]

2A73D680 00BF001B 00B00002 0004007400240039 [........t...9.$.]

2A73D690 0033001B 001200BF 0022000300060001 [..3.......".....]

2A73D6A0 12021400 00010022 0A000006006B0802 [....".........k.]

2A73D6B0 03671F85 EF042C04 001C004E000C0000 [..g..,..N.......]

2A73D6C0 001D0400 001C0056 000002E0001D0040 [....V.......@...]

2A73D6D0 00B10028 000D0028 008C00260026005E [(...(...&...^.&.]

2A73D6E0 00A40026 000E000C 0E00002612011201 [&.......&.......]

2A73D6F0 00010022 AC000009 00AF0A0000010021 ["...........!...]

2A73D700 00020074 00210009 001500BF00780026 [t.....!.....&.x.]

....

dbsnake blog里的总结如下:

heap6实际上就是sql area(这个和上图中描述的heap 6是sql context不尽相同)。 Oracle把sql的执行计划存在了这个sql的子cursor的heap 6(也就是sql area)中,只不过存储的形式是编译好的二进制格式。

MOS 上的相关说明:

Parsinga cursor builds four different library cache structures, if they do not alreadyexist, 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 theparent object

4、childcursor object, containing the compilation and run-time execution plan for thecompiled SQL statement.

-------------------------------------------------------------------------------------------------------

Blog: http://blog.csdn.net/tianlesoftware

Email: dvd.dba@gmail.com

DBA1 群:62697716(满); DBA2 群:62697977(满)DBA3 群:62697850(满)

DBA 超级群:63306533(满); DBA4 群: 83829929 DBA5群: 142216823

DBA6 群:158654907 聊天 群:40132017 聊天2群:69087192

--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请

分享到:
评论

相关推荐

    彻底搞清楚library cache lock的成因和解决方法

    当出现上述情况的时候,我们可以通过v$session_wait查询等待事件,当然,这种情况下,该session的等待事件一定是'Library cache lock'。 让我们来解释一下, 第一种情况,当会话1(session 1)在对一个表执行DML ...

    library cache pin lock解决办法

    针对oracle性能问题中的library cache pin lock,给出解决方案。

    oracle Library cache latch 竞争的解决

    确定系统慢的原因: select * from v$session_wait where event != 'client message' and event not like '%NET%' and wait_time = 0 and sid > 5;

    library cache lock等待事件处理

    library cache lock等待事件分析处理

    Library cache 库缓冲

    关于oracle share pool 中library cache的详解

    Latch Free、Library cache伪游标(pseudo cursor)之间的那些事

    你也在为LatchFree、Librarycache争用那些事头疼吗,快来看看本文作者是怎么解决的。

    library_cache内存详解

    详解library cache 自己手工整理 有latch pin cursor leap 等的论述

    library cache pin等待事件

    library cache pin 事件是用来管理library cache的并发访问的, pin一个object会引起相应的heap被载入内存中,如果客户端需要修改或检测这个object它就必须在锁住后取得一个pin.library cache pin的等待时间为3秒钟,...

    library cache lock等待事件

    Oracle利用Library cache lock和Library cache pin来实现并发控制,Library cache lock是在handle上获取的,而Library cache pin则是在data heap上获取。访问对象时,首先必须获取handle上的lock,然后将访问的数据...

    等待事件row cache lock,latch row cache objects处理过程.txt

    等待事件row cache lock,latch row cache objects处理过程

    oracle调优之-共享池尺寸调优+library_cache+dicitonary_library_命中率.doc

    oracle调优之-共享池尺寸调优+library cache+dicitonary library 命中率 当构建生产系统,我们的一般做法是首先根据经验给出一个共享池尺寸,然后,在一定的工作载荷压力下,检查相关的统计 来检查共享池的效率。 ...

    数据库oracle

    在分析完这个SQL,Oracle会把他的分析结果给保存在Shared pool的Library Cache中,当数据库第二次执行该SQL时,Oracle自动跳过这个分析过程,从而减少了系统运行的时间。这也是为什么第一次运行的SQL 比第二次运行的...

    oracle执行计划

    sql语句按照hash的算法,产生hash 值,这里,我们可以把hash值当做一个PK值,然后根据这个值去寻找library cache相同的hash 值。如果有则应用这个hash所对应的执行计划。如果没有则再生成一个新的执行计划。

    深入解析Oracle.DBA入门进阶与诊断案例

    6.2.8 Library Cache Pin及Library Cache Lock分析 273 6.2.9 诊断案例一:version_count过高造成的Latch竞争解决 281 6.2.10 V$SQL与V$SQLAREA视图 287 6.2.11 Oracle 10g中version_count过高的诊断 292 ...

    ORACLE Bug 17588480

    Bug 17588480 library cache mutex/shared pool latch deadlock 官方说明文档

    第1章 Oracle性能调整概述1

    第1章 Oracle性能调整概述 第1章 Oracle性能调整概述1

    oracle数据库性能优化案例分析及最佳实践

    案例一:Library cache lock等待 问题背景: 严重的Library cache lock等待,导致SQL执行的很慢 问题分析: Library cache lock等待常见场景: DDL、统计信息搜集 Namespace→1:table/view/sequence/synonym/ 错误...

    Oracle9i的init.ora参数中文说明

    Oracle9i初始化参数中文说明 Blank_trimming: 说明: 如果值为TRUE, 即使源长度比目标长度 (SQL92 兼容) 更长, 也允许分配数据。 值范围: TRUE | FALSE 默认值: FALSE serializable: 说明: 确定查询是否获取表级...

    2021 云和恩墨大讲堂PPT汇总(50份).zip

    Oracle 12cR2 ADG LGWR Library Cache案例分享 Oracle 10046 Trace 的取得和解析方法 Oracle 基于AWR ASH的性能分析 Oracle DataGuard备份恢复最佳实践-姚远 Oracle Nologging全面总结 Oracle RAC 集群安装部署 ...

Global site tag (gtag.js) - Google Analytics