PGA管理

PGA(Program Global Area)指的是程序全局区,是用于单数据库服务进程(Server Process)的一块包含数据和控制信息的内存区域。

当用户连接Oracle 数据库实例时,会创建出一个session用于服务进程和客户端间的通讯。每一个服务进程都有自己的PGA。

PGA用于处理SQL语句并且控制登录和其他会话信息。大部分的PGA区域被用于SQL work areas,这个工作区域主要用于SQL的排序或其他操作。

实例是以dedicated server(专用服务)或shared server(共享服务)模式运行,决定了PGA的内存使用总量。

在Oracle中PGA由一系列参数控制:

SQL> show parameter area_size
NAME                                 TYPE                              VALUE
------------------------------------ --------------------------------- ------------------------------
bitmap_merge_area_size               integer                           1048576
create_bitmap_area_size              integer                           8388608
hash_area_size                       integer                           131072
sort_area_size                       integer                           65536
workarea_size_policy                 string                            AUTO

Oracle为了方便PGA的管理,后续提供了一下中PGA的新管理方法,Automatic PGA Memory Management.通过这种特性,Oracle可以自动调整PGA的内存区,简化DBA的工作。

Oracle为这个新特性引入几个初始化参数:

PGA_AGGREGATE_TARGET 该参数指定所有session总计可以使用的最大PGA内存数,参数可以动态修改,取值范围从10M~(4096G-1)byte。

WORKAREA_SIZE_POLICY 此参数用于开关PGA自动管理功能。该参数有两个值AUTO和MANUAL,默认情况下该参数为开启自动管理功能。

 

pga_aggregate_target参数同时限制全局PGA和私有工作区内存分配。

对于串行操作,单个SQL操作能够使用PGA按照以下原则分配

MIN(PGA_AGGREGATE_TARGET*5%,100M)

对于并行操作,

(PGA_AGGREGATE_TARGET/dop)*30% dop degree of parallelism 并行度

 

PGA还分为TUNABLE MEMORY SIZE和UNTUNABLE MEMORY SIZE。而TUNABLE MEMORY SIZE是由SQL工作区使用的,其余区域为UNTUNABLE MEMORY SIZE。

自动PGA启用后,遵循以下规则:

TUNABLE MEMORY SIZE+UNTUNABLE MEMORY SIZE≤PGA_AGGREGATE_TARGET

对于PGA_AGGREGATE_TARGET参数的设置,Oracle提供一个建议方案:

对于OLTP系统

PGA_AGGREGATE_TARGET=(Total Physical Memory*80%)*20%

对于DSS系统

PGA_AGGREGATE_TARGET=(Total Physical Memory*80%)*50%

 

伴随新功能引入,Oracle同时在V$PROCESS视图中增加了相应的字段记录进程PGA的耗用:

[oracle@021Y-SH-BKAP ~]$ ps -ef | grep ora | head -1
oracle    5191     1  0 Feb21 ?        00:00:41 ora_pmon_orcl
SQL> col SPID for a10
SQL> col USERNAME for a10
SQL> select pid,spid,username,pga_used_mem,pga_alloc_mem,pga_freeable_mem,pga_max_mem from v$process where spid='5191';
       PID SPID       USERNAME   PGA_USED_MEM PGA_ALLOC_MEM PGA_FREEABLE_MEM PGA_MAX_MEM
---------- ---------- ---------- ------------ ------------- ---------------- -----------
         2 5191       oracle           694480        778384                0      778384

 

SQL在工作区内以三种方式执行:

  • Optimal 指所有处理可以再内存中完成。

  • Onepass 大部分操作可以再内存中完成,但也需要用到磁盘操作。

  • Multipass 大量操作产生磁盘交互。

通过以下语句查看数据库中SQL工作的效率

SELECT NAME,
       VALUE,
       ROUND(100 *
             (VALUE /
             DECODE((SELECT SUM(VALUE)
                       FROM V$SYSSTAT
                      WHERE NAME LIKE 'workarea executions%'),
                     0,
                     NULL,
                     (SELECT SUM(VALUE)
                        FROM V$SYSSTAT
                       WHERE NAME LIKE 'workarea executions%'))),
              3) PCT
  FROM V$SYSSTAT
 WHERE NAME LIKE 'workarea executions%'

 通过一个实验来观察PGA上限

SQL> alter system set pga_aggregate_target=&N;
Enter value for n: 10m
old   1: alter system set pga_aggregate_target=&N
new   1: alter system set pga_aggregate_target=10m
SQL> set line 300
SQL> show parameter pga_aggregate_target
NAME                  TYPE                 VALUE
--------------------- -------------------- --------------pga_aggregate_target  big integer          10M
SQL> select sql_text,operation_type,policy,last_memory_used/1024/1024 last_memory_used,last_execution,last_tempseg_size from v$sql l,v$sql_workarea a where l.hash_value=a.hash_value and sql_text='select * from dba_objects where rownum<50000';
SQL_TEXT                                           OPERATION_TYPE       POLICY     LAST_MEMORY_USED LAST_EXECU LAST_TEMPSEG_SIZE
-------------------------------------------------- -------------------- ---------- ---------------- ---------- -----------------
select * from dba_objects where rownum<50000       HASH-JOIN            AUTO             1.27246094 OPTIMAL
select * from dba_objects where rownum<50000       HASH-JOIN            AUTO             1.23632813 OPTIMAL
SQL> col name for a40
SQL> select name,value/1024/1024 MB from v$pgastat where name in('aggregate PGA target parameter','global memory bound');
NAME                                             MB
---------------------------------------- ----------
aggregate PGA target parameter                 2560
global memory bound                              10

 这个书中的实验我尝试在11G下做,但发现无论是修改隐藏参数还是修改动态参数,都无法改变视图v$pgastat 中的值。不知道PGA 大小是受什么限制的

