首先我们来看下什么是archive log file,Oracle 11g 的concept中是这样定义的:When you enable archiving of the online redo logs, Oracle Database copies the online redo log files to another location before they are overwritten. These copied files are referred to as archived redo log files. 那么根据这个定义,archive log file就是redo log file的拷贝,既然是拷贝,在排除压缩的情况下,两种文件的大小应该是一致的。但是我们在真实环境中看到的archive log file就是redo log file却不是一样大,真实情况是archive log file比redo log file小,极端情况下,甚至会小非常多。
引起archive log file就是redo log file大小不一致的原因大致有如下几种:
一、人为操作类型
1、SQL>alter system switch logfile;
2、SQL>alter system archive log current;
3、RMAN>backup archive log all;
4、RMAN>backup database plus archivelog;
二、参数设置类型
archive_lag_target:日志切换的强制时间间隔,即只要到达该参数设置的时间间隔,无论redo 文件是否写满,都会进行日志切换。
三、oracle bug类型
BUG 9272059、BUG 10354739、BUG 12317474、BUG 5450861、BUG 7016254
下面对archive log file就是redo log file大小不一致的原因进行分析,首先,如果redo log file中是以空白结尾,那么,archive log file中会将末尾的空白去除,这就样就会出现archive log比redo log file小,具体小多少,就根据归档时redo log file末尾的空白大小决定。这种情形常见于前面提到的认为操作类型和参数设置类型。因为在进行强制切换日志的时候,redo log file是没有被写满的,文件的末尾必然存在空白。
另外,日志切换并不是发生redo log file 100%满的时候,这是由于oracle的内部算法决定的,这样做的主要目的是处于性能的考虑。所以redo log file始终不会被100%的写满,在进行归档的时候,末尾的空白会被丢弃,所以就导致了archive log file小于redo log file。影响redo log切换时间的因素有:LOG_BUFFER_SIZE参数设置、系统负载、log file size、logfile 空间分配算法。
CUP_COUNT值会影响logfile空间分配算法,所以,如果出现日志频繁切换且归档日志远小于redo log file的情况,请检查CUP_COUNT是否符合系统的实际情况。
再次,如果是RAC环境,如果各节点的负载不一致,为了保证数据库的可恢复性,空闲节点会进行一些的日志切换,主要是为了增进redo 日志的FIRST_CHANGE#,空闲节点产生的归档日志大小会与redo file大小有较大差距。下面进行验证:
--查看redo file大小
SQL> select thread#,group#,bytes/1024/1024 "size" from v$log order by 1,2;
THREAD# GROUP# size
---------- ---------- ----------
1 1 50
1 2 50
2 3 50
2 4 50
--在节点1上建立测试表
SQL> create table darren(id number,item varchar2(2));
--查看当前的归档情况和redo log的FRIST_CHANGE#
SQL> select thread#,name,blocks*block_size/1024/1024 "size" from v$archived_log order by 1,2;
THREAD# NAME size
---------- ---------------------------------------------------------------------- ----------
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_10.268.861729569 1.4453125
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_11.270.861730475 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_12.271.861730509 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_13.272.861730545 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_14.274.861730573 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_15.275.861730601 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_16.276.861788401 35.8242188
2 +DATADG01/gyl/archivelog/2014_10_23/thread_2_seq_2.269.861729571 2.37207031
2 +DATADG01/gyl/archivelog/2014_10_23/thread_2_seq_3.273.861730551 .008300781
2 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_4.277.861788403 .567871094
SQL> select GROUP#,THREAD#,SEQUENCE#,STATUS,FIRST_CHANGE# from v$log order by 2;
GROUP# THREAD# SEQUENCE# STATUS FIRST_CHANGE#
---------- ---------- ---------- ---------------- -------------
1 1 17 CURRENT 794878
2 1 16 INACTIVE 700672
3 2 5 CURRENT 794876
4 2 4 INACTIVE 517670
--在节点1上进行事务,由于是测试环境,节点2上完全没事务,是空闲实例
begin
for i in 1..500000 loop
insert into darren values(1,'aa');
commit;
end loop;
end;
--查看归档情况和redo log 的FRIST_CHANGE#
SQL> select thread#,name,blocks*block_size/1024/1024 "size" from v$archived_log order by 1,2;
THREAD# NAME size
---------- ---------------------------------------------------------------------- ----------
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_10.268.861729569 1.4453125
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_11.270.861730475 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_12.271.861730509 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_13.272.861730545 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_14.274.861730573 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_15.275.861730601 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_16.276.861788401 35.8242188
1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_17.278.861791005 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_18.279.861791039 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_19.281.861791071 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_20.282.861791091 49.9980469
1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_21.283.861791119 49.9980469
2 +DATADG01/gyl/archivelog/2014_10_23/thread_2_seq_2.269.861729571 2.37207031
2 +DATADG01/gyl/archivelog/2014_10_23/thread_2_seq_3.273.861730551 .008300781
2 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_4.277.861788403 .567871094
2 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_5.280.861791047 .791503906
2 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_6.284.861791125 .000976563
(thread_1_seq_17至thread_1_seq_21为insert过程中节点1产生的归档,大小都接近redo file大小,thread_2_seq_5和thread_2_seq_6为节点2产生的归档,远小于redo file大小)
SQL> select GROUP#,THREAD#,SEQUENCE#,STATUS,FIRST_CHANGE# from v$log order by 2;
GROUP# THREAD# SEQUENCE# STATUS FIRST_CHANGE#
---------- ---------- ---------- ---------------- -------------
1 1 21 ACTIVE 1206256
2 1 22 CURRENT 1308608
3 2 7 CURRENT 1338258
4 2 6 INACTIVE 1014874
(可以看到,节点2的FIRST_CHANGE#也跟进了,这里还超过了节点1的)
再考虑一种极端情况,如果节点2已经down了,那么,节点2的归档将会由节点1进行代为执行,同样会推进节点2的redo log的FIRST_CHANGE#,继续上面的实验:
--关闭节点2
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
--继续在节点1插入数据
begin
for i in 1..500000 loop
insert into darren values(1,'aa');
commit;
end loop;
end;
--查看归档情况和redo log 的FRIST_CHANGE#
SQL> select thread#,ARCHIVAL_THREAD#,name,blocks*block_size/1024/1024 "size" from v$archived_log order by 1,2;
THREAD# ARCHIVAL_THREAD# NAME size
---------- ---------------- ---------------------------------------------------------------------- ----------
1 1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_10.268.861729569 1.4453125
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_18.279.861791039 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_27.291.861795885 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_26.290.861795861 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_25.289.861795837 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_24.287.861795815 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_23.286.861795789 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_22.285.861795765 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_11.270.861730475 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_12.271.861730509 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_13.272.861730545 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_14.274.861730573 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_23/thread_1_seq_15.275.861730601 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_16.276.861788401 35.8242188
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_17.278.861791005 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_19.281.861791071 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_20.282.861791091 49.9980469
1 1 +DATADG01/gyl/archivelog/2014_10_24/thread_1_seq_21.283.861791119 49.9980469
2 1 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_8.292.861795895 .000488281
2 1 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_7.288.861795827 .921386719
2 1 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_4.277.861788403 .567871094
2 2 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_5.280.861791047 .791503906
2 2 +DATADG01/gyl/archivelog/2014_10_23/thread_2_seq_3.273.861730551 .008300781
2 2 +DATADG01/gyl/archivelog/2014_10_24/thread_2_seq_6.284.861791125 .000976563
2 2 +DATADG01/gyl/archivelog/2014_10_23/thread_2_seq_2.269.861729571 2.37207031
(注意thread_2_seq_7和thread_2_seq_8,他们的归档是由thread 1 执行的,参看THREAD# 和ARCHIVAL_THREAD# 列,这两个归档正是在实例2关闭的时候生成的)
SQL> select GROUP#,THREAD#,SEQUENCE#,STATUS,FIRST_CHANGE# from v$log order by 2;
GROUP# THREAD# SEQUENCE# STATUS FIRST_CHANGE#
---------- ---------- ---------- ---------------- -------------
1 1 27 ACTIVE 1831962
2 1 28 CURRENT 1935906
3 2 9 CURRENT 1955175
4 2 8 INACTIVE 1690602
由于redo wastage的存在,redo log file中间也会存在空白,那这部分空白会不会被丢弃呢?首先看下什么是redo wastage,简单的说就是LGWR进程在写redo log file的时候是按操作系统的标准块为单位进行写入的,具体块的大小,可以使用下述语句进行查询:
1. select max(l.lebsz) log_block_size_kccle
2. from sys.x$kccle l
3. where l.inst_id = userenv('Instance');
假设标准块大小为512字节,在一次写入操作中一共要写入1036字节数据,那么就需要3个标准块,尽管第三个块没有被写满,但是SGA中redo log写入的指针会跳转到下面一个块,这里的第三个块剩下的空间就被浪费了,这就是redo wastage。减少这种情况的方法就是减少commit次数。
下面通过实验观察redo wastage造成的空白会不会在归档的时候被丢弃:
--查看redo file大小
SQL> select group#,bytes/1024/1024 "size(M)" from v$log;
GROUP# size(M)
---------- ----------
1 50
2 50
3 50
--建立测试表
SQL> create table darren(id number,item varchar2(2));
Table created.
--查看当前归档的情况
SQL> select SEQUENCE#,ARCHIVED,status,COMPRESSED from v$archived_log;
SEQUENCE# ARC S COM
---------- --- - ---
81 YES A NO
82 YES A NO
83 YES A NO
84 YES A NO
85 YES A NO
86 YES A NO
87 YES A NO
88 YES A NO
89 YES A NO
90 YES A NO
--查看当前的redo size和redo wastage
SQL> select name,value from v$sysstat where name in('redo size','redo wastage');
NAME VALUE
--------------------- ------------------------------------------ ----------
redo size 258664912
redo wastage 86181420
--向测试表插入数据,产生redo记录
begin
for i in 1..500000 loop
insert into darren values(1,'aa');
commit;
end loop;
end;
--切换一起日志,将insert过程中产生的redo文件全部归档
SQL> alter system archive log current;
System altered.
--查看现在的redo size和redo wastage
SQL> select name,value from v$sysstat where name in('redo size','redo wastage');
NAME VALUE
---------- ------------
redo size 512888704
redo wastage 202172176
--计算insert过程中产生的redo size和redo wastage
SQL> select 512888704-258664912 redo from dual;
REDO
----------
254223792
SQL> select 202172176-86181420 wastage from dual;
WASTAGE
----------
115990756
--计算redo wastage的比例
SQL> select 115990756/254223792 from dual;
115990756/254223792
-------------------
.456254527
--查看insert 过程中产生的archive log file
SQL> select SEQUENCE#,ARCHIVED,status,COMPRESSED from v$archived_log;
SEQUENCE# ARC S COM
---------- --- - ---
81 YES A NO
82 YES A NO
83 YES A NO
84 YES A NO
85 YES A NO
86 YES A NO
87 YES A NO
88 YES A NO
89 YES A NO
90 YES A NO
91 YES A NO
92 YES A NO
93 YES A NO
94 YES A NO
95 YES A NO
96 YES A NO
97 YES A NO
98 YES A NO
99 YES A NO
从91号归档开始为本次insert操作产生的归档
--查看归档文件大小
[oracle@oracle11g archive]$ ls -trl
-rw-r----- 1 oracle oinstall 49917440 Oct 23 13:49 orcl_1_91_851966182.arc
-rw-r----- 1 oracle oinstall 49257472 Oct 23 13:49 orcl_1_92_851966182.arc
-rw-r----- 1 oracle oinstall 49896448 Oct 23 13:50 orcl_1_93_851966182.arc
-rw-r----- 1 oracle oinstall 44149760 Oct 23 13:50 orcl_1_94_851966182.arc
-rw-r----- 1 oracle oinstall 49917440 Oct 23 13:50 orcl_1_95_851966182.arc
-rw-r----- 1 oracle oinstall 44199936 Oct 23 13:50 orcl_1_96_851966182.arc
-rw-r----- 1 oracle oinstall 46582784 Oct 23 13:51 orcl_1_97_851966182.arc
-rw-r----- 1 oracle oinstall 48513536 Oct 23 13:51 orcl_1_98_851966182.arc
-rw-r----- 1 oracle oinstall 13312 Oct 23 13:51 orcl_1_99_851966182.arc
根据实验数据显示,redo wastage的比例约为46%,redo log大小为50M,忽略文件末尾的空白影响,如果归档时丢弃redo wastage产生的日志文件中间的空白,那么,归档文件的大小约为50*1024*1024*46%=24117248字节。从实验数据看,归档日志都远大于24117248字节(不考虑99号日志,该日志为手动切换产生)。
结论:归档时不会丢弃由于redo wastage产生的redo log file中间的空白。
另外再说明一点,由于某些BUG的存在,会出现redo log切换非常频繁,产生的归档都远小于redo log file的大小,所以,在观察到redo log切换频繁的时候,要关注下归档日志的大小,如归归档日志远小于redo log file大小,这时造成redo log频繁切换的原因可能不是大量的事务,这时要综合考虑,不要贸然加大redo log file大小。
: