---
title: "Oracle Redo log并行机制详解"
author: "meng702"
date: "2011-04-20T01:31:20Z"
tags: ["Oracle", "SQL", "活动"]
categories: ["数据库"]
slug: "oracle-redo-log"
draft: false
lastmod: "2011-04-20T01:31:20Z"
id: 1008641
url: "https://meng702.iteye.com/blog/1008641"
---

转载:http://database.51cto.com/art/201001/175898\_1.htm

Oracle的数据库日志称为Redo log，所有数据改变都记录Redo log，可以用于修复受损的数据库。Redo log 是用于恢复和一个高级特性的重要数据，一个redo条目包含了相应操作导致的数据库变化的所有信息，所有redo条目最终都要被写入redo文件中去。

Redo log buffer是为了避免Redo文件IO导致性能瓶颈而在sga中分配出的一块内存。一个redo条目首先在用户内存(PGA)中产生，然后由oracle服务进程拷贝到log buffer中，当满足一定条件时，再由LGWR进程写入redo文件。由于log buffer是一块“共享”内存，为了避免冲突，它是受到redo allocation latch保护的，每个服务进程需要先获取到该latch才能分配redo buffer。因此在高并发且数据修改频繁的oltp系统中，我们通常可以观察到redo allocation latch的等待。Redo写入redo buffer的整个过程如下：

在PGA中生产Redo Enrey -&gt; 服务进程获取Redo Copy latch(存在多个---CPU\_COUNT\*2) -&gt; 服务进程获取redo allocation latch(仅1个) -&gt; 分配log buffer -&gt; 释放redo allocation latch -&gt; 将Redo Entry写入Log Buffer -&gt; 释放Redo Copy latch;

shared strand

为了减少redo allocation latch等待，在oracle 9.2中，引入了log buffer的并行机制。其基本原理就是，将log buffer划分为多个小的buffer，这些小的buffer被成为strand(为了和之后出现的private strand区别，它们被称之为shared strand)。每一个strand受到一个单独redo allocation latch的保护。多个shared strand的出现，使原来序列化的redo buffer分配变成了并行的过程，从而减少了redo allocation latch等待。

shared strand的初始数据量是由参数log\_parallelism控制的;在10g中，该参数成为隐含参数，并新增参数\_log\_parallelism\_max控制shared strand的最大数量;\_log\_parallelism\_dynamic则控制是否允许shared strand数量在\_log\_parallelism和\_log\_parallelism\_max之间动态变化。

SQL&gt;select  nam.ksppinm, val.KSPPSTVL, nam.ksppdesc from sys.x$ksppi nam,  sys.x$ksppsv val where nam.indx = val.indx AND   nam.ksppinm LIKE '\_%' AND   upper(nam.ksppinm) LIKE '%LOG\_PARALLE%';      

      

KSPPINM               KSPPSTVL   KSPPDESC      

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

\_log\_parallelism           1          Number of log buffer strands      

\_log\_parallelism\_max       2          Maximum number of log buffer strands      

\_log\_parallelism\_dynamic   TRUE       Enable dynamic strands  

每一个shared strand的大小 = log\_buffer/(shared strand数量)。strand信息可以由表x$kcrfstrand查到(包含shared strand和后面介绍的private strand，10g以后存在)。

SQL&gt;select indx,strand\_size\_kcrfa from x$kcrfstrand where last\_buf\_kcrfa != '00';      

       INDX STRAND\_SIZE\_KCRFA      

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

         0           3514368      

         1           3514368      

     

SQL&gt;show parameter log\_buffer      

     

NAME                                 TYPE        VALUE      

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

log\_buffer                           integer     7028736    

关于shared strand的数量设置，16个cpu之内最大默认为2，当系统中存在redo allocation latch等待时，每增加16个cpu可以考虑增加1个strand，最大不应该超过8。并且\_log\_parallelism\_max不允许大于cpu\_count。

注意：在11g中，参数\_log\_parallelism被取消，shared strand数量由\_log\_parallelism\_max、\_log\_parallelism\_dynamic和cpu\_count控制。

Private strand

为了进一步降低redo buffer冲突，在10g中引入了新的strand机制——Private strand。Private strand不是从log buffer中划分的，而是在shared pool中分配的一块内存空间。

SQL&gt;select * from V$sgastat where name like '%strand%';  

 

POOL NAME BYTES  

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

shared pool private strands 2684928  

 

HELLODBA.COM&gt;select indx,strand\_size\_kcrfa from x$kcrfstrand where last\_buf\_kcrfa = '00';  

 

INDX STRAND\_SIZE\_KCRFA  

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

2 66560  

3 66560  

4 66560  

5 66560  

... ...

Private strand的引入为Oracle的Redo/Undo机制带来很大的变化。每一个Private strand受到一个单独的redo allocation latch保护，每个Private strand作为“私有的”strand只会服务于一个活动事务。获取到了Private strand的用户事务不是在PGA中而是在Private strand生成Redo，当flush private strand或者commit时，Private strand被批量写入log文件中。如果新事务申请不到Private strand的redo allocation latch，则会继续遵循旧的redo buffer机制，申请写入shared strand中。事务是否使用Private strand，可以由x$ktcxb的字段ktcxbflg的新增的第13位鉴定：

SQL&gt;select decode(bitand(ktcxbflg, 4096),0,1,0) used\_private\_strand, count(\*)   from x$ktcxb   where bitand(ksspaflg, 1) != 0   and bitand(ktcxbflg, 2) != 0   group by bitand(ktcxbflg, 4096);  

 

USED\_PRIVATE\_STRAND COUNT(\*)  

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

1 10  

0 1 