SQL> @?/getparDescr.sql
Enter value for get_parameters:pga
NAME                       TYPE VALUE           VALUE           DEFAULTS                    DESCRIBE                        KSPPIHASH
-------------------- ---------- --------------- --------------- --------------------------- ------------------------------ ----------
__pga_aggregate_targ          6 10485760        10M             FALSE                       Current target size for the ag 1017923387
                                                                                          gregate PGA memory consumed
_pga_max_size                 6 20971520        20M             FALSE                       Maximum size of the PGA memory 2337595655
                                                                                             for one process
SQL> show parameter pga
NAME                      TYPE               VALUE
------------------------ ------------------- -------------
_pga_max_size             big integer        20M
pga_aggregate_target      big integer        10M

 对于

SQL> @?/getparDescr.sql
Enter value for get_parameters: smm
old  10:  and ksppinm like '%&get_parameters%'
new  10:  and ksppinm like '%smm%'
NAME                       TYPE VALUE           VALUE           DEFAULTS                    DESCRIBE                        KSPPIHASH
-------------------- ---------- --------------- --------------- --------------------------- ------------------------------ ----------
_smm_auto_min_io_siz          3 56              56              TRUE                        Minimum IO size (in KB) used b   23088013
e                                                                                           y sort/hash-join in auto mode
_smm_auto_max_io_siz          3 248             248             TRUE                        Maximum IO size (in KB) used b  876173738
e                                                                                           y sort/hash-join in auto mode
_smm_auto_cost_enabl          1 TRUE            TRUE            TRUE                        if TRUE, use the AUTO size pol 1976570560
ed                                                                                          icy cost functions
_smm_control                  3 0               0               TRUE                        provides controls on the memor 1609852098
                                                                                            y manager
_smm_trace                    3 0               0               TRUE                        Turn on/off tracing for SQL me 4181964803
                                                                                            mory manager
_smm_min_size                 3 1024            1024            TRUE                        minimum work area size in auto 2719042006
                                                                                             mode
_smm_max_size                 3 10240           10240           TRUE                        maximum work area size in auto 2148264545
                                                                                             mode (serial)
_smm_px_max_size              3 1310720         1310720         TRUE                        maximum work area size in auto 2315338250
                                                                                             mode (global)
_smm_retain_size              3 0               0               TRUE                        work area retain size in SGA f 3748517925
                                                                                            or shared server sessions (0 f
                                                                                            or AUTO)
_smm_bound                    3 0               0               TRUE                        overwrites memory manager auto 1005027971
                                                                                            matically computed bound
_smm_advice_log_size          3 0               0               TRUE                        overwrites default size of the 3174969030
                                                                                             PGA advice workarea history l
                                                                                            og
_smm_advice_enabled           1 TRUE            TRUE            TRUE                        if TRUE, enable v$pga_advice   1787600467
_smm_freeable_retain          3 5120            5120            TRUE                        value in KB of the instance fr 3791311125
                                                                                            eeable PGA memory to retain
_smm_isort_cap                3 0               0               TRUE                        maximum work area for insertio  157700598
                                                                                            n sort(v1)

PGA调整建议

SQL> set pagesize 200;
SQL> select PGA_TARGET_FOR_ESTIMATE/1024/1024 PGAMB,PGA_TARGET_FACTOR,ESTD_PGA_CACHE_HIT_PERCENTAGE,ESTD_OVERALLOC_COUNT FROM V$PGA_TARGET_ADVICE;
     PGAMB PGA_TARGET_FACTOR ESTD_PGA_CACHE_HIT_PERCENTAGE ESTD_OVERALLOC_COUNT
---------- ----------------- ----------------------------- --------------------
       320              .125                           100                    0
       640               .25                           100                    0
      1280                .5                           100                    0
      1920               .75                           100                    0
      2560                 1                           100                    0
      3072               1.2                           100                    0
      3584               1.4                           100                    0
      4096               1.6                           100                    0
      4608               1.8                           100                    0
      5120                 2                           100                    0
      7680                 3                           100                    0
     10240                 4                           100                    0
     15360                 6                           100                    0
     20480                 8                           100                    0

v$pga_target_advice_histogram视图可以通过对不同工作区大小的采样评估提供统计信息供分析使用。

SQL> desc v$pga_target_advice_histogram;
PGA_TARGET_FOR_ESTIMATEPGA_TARGET_FACTOR                                    
ADVICE_STATUS                                                           
LOW_OPTIMAL_SIZE    Histogram评估区间Optimal下限(bytes)  
HIGH_OPTIMAL_SIZE    Histogram评估区间Optimal上限(bytes)
ESTD_OPTIMAL_EXECUTIONS    Histogram评估区间Optimal执行次数
ESTD_ONEPASS_EXECUTIONS  Histogram评估区间Onepassl执行次数
ESTD_MULTIPASSES_EXECUTIONS   Histogram评估区间Multipassl执行次数ESTD_TOTAL_EXECUTIONS   Histogram评估区间执行的总次数IGNORED_WORKAREAS_COUNT  被忽略的工作区总次数

select pga_target_factor factor,
       low_optimal_size / 1024 low,
       round(high_optimal_size / 1024) high,
       estd_optimal_executions estd_opt,
       estd_onepass_executions estd_op,
       estd_multipasses_executions estd_mp,
       estd_total_executions estd_exec
  from v$pga_target_advice_histogram
 where pga_target_factor = 0.25
   and estd_total_executions > 0;

 

你可能感兴趣的:(PGA管理)