我们知道在Oracle 10g之前,Oracle执行事务之前必须在undo表空间记录事务的执行情况,如分配事务表(TX Table),分配undo block。进行这些操作必须将undo block读至buffer cache,这又会涉及到Oracle latch分配和latch pin等操作。Oracle为了实现一致性读取要求,假如undo block没在buffer cache,又需从undo表空间物理读取block,这些资源密集型操作势必影响其性能。从Oracle 10g开始,Oracle开始推出一新特性memory undo,即专门在shared pool中开辟内存处理事务,以下就简单研究一下In memory UNDO,简称imu,注意在RAC情况下imu不被支持,搜索metalink目前imu存在较多bug
研究平台为
引用
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
Oralce 10g是否使用由隐含参数_in_memory_undo决定,默认情况为TRUE。
引用
_in_memory_undo TRUE Make in memory undo for top level transactions
此外imu还涉及以下参数
imu pool在shard_pool分配的个数,注意此参数和实际分配的个数不符,只作为阀值存在:
引用
_imu_pools 3 in memory undo pools
DBWR是否刷新imu pool:
引用
_db_writer_flush_imu TRUE If FALSE, DBWR will not downgrade IMU txns for AGING
递归操作是否启动imu:
引用
_recursive_imu_transactions FALSE recursive transactions may be IMU
基于以上参数我们再深入探索。
查看imu pool在shared_pool分配的大小,可以看到在shared_pool中分配了1235304 BYTES。
引用
SQL> select * from v$sgastat where name='KTI-UNDO';
POOL NAME BYTES
------------ ---------------------------------------- ----------
shared pool KTI-UNDO 1235304
进一步通过X$KTIFP,可以查看每一个imu pool内存使用情况,可以看到目前系统中imu pool共为18个(为参数transactions*10%),每个pool大小为65535 bytes(对于32为系统为64k + 24bytes=65535 bytes,对于64位系统为128k + 1k=132096 bytes),可以看到pool大小为64k,所以imu只针对小事务使用。
引用
SQL> show parameter transactions
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
transactions integer 187
SQL> select KTIFPNO,KTIFPPSI from X$KTIFP;
KTIFPNO KTIFPPSI
---------- ----------
0 65535
1 65535
2 65535
3 16
4 65535
5 65535
6 65535
7 65535
8 65535
9 65535
10 65535
KTIFPNO KTIFPPSI
---------- ----------
11 65535
12 65535
13 65535
14 65535
15 65535
16 65535
17 65535
18 rows selected.
Oracle alert日志也可以看到分配给imu pool数量,用ILAT =18表示
引用
Autotune of undo retention is turned on.
IMODE=BR
ILAT =18
LICENSE_MAX_USERS = 0
SYS auditing is disabled
通过以下视图可以查看Oracle触发imu刷新内存的条件
引用
SQL> col INDX for 999
SQL> col KTIFFCAT for a50
SQL> select indx,ktiffcat,ktiffflc from X$KTIFF;
INDX KTIFFCAT KTIFFFLC
---- -------------------------------------------------- ----------
0 Undo pool overflow flushes 0
1 Stack cv flushes 34142
2 Multi-block undo flushes 0
3 Max. chgs flushes 51607
4 NTP flushes 492
5 Contention flushes 679
6 Redo pool overflow flushes 1
7 Logfile space flushes 0
8 Multiple persistent buffer flushes 0
9 Bind time flushes 0
10 Rollback flushes 4945
INDX KTIFFCAT KTIFFFLC
---- -------------------------------------------------- ----------
11 Commit flushes 215244
12 Recursive txn flushes 49992
13 Redo only CR flushes 0
14 Ditributed txn flushes 0
15 Set txn use rbs flushes 0
16 Bitmap state change flushes 85557
17 Presumed commit violation 0
18 rows selected.
当系统启用imu,部分事务即可通过imu提交。
引用
SQL> select name,value from v$sysstat where name like '%commits%';
NAME VALUE
---------------------------------------------------------------- ----------
user commits 42785
IMU commits 40411
当然了Oracle使用imu特性也是通过latch保护的,每一个imu pool都有相应的latch保护。每个事务独立的使用imu pool。
引用
SQL> select LATCH#,NAME,GETS,MISSES from v$latch where name ='In memory undo latch';
LATCH# NAME GETS MISSES
---------- ---------------------------------------- ---------- ----------
192 In memory undo latch 14548635 74
SQL> select NAME,GETS,MISSES from v$latch_children where name='In memory undo latch';
NAME GETS MISSES
---------------------------------------- ---------- ----------
In memory undo latch 2997946 31
In memory undo latch 286606 4
In memory undo latch 6641899 13
In memory undo latch 4328804 13
In memory undo latch 113778 13
In memory undo latch 121081 0
In memory undo latch 58811 0
In memory undo latch 0 0
In memory undo latch 0 0
In memory undo latch 0 0
In memory undo latch 0 0
NAME GETS MISSES
---------------------------------------- ---------- ----------
In memory undo latch 0 0
In memory undo latch 0 0
In memory undo latch 0 0
In memory undo latch 0 0
In memory undo latch 0 0
In memory undo latch 0 0
In memory undo latch 0 0
18 rows selected.
简单总结:
Oracle 10g通过在shared_pool中开辟一小块内存,用来缓存未提交的小事务相关的undo block,然后通过一定的触发条件,有序批量的写出,有效的避免了不必要的读写操作,对于性能的提高启到了一定的作用。