SGA_MAX_SIZE是从9i以来就有的作为设置SGA大小的一个参数,而SGA_TARGET则是从10g才有的一个新参数,作为配合10g自动管理SGA而出现的,下面以实验的方式,深入解析这2个参数的区别和作用
[oracle@bak ~]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Mon Oct 13 01:59:11 2014
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 272M
sga_target big integer 272M
SQL> desc v$sgainfo
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(32)
BYTES NUMBER
RESIZEABLE VARCHAR2(3)
SQL> set pages 100
SQL> select name,bytes/1024/1024 "size(MB)",resizeable from v$sgainfo;
NAME size(MB) RES
-------------------------------- ---------- ---
Fixed SGA Size 1.16254425 No
Redo Buffers 2.8359375 No
Buffer Cache Size 172 Yes
Shared Pool Size 80 Yes
Large Pool Size 4 Yes
Java Pool Size 4 Yes
Streams Pool Size 8 Yes
Granule Size 4 No
Maximum SGA Size 272 No --SGA_MAX_SIZE对应的值
Startup overhead in Shared Pool 36 No
Free SGA Memory Available 0
11 rows selected.
注意,resizeable值为NO的,都是不可动态调整的值,并且由于SGA_TARGET和SGA_MAX_SIZE的大小一致,因此Free SGA Memory Available=0,而所有可动态调整的几个内存组件再加上一个4M Granule Size,则正好等于SGA_TARGET的值
SQL> select (172 + 80 + 4 + 4 + 8 + 4) "sga_target(MB)" from dual;
sga_target(MB)
--------------
272
我们也可以从v$sga_dynamic_components这个视图了解各内存组件的内存分配情况,和v$sgainfo内容基本是一致的,除了没有展示Gradual Size这一项
SQL> select component,current_size/1024/1024 "size(MB)" from v$sga_dynamic_components;
COMPONENT size(MB)
---------------------------------------------------------------- ----------
shared pool 80
large pool 4
java pool 4
streams pool 8
DEFAULT buffer cache 172
KEEP buffer cache 0
RECYCLE buffer cache 0
DEFAULT 2K buffer cache 0
DEFAULT 4K buffer cache 0
DEFAULT 8K buffer cache 0
DEFAULT 16K buffer cache 0
DEFAULT 32K buffer cache 0
ASM Buffer Cache 0
13 rows selected.
SGA = shared pool + large pool + java pool + streams pool + DEFAULT buffer cache = 80 +4 + 4 + 8 + 172 =268M
这个算出来的SGA值比SGA_TARGET的值少了4M,其实就是那个Gradual Size的值
SQL> col name for a35
SQL> select name,issys_modifiable from v$parameter where name like '%sga%';
NAME ISSYS_MOD
----------------------------------- ---------
sga_max_size FALSE
pre_page_sga FALSE
lock_sga FALSE
sga_target IMMEDIATE
ISSYS_MODIFIABLE列的值为FALSE的,就是不能动态修改的参数,需重启生效,为IMMEDIATE的,即可以动态修改
SQL> alter system set sga_target=350m;
alter system set sga_target=350m
*
ERROR at line 1:
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-00823: Specified value of sga_target greater than sga_max_size
如果不带scope参数,默认为both,但要在内存中生效,必须满足SGA_TARGET<SGA_MAX_SIZE的条件,因此必须指定scope=spfile,因此虽然是动态参数,但是依旧要重启后生效
SQL> alter system set sga_target=350m scope=spfile;
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 369098752 bytes
Fixed Size 1219472 bytes
Variable Size 100664432 bytes
Database Buffers 264241152 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 352M
sga_target big integer 352M
可以看到,当调整了SGA_TARGET参数后,尽管没有去手动调整SGA_MAX_SIZE的值,但现在它自动和修改后的SGA_TARGET的值匹配了,从272M调整为352M,这里还有个比较有意思的地方,就是指定了350M,为何会变为352M,这多出来的2M又是什么呢?
SQL> select 350/4 "Granual Count" from dual;
Granual Count
-------------
87.5
SQL> select 88*4 "SGA_SIZE(MB)" from dual;
SGA_SIZE(MB)
------------
352
其实很容易理解,刚才说了Granual Size的大小为4M,由于Granual必须完整地分配,因此会分配给SGA共88个Granual,即88*4=352M,也就是多给了2M
再来看一下刚才那些内存组件的内存分配情况
SQL> select name,bytes/1024/1024 "size(MB)",resizeable from v$sgainfo;
NAME size(MB) RES
----------------------------------- ---------- ---
Fixed SGA Size 1.16297913 No
Redo Buffers 2.8359375 No
Buffer Cache Size 252 Yes
Shared Pool Size 80 Yes
Large Pool Size 4 Yes
Java Pool Size 4 Yes
Streams Pool Size 8 Yes
Granule Size 4 No
Maximum SGA Size 352 No
Startup overhead in Shared Pool 36 No
Free SGA Memory Available 0
11 rows selected.
SQL> select component,current_size/1024/1024 "size(MB)" from v$sga_dynamic_components;
COMPONENT size(MB)
---------------------------------------------------------------- ----------
shared pool 80
large pool 4
java pool 4
streams pool 8
DEFAULT buffer cache 252
KEEP buffer cache 0
RECYCLE buffer cache 0
DEFAULT 2K buffer cache 0
DEFAULT 4K buffer cache 0
DEFAULT 8K buffer cache 0
DEFAULT 16K buffer cache 0
DEFAULT 32K buffer cache 0
ASM Buffer Cache 0
13 rows selected.
SQL>
当我们把SGA_TARGET从272M调整为350M时,由于Gruanual的存在,Oracle实际给SGA分配了352M的内存,而这352M内存中,其他内存组件的大小都没有发生变化,仅仅只是db buffer cache的值由之前的172M调整到了252M
之所以db buffer cache size随着SGA_TARGET的值自动调整,是因为这时10g的新特性,只要SGA_TARGET为非零值,那么内存组件就是采用动态分配原则,由Oracle自动调整各内存组件的大小。不过有个地方挺奇怪的,虽然从启动实例时分配地内存也好,从v$sgainfo以及v$sga_dynamic_components中也好,看到的值都是252M,而从参数看db_cache_size的值,仍然是172M,不知何故,难道是bug?
SQL> show parameter db_cache
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_cache_advice string ON
db_cache_size big integer 172M
刚才是直接修改SGA_TARGET而不修改SGA_MAX_SIZE的情况,我们再来看一下反过来的情况,先把SGA_TARGET调整回272M
SQL> alter system set sga_target=270m scope=spfile;
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 1219016 bytes
Variable Size 100664888 bytes
Database Buffers 180355072 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 272M
sga_target big integer 272M
注意,一旦SGA_TARGET动了,重启数据库后,SGA_MAX_SIZE会跟着调整,也一起回到了272M的状态,但与增大SGA_TARGET时的情况不同,也可以选择不重启数据库,那么SGA_MAX_SIZE的值就不会做调整,而内存组件的值依然会跟着SGA_TARGET走(体现动态调整)
SQL> alter system set sga_target=270m;
System altered.
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 352M
sga_target big integer 272M
由于是减小SGA_TARGET的值,因此不受必须比TARGET_MAX_SIZE小的这个条件限制,因此数据库无需重启
SQL> select name,bytes/1024/1024 "size(MB)",resizeable from v$sgainfo;
NAME size(MB) RES
----------------------------------- ---------- ---
Fixed SGA Size 1.16297913 No
Redo Buffers 2.8359375 No
Buffer Cache Size 172 Yes --仅仅调整了db buffer cache,由252M变为172M
Shared Pool Size 80 Yes
Large Pool Size 4 Yes
Java Pool Size 4 Yes
Streams Pool Size 8 Yes
Granule Size 4 No
Maximum SGA Size 352 No
Startup overhead in Shared Pool 36 No
Free SGA Memory Available 80
11 rows selected.
刚才是直接调整SGA_TARGET,我们再来看一下只调整SGA_MAX_SIZE的情况,先重启数据库,使SGA_MAX_SIZE的值回到272M
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 1219016 bytes
Variable Size 100664888 bytes
Database Buffers 180355072 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 272M
sga_target big integer 272M
SQL> alter system set sga_max_size=350m scope=spfile;
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 369098752 bytes
Fixed Size 1219496 bytes
Variable Size 184550488 bytes
Database Buffers 180355072 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 352M
sga_target big integer 272M
SQL> select name,bytes/1024/1024 "size(MB)",resizeable from v$sgainfo;
NAME size(MB) RES
----------------------------------- ---------- ---
Fixed SGA Size 1.16300201 No
Redo Buffers 2.8359375 No
Buffer Cache Size 172 Yes
Shared Pool Size 80 Yes
Large Pool Size 4 Yes
Java Pool Size 4 Yes
Streams Pool Size 8 Yes
Granule Size 4 No
Maximum SGA Size 352 No
Startup overhead in Shared Pool 36 No
Free SGA Memory Available 80
11 rows selected.
注意,仅仅调整SGA_MAX_SIZE后,SGA_TARGET并不会跟着调整,因此各内存组件的值也不会调整,而这时我们发现Free SGA Memory Available的值为80M,而不再是之前的0了,调整SGA_MAX_SIZE为SGA_TARGET增加了80M的上限,这80就是352-272得到的,相当于做了个预留,告诉SGA,你可以现在只用172M,但如果想增加,可以再多用80M,达到352M。而这个时候只要SGA_TARGET重新设置的值没有超过SGA_MAX_SIZE的值,就可以在不停库的情况下增加SGA大小,这样做是有好处的
最后,我们还可以把SGA_TARGET设置为0,即表示禁用10g的新特性——自动分配内存,这样我们可以对各个内存组件的值进行单独设置,对于特殊的应用场景,有时候也是需要的,因为自动管理只是oracle提供的一种便利行,但并不意味着自动调整就一定万事皆OK,11g的自动内存管理就一度被诟病,这里不讨论
SQL> alter system set sga_target=0;
System altered.
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 352M
sga_target big integer 0
SQL> select name,bytes/1024/1024 "size(MB)",resizeable from v$sgainfo;
NAME size(MB) RES
----------------------------------- ---------- ---
Fixed SGA Size 1.16300201 No
Redo Buffers 2.8359375 No
Buffer Cache Size 172 Yes
Shared Pool Size 80 Yes
Large Pool Size 4 Yes
Java Pool Size 4 Yes
Streams Pool Size 8 Yes
Granule Size 4 No
Maximum SGA Size 352 No
Startup overhead in Shared Pool 36 No
Free SGA Memory Available 80
设置SGA_TARGET=0以后,已经分配的内存组件的值不会变化,如果不设置新值,那么依旧保持原来的值,除非单独设置,即使从起数据库实例也一样,SGA_MAX_TARGET依然不会再根据SGA_TARGET调整,因为SGA_MAX_TARGET我们是给过它确定的值的
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 369098752 bytes
Fixed Size 1219496 bytes
Variable Size 184550488 bytes
Database Buffers 180355072 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SQL> show parameter sge
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 352M --重启后依然是352M,并不会变为0
sga_target big integer 0
SQL> select name,bytes/1024/1024 "size(MB)",resizeable from v$sgainfo;
NAME size(MB) RES
----------------------------------- ---------- ---
Fixed SGA Size 1.16300201 No
Redo Buffers 2.8359375 No
Buffer Cache Size 172 Yes
Shared Pool Size 80 Yes
Large Pool Size 4 Yes
Java Pool Size 4 Yes
Streams Pool Size 8 Yes
Granule Size 4 No
Maximum SGA Size 352 No
Startup overhead in Shared Pool 36 No
Free SGA Memory Available 80
11 rows selected.
SQL> alter system set sga_target=270m;
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 369098752 bytes
Fixed Size 1219496 bytes
Variable Size 184550488 bytes
Database Buffers 180355072 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 352M
sga_target big integer 272M
重启数据库实例后,SGA_MAX_SIZE的值没有跟着SGA_TARGET做调整,除非再次增大SGA_TARGET的大小并超过SGA_MAX_SIZE的上限,那么下次重启后,SGA_MAX_SIZE又会再一次跟着SGA_TARGET调整
SQL> alter system set sga_target=380m scope=spfile;
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 398458880 bytes
Fixed Size 1219640 bytes
Variable Size 100664264 bytes
Database Buffers 293601280 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 380M
sga_target big integer 380M
SGA_MAX_SIZE又再一次和SGA_TARGET的值一致了,并且380正好可以除尽4,共分配95个Granual,因此不会有多2M的问题
总结:
1.SGA_MAX_SIZE是静态参数,而SGA_TARGET可以动态修改,当要改的SGA_TARGET值超过SGA_MAX_SIZE的值时,因为不允许在内存中直接生效,即scope=both或memory都不行,因此必须指定scope=spfile,重启后才能修改成功。如果此时没有设置过SGA_MAX_SIZE得值,那么无论是改大还是改小,重启数据库后,SGA_MAX_SIZE都回跟着SGA_TARGET做调整。
2.当先给SGA_MAX_SIZE设置了一个较大的值,重启数据库后,SGA_TARGET可以依然保持原有大小,相当于给SGA_TARGET先预留了一些内存,预先设置好可SGA可分配内存,等到数据库出现压力导致SGA内存不足,可以直接通过SGA_TARGET动态修改到这一上限而无需停库。
3.当给SGA_TARGET设置非零值时,表示采用内存组件内存由oracle动态调整,如shared pool,db buffer cache等,这些内存组件只会跟着SGA的大小动态进行调整(增大或减小),与其他值无关。
4.当SGA_TARGET设置为零时,表示禁用内存组件由SGA自动管理,如果未做单独设置,那么保持原有值。此时可以根据具体场景需求,单独
为某个内存组件配置合理的内存大小。
5.如果是先设置了SGA_MAX_SIZE的值,再设置了SGA_TARGET,那么只有当SGA_TARGET设置的值超过SGA_MAX_SIZE的值时,SGA_MAX_SIZE才会在重启生效后,调整到与SGA_TARGET的值一致,反之则不会改变。