对于使用Private strand的事务，无需先申请Redo Copy Latch，也无需申请Shared Strand的redo allocation latch，而是flush或commit是批量写入磁盘，因此减少了Redo Copy Latch和redo allocation latch申请/释放次数、也减少了这些latch的等待，从而降低了CPU的负荷。过程如下：

事务开始 -&gt; 申请Private strand的redo allocation latch (申请失败则申请Shared Strand的redo allocation latch) -&gt; 在Private strand中生产Redo Enrey -&gt; Flush/Commit -&gt; 申请Redo Copy Latch -&gt; 服务进程将Redo Entry批量写入Log File -&gt; 释放Redo Copy Latch -&gt; 释放Private strand的redo allocation latch 。 

注意：对于未能获取到Private strand的redo allocation latch的事务，在事务结束前，即使已经有其它事务释放了Private strand，也不会再申请Private strand了。

每个Private strand的大小为65K。10g中，shared pool中的Private strands的大小就是活跃会话数乘以65K，而11g中，在shared pool中需要为每个Private strand额外分配4k的管理空间，即：数量\*69k。--10g:  

SQL&gt; select * from V$sgastat where name like '%strand%';  

POOL NAME BYTES  

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

shared pool private strands 1198080  

 

SQL&gt;select trunc(value * KSPPSTVL / 100) * 65 * 1024  

from (select value from v$parameter where name = 'transactions') a,  

(select val.KSPPSTVL   from sys.x$ksppi nam, sys.x$ksppsv val  where nam.indx = val.indx  

AND nam.ksppinm = '\_log\_private\_parallelism\_mul') b;  

 

TRUNC(VALUE\*KSPPSTVL/100)\*65\*1024  

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

1198080 

 

--11g:  

SQL&gt;select * from V$sgastat where name like '%strand%';  

POOL NAME BYTES  

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

shared pool private strands 706560 

 

SQL&gt;select trunc(value * KSPPSTVL / 100) * (65 + 4) * 1024  

from (select value from v$parameter where name = 'transactions') a,  

(select val.KSPPSTVL  

from sys.x$ksppi nam, sys.x$ksppsv val  where nam.indx = val.indx  AND nam.ksppinm = '\_log\_private\_parallelism\_mul') b;  

 

TRUNC(VALUE\*KSPPSTVL/100)\*(65+4)\*1024  

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

706560 

Private strand的数量受到2个方面的影响：logfile的大小和活跃事务数量。

参数\_log\_private\_mul指定了使用多少logfile空间预分配给Private strand，默认为5。我们可以根据当前logfile的大小(要除去预分配给log buffer的空间)计算出这一约束条件下能够预分配多少个Private strand：

SQL&gt;select bytes from v$log where status = 'CURRENT';  

BYTES  

----------  

52428800  

 

SQLM&gt;select trunc(((select bytes from v$log where status = 'CURRENT') - (select to\_number(value) from v$parameter where name = 'log\_buffer'))*   (select to\_number(val.KSPPSTVL)  

from sys.x$ksppi nam, sys.x$ksppsv val  where nam.indx = val.indx  

AND nam.ksppinm = '\_log\_private\_mul') / 100 / 66560)   as "calculated private strands" from dual;  

 

calculated private strands  

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

5  

 

SQL&gt;select count(1) "actual private strands" from x$kcrfstrand where last\_buf\_kcrfa = '00';  

 

actual private strands  

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

5 

当logfile切换后(和checkpoint一样，切换之前必须要将所有Private strand的内容flush到logfile中，因此我们在alert log中可能会发现日志切换信息之前会有这样的信息："Private strand flush not complete"，这是可以被忽略的)，会重新根据切换后的logfile的大小计算对Private strand的限制：

SQL&gt;alter system switch logfile;  

System altered.  

SQL&gt;select bytes from v$log where status = 'CURRENT';  

 

BYTES  

----------  

104857600  

 

SQL&gt;select trunc(((select bytes from v$log where status = 'CURRENT') - (select to\_number(value) from v$parameter where name = 'log\_buffer'))*  (select to\_number(val.KSPPSTVL)   from sys.x$ksppi nam, sys.x$ksppsv val  where nam.indx = val.indx  AND nam.ksppinm = '\_log\_private\_mul') / 100 / 66560) as "calculated private strands" from dual;  

 

calculated private strands  

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

13  

 

SQL&gt;select count(1) "actual private strands" from x$kcrfstrand where last\_buf\_kcrfa = '00';  

 

actual private strands  

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

13 

参数\_log\_private\_parallelism\_mul用于推算活跃事务数量在最大事务数量中的百分比，默认为10。Private strand的数量不能大于活跃事务的数量。

SQL&gt;show parameter transactions  

 

NAME TYPE VALUE  

\------------------------------------

 

transactions integer 222  

 

transactions\_per\_rollback\_segment integer 5  

 

SQL&gt;select trunc((select to\_number(value) from v$parameter where name = 'transactions') *  (select to\_number(val.KSPPSTVL)  

from sys.x$ksppi nam, sys.x$ksppsv val  where nam.indx = val.indx  

AND nam.ksppinm = '\_log\_private\_parallelism\_mul') / 100 )  as "calculated private strands"  from dual;  

 

calculated private strands  

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

22  

 

SQL&gt;select count(1) "actual private strands" from x$kcrfstrand where last\_buf\_kcrfa = '00';  

 

actual private strands  

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

22 

注：在预分配Private strand时，会选择上述2个条件限制下最小一个数量。但相应的shared pool的内存分配和redo allocation latch的数量是按照活跃事务数预分配的

因此，如果logfile足够大，\_log\_private\_parallelism\_mul与实际活跃进程百分比基本相符的话，Private strand的引入基本可以消除redo allocation latch的争用问题。