[20190507]sga_target=0注意修改_kghdsidx_count设置.txt
--//昨天遇到一例视图定义太复杂导致长时间分析sql语句出现library cache lock等待事件的情况.--//加上大量使用非绑定变量语句,导致硬解析增加,导致问题更加严重.--//顺便解析当时同事发现仅仅1个CPU特别忙.实际上因为仅仅1个shared pool latch在工作.1.环境:> @ ver1PORT_STRING VERSION BANNER------------------ -------------- ----------------------------------------------------------------IBMPC/WIN_NT-8.1.0 10.2.0.3.0 Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod--//注32位版本.2.解析:SELECT addr ,latch# ,child# ,level# ,name ,gets ,sleeps ,immediate_gets ,immediate_misses ,spin_gets FROM V$LATCH_CHILDREN WHERE name LIKE 'shared pool'ORDER BY addr;ADDR LATCH# CHILD# LEVEL# NAME GETS SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES SPIN_GETS-------- ---------- ---------- ---------- ----------- ---------- ---------- -------------- ---------------- ----------05AA3858 213 1 7 shared pool 25235287 182 0 0 738405AA38C0 213 2 7 shared pool 151 0 0 0 005AA3928 213 3 7 shared pool 151 0 0 0 005AA3990 213 4 7 shared pool 151 0 0 0 005AA39F8 213 5 7 shared pool 151 0 0 0 005AA3A60 213 6 7 shared pool 151 0 0 0 005AA3AC8 213 7 7 shared pool 151 0 0 0 07 rows selected.--//仅仅1个shared pool latch在使用.> show parameter sga_NAME TYPE VALUE------------- ----------- ------sga_max_size big integer 1200Msga_target big integer 1200M> @ hide _kghdsidx_countNAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE--------------- ------------------ ---------------------- ---------------------- ----------------------_kghdsidx_count max kghdsidx count TRUE 1 1> @ hide _enable_shared_pool_durationsNAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE----------------------------- -------------------------------------- ------------- ------------- ------------_enable_shared_pool_durations temporary to disable/enable kgh policy TRUE TRUE TRUE> show parameter cpu_countNAME TYPE VALUE--------- ------- ------cpu_count integer 16--//注:在任务管理器看到32个CPU.不过我发现图中下面一排16个cpu好像没有负载.--//主要问题是参数_kghdsidx_count=1.仅仅1个subpool.看来这个版本按照每个子缓冲池至少为512MB设计的.--//参数_kghdsidx_count大小由CPU数量以及共享池大小决定的.最大不过7个.--//我仅仅知道的原则:--//共享子缓冲池的分配的算法很简单:--//·每个子缓冲池必须满足一定的内存约束;--//·每4颗CPU可以分配一个子缓冲池,最多7个。--//本来oracle这样做为了减少shared pool,但是带来另外的问题,如果每个子池太小,反而出现ora-04031错误.于是oracle限制每个子池--//的大小,你可以发现一些blog提到减少参数_kghdsidx_count大小,限制每个子池避免出现子池太小的情况.--//在Oracle 9i中,每个SubPool至少128MB,在Oracle 10g中,每个子缓冲池至少为256MB,在Oracle 11g中,每个子缓冲池至少为512MB.--//Oracle 10g会将单个缓冲池分割再细分4个子分区进行管理(这可能是因为通常4颗CPU才分配一个SubPool),--//分别是"instance", "session", "cursor", and "execution".--//对方安装32位系统,不能设置很大sga,受限共享内存大小600M上下,这样仅仅1个shared pool latch.--//这样在大量硬解析的情况下,特别在分析sql语句很长时间的情况下仅仅1个shared pool latch自然很忙.--//也就是同事看到的情况,仅仅1个CPU在忙...而且sql语句中的视图关联的表太多,导致1条sql语句消耗共享池很大,我执行前面的语句,查看--//v$sqlarea的SHARABLE_MEM达到512K.这样大量非绑定变量语句导致许多对象退出又再进入共享池.--//这就好比一个宾馆的前台仅仅1名接待人员一样,客户入住登记少没有问题,一旦大量客户登记入住,1个人自然忙不过来,--//而其他人根本插不上手.有时候非常像现实工作的场景,1个忙的要死,别人根本插不上手,只能在那干等^_^.3.突然想起我以前1个测试:--//[20190104]sga_target 的设置和ORA-04031错误.txt => http://blog.itpub.net/267265/viewspace-2305567/--//发现一个问题,就是设置sga_target=0,如果大量语句不使用绑定变量可能存在问题.参数_kghdsidx_count=1,仅仅1个shared pool latch.--//这样情况应该适当增加_kghdsidx_count,通过测试说明问题:$ export ORACLE_SID=xxxx$ cat initxxxx.oradb_name=xxxxinstance_name=xxxxsga_target=0sga_max_size=20G--//设置sga_target=0.SYS@xxxx> startup nomountORACLE instance started.Total System Global Area 2.1379E+10 bytesFixed Size 2262656 bytesVariable Size 2.1206E+10 bytesDatabase Buffers 134217728 bytesRedo Buffers 36073472 bytesSELECT addr ,latch# ,child# ,level# ,name ,gets ,sleeps ,immediate_gets ,immediate_misses ,spin_gets FROM V$LATCH_CHILDREN WHERE name LIKE 'shared pool'ORDER BY addr;ADDR LATCH# CHILD# LEVEL# NAME GETS SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES SPIN_GETS---------------- ---------- ---------- ---------- ----------- ---- ---------- -------------- ---------------- ----------000000006010F288 336 1 7 shared pool 1397 0 0 0 5000000006010F328 336 2 7 shared pool 0 0 0 0 0000000006010F3C8 336 3 7 shared pool 0 0 0 0 0000000006010F468 336 4 7 shared pool 0 0 0 0 0000000006010F508 336 5 7 shared pool 0 0 0 0 0000000006010F5A8 336 6 7 shared pool 0 0 0 0 0000000006010F648 336 7 7 shared pool 0 0 0 0 07 rows selected.SYS@xxxx> @ hide _kghdsidx_countold 10: and lower(a.ksppinm) like lower('%&1%')new 10: and lower(a.ksppinm) like lower('%_kghdsidx_count%')NAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE--------------- ------------------ ------------- ------------- ------------_kghdsidx_count max kghdsidx count TRUE 1 1SYS@xxxx> @ hide _enable_shared_pool_durationsold 10: and lower(a.ksppinm) like lower('%&1%')new 10: and lower(a.ksppinm) like lower('%_enable_shared_pool_durations%')NAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE----------------------------- -------------------------------------- ------------- ------------- ------------_enable_shared_pool_durations temporary to disable/enable kgh policy TRUE FALSE FALSE--//注意_enable_shared_pool_durations参数,在sga_target=0的情况下,_enable_shared_pool_durations的缺省值等于FALSE(实际上设--//置为true也无效,看后面测试)--//也就是仅仅1个subpool,下面仅仅一个子子池.SYS@xxxx> @ sgastatx.sql 'free memory'-- All allocations:SUBPOOL BYTES MB------------------------------ ---------- ----------shared pool (0 - Unused): 67108864 64shared pool (1): 268435456 256shared pool (Total): 335544320 320-- Allocations matching "free memory":old 15: AND LOWER(ksmssnam) LIKE LOWER('%&1%')new 15: AND LOWER(ksmssnam) LIKE LOWER('%free memory%')SUBPOOL NAME SUM(BYTES) MB------------------------------ -------------------- ---------- ----------shared pool (0 - Unused): free memory 67108864 64shared pool (1): free memory 30813880 29.39--//仅仅1个子池.共享内存不足的情况下会从shared pool (0 - Unused)分过来.--//另外注意1个问题,有一些文章提示查询x$kghlu可以查询这些子缓冲池的分配,我的测试不行,当然我现在sga_target=0的情况下不存--//在子子池的情况.--//通过一个内部表X$KGHLU([K]ernel [G]eneric memory [H]eap manager State of [L]R[U] Of Unpinned Recreatable chunks)可以--//查询这些子缓冲池的分配: (我的测试不行!!)SYS@xxxx> select * from x$kghlu;ADDR INDX INST_ID KGHLUIDX KGHLUDUR KGHLUSHRPOOL KGHLUFSH KGHLUOPS KGHLURCR KGHLUTRN KGHLUMXA KGHLUMES KGHLUMER KGHLURCN KGHLURMI KGHLURMZ KGHLURMX KGHLUNFU KGHLUNFS---------------- ---------- ---------- ---------- ---------- ------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------00007F6D32E5EFF0 0 1 1 0 1 0 64 3 43 2147483647 0 0 0 0 0 0 0 0--//只能通过heapdump转储获取这方面信息.SYS@xxxx> alter session set events 'immediate trace name heapdump level 2';Session altered.$ grep 'sga heap' /u01/app/oracle/diag/rdbms/xxxx/xxxx/trace/xxxx_ora_43866.trcHEAP DUMP heap name="sga heap" desc=0x60001190HEAP DUMP heap name="sga heap(1,0)" desc=0x6005c318--//可以看出问题.仅仅1个子池,不细分子子池.也就是在sga_target=0的情况下,可能需要手工设置_kghdsidx_count的大小.--//不然可能出现shared pool latch的争用,特别在应用没有绑定变量的情况下.--//我个人建议sga_target=0的情况下手工设置shared_pool_size,db_cache_size.--//或者sga_target<>0的情况下,也设置shared_pool_size,db_cache_size作为最小值,避免内存在这些组件中变换.--//修改*._kghdsidx_count=3$ cat initxxxx.oradb_name=xxxxinstance_name=xxxx#sga_target=20Gsga_target=0sga_max_size=20G#pre_page_sga=true*._kghdsidx_count=3SYS@xxxx> startup nomountORA-04031: unable to allocate 320032 bytes of shared memory ("shared pool","unknown object","KGSK scheduler","KGSK chg class latches")--//shared_pool_size太小.仅仅320M.--//测试增加*._kghdsidx_count=3,*.shared_pool_size=1600M,*._enable_shared_pool_durations=true的情况:$ cat initxxxx.oradb_name=xxxxinstance_name=xxxxsga_target=0G#sga_target=20Gsga_max_size=20G#pre_page_sga=true*._kghdsidx_count=3*.shared_pool_size=1600M*._enable_shared_pool_durations=true~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SYS@xxxx> startup nomountORACLE instance started.Total System Global Area 2.1379E+10 bytesFixed Size 2262656 bytesVariable Size 2.1206E+10 bytesDatabase Buffers 134217728 bytesRedo Buffers 36073472 bySYS@xxxx> @ hide _kghdsidx_countold 10: and lower(a.ksppinm) like lower('%&1%')new 10: and lower(a.ksppinm) like lower('%_kghdsidx_count%')NAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE--------------- ------------------ ------------- ------------- ------------_kghdsidx_count max kghdsidx count FALSE 3 3SYS@xxxx> @ hide _enable_shared_pool_durationsold 10: and lower(a.ksppinm) like lower('%&1%')new 10: and lower(a.ksppinm) like lower('%_enable_shared_pool_durations%')NAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE---------------------------------------- -------------------------------------- ------------- ------------- ------------_enable_shared_pool_durations temporary to disable/enable kgh policy FALSE TRUE TRUESYS@xxxx> select * from x$kghlu;ADDR INDX INST_ID KGHLUIDX KGHLUDUR KGHLUSHRPOOL KGHLUFSH KGHLUOPS KGHLURCR KGHLUTRN KGHLUMXA KGHLUMES KGHLUMER KGHLURCN KGHLURMI KGHLURMZ KGHLURMX KGHLUNFU KGHLUNFS---------------- ---------- ---------- ---------- ---------- ------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------00007FC432A52F70 0 1 3 0 1 0 20 0 14 2147483647 0 0 0 0 0 0 0 000007FC432A54640 1 1 2 0 1 0 9 0 7 2147483647 0 0 0 0 0 0 0 000007FC432A53FF0 2 1 1 0 1 0 14 0 10 2147483647 0 0 0 0 0 0 0 0--//仅仅3个子池.看不出子子池.SYS@xxxx> alter session set events 'immediate trace name heapdump level 2';Session altered.$ grep 'sga heap' /u01/app/oracle/diag/rdbms/xxxx/xxxx/trace/xxxx_ora_43949.trcHEAP DUMP heap name="sga heap" desc=0x60001190HEAP DUMP heap name="sga heap(1,0)" desc=0x6005c318HEAP DUMP heap name="sga heap(2,0)" desc=0x60065be0HEAP DUMP heap name="sga heap(3,0)" desc=0x6006f4a8--//可以发现设置即使_enable_shared_pool_durations=TRUE,在sga_target=0G的情况下也不会出现子子池的情况.SYS@xxxx> @ sgastatx.sql 'free memory'-- All allocations:SUBPOOL BYTES MB------------------------------ ---------- ----------shared pool (0 - Unused): 1275068416 1216shared pool (1): 134217728 128shared pool (2): 134217728 128shared pool (3): 134217728 128shared pool (Total): 1677721600 1600-- Allocations matching "free memory":old 15: AND LOWER(ksmssnam) LIKE LOWER('%&1%')new 15: AND LOWER(ksmssnam) LIKE LOWER('%free memory%')SUBPOOL NAME SUM(BYTES) MB------------------------------ ---------------------------------------- ---------- ----------shared pool (0 - Unused): free memory 1275068416 1216shared pool (1): free memory 48797608 46.54shared pool (2): free memory 34835672 33.22shared pool (3): free memory 48306064 46.07SELECT addr ,latch# ,child# ,level# ,name ,gets ,sleeps ,immediate_gets ,immediate_misses ,spin_gets FROM V$LATCH_CHILDREN WHERE name LIKE 'shared pool'ORDER BY addr;ADDR LATCH# CHILD# LEVEL# NAME GETS SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES SPIN_GETS---------------- ---------- ---------- ---------- ----------- ---- ---------- -------------- ---------------- ----------000000006010F288 336 1 7 shared pool 732 0 1 0 0000000006010F328 336 2 7 shared pool 673 0 1 0 0000000006010F3C8 336 3 7 shared pool 903 0 1 0 1000000006010F468 336 4 7 shared pool 1 0 0 0 0000000006010F508 336 5 7 shared pool 1 0 0 0 0000000006010F5A8 336 6 7 shared pool 1 0 0 0 0000000006010F648 336 7 7 shared pool 1 0 0 0 07 rows selected.--//总之,如果手工管理内存,设置sga_target=0G的情况下,适当设置_kghdsidx_count,shared_pool_size,db_cache_size值.4.最后测试sga_target<>0的情况:$ grep -v "^#" initxxxx.oradb_name=xxxxinstance_name=xxxxsga_target=20Gsga_max_size=20GSYS@xxxx> @ hide _kghdsidx_countold 10: and lower(a.ksppinm) like lower('%&1%')new 10: and lower(a.ksppinm) like lower('%_kghdsidx_count%')NAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE---------------------------------------- --------------------------------------- ------------- ------------- ------------_kghdsidx_count max kghdsidx count TRUE 6 6SYS@xxxx> @ hide _enable_shared_pool_durationsold 10: and lower(a.ksppinm) like lower('%&1%')new 10: and lower(a.ksppinm) like lower('%_enable_shared_pool_durations%')NAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE---------------------------------------- --------------------------------------- ------------- ------------- ------------_enable_shared_pool_durations temporary to disable/enable kgh policy TRUE TRUE TRUESYS@xxxx> @ sgastatx.sql 'free memory'-- All allocations:SUBPOOL BYTES MB------------------------------ ---------- ----------shared pool (0 - Unused): 469762048 448shared pool (1): 268435456 256shared pool (2): 335544320 320shared pool (3): 335544320 320shared pool (4): 335544320 320shared pool (5): 268435456 256shared pool (6): 335544320 320shared pool (Total): 2348810240 22408 rows selected.-- Allocations matching "free memory":old 15: AND LOWER(ksmssnam) LIKE LOWER('%&1%')new 15: AND LOWER(ksmssnam) LIKE LOWER('%free memory%')SUBPOOL NAME SUM(BYTES) MB------------------------------ -------------------- ---------- ----------shared pool (0 - Unused): free memory 469762048 448shared pool (1): free memory 207961376 198.33shared pool (2): free memory 224686568 214.28shared pool (3): free memory 270474992 257.95shared pool (4): free memory 234357448 223.5shared pool (5): free memory 206367112 196.81shared pool (6): free memory 239940912 228.837 rows selected.SYS@xxxx> select * from x$kghlu; SUB SSUB FLUSHED LRU LIST RECURRENT TRANSIENT RESERVED RESERVED RESERVED RESERVED FREE UNPIN LAST FRUNPADDR INDX INST_ID POOL POOL KGHLUSHRPOOL CHUNKS OPERATIONS CHUNKS CHUNKS KGHLUMXA KGHLUMES KGHLUMER SCANS MISSES MISS SIZE MISS MAX SZ UNSUCCESS UNSUCC SIZE---------------- ---------- ---------- ---------- ---------- ------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------- ---------- -----------00007F78519F2E48 0 1 6 0 1 0 17 0 17 2147483647 0 0 0 0 0 0 0 000007F78519F27F8 1 1 5 0 1 0 27 0 17 2147483647 0 0 0 0 0 0 0 000007F78519F3E60 2 1 4 0 1 0 23 0 19 2147483647 0 0 0 0 0 0 0 000007F78519F3810 3 1 3 0 1 0 40 0 16 2147483647 0 0 0 0 0 0 0 000007F78519F4EE0 4 1 2 0 1 0 34 0 22 2147483647 0 0 0 0 0 0 0 000007F78519F4890 5 1 1 0 1 0 26 0 16 2147483647 0 0 0 0 0 0 0 06 rows selected.--// 看不出子子池的分配情况.SYS@xxxx> alter session set events 'immediate trace name heapdump level 2';Session altered.$ egrep "sga heap|Total heap size" /u01/app/oracle/diag/rdbms/xxxx/xxxx/trace/xxxx_ora_44083.trcHEAP DUMP heap name="sga heap" desc=0x60001190HEAP DUMP heap name="sga heap(1,0)" desc=0x6005c318Total heap size = 67108776HEAP DUMP heap name="sga heap(1,1)" desc=0x6005db70Total heap size = 67108776HEAP DUMP heap name="sga heap(1,2)" desc=0x6005f3c8Total heap size = 67108776HEAP DUMP heap name="sga heap(1,3)" desc=0x60060c20Total heap size = 67108776--// 67108776*4/1024/1024 = 255.99966430664062500000 ,接近256M.HEAP DUMP heap name="sga heap(2,0)" desc=0x60065be0Total heap size =134217552HEAP DUMP heap name="sga heap(2,1)" desc=0x60067438Total heap size = 67108776HEAP DUMP heap name="sga heap(2,2)" desc=0x60068c90Total heap size = 67108776HEAP DUMP heap name="sga heap(2,3)" desc=0x6006a4e8Total heap size = 67108776--//(134217552+67108776+67108776+67108776)/1024/1024 = 319.99958038330078125000,接近320M.HEAP DUMP heap name="sga heap(3,0)" desc=0x6006f4a8Total heap size =134217552HEAP DUMP heap name="sga heap(3,1)" desc=0x60070d00Total heap size = 67108776HEAP DUMP heap name="sga heap(3,2)" desc=0x60072558Total heap size = 67108776HEAP DUMP heap name="sga heap(3,3)" desc=0x60073db0Total heap size = 67108776HEAP DUMP heap name="sga heap(4,0)" desc=0x60078d70Total heap size =134217552HEAP DUMP heap name="sga heap(4,1)" desc=0x6007a5c8Total heap size = 67108776HEAP DUMP heap name="sga heap(4,2)" desc=0x6007be20Total heap size = 67108776HEAP DUMP heap name="sga heap(4,3)" desc=0x6007d678Total heap size = 67108776HEAP DUMP heap name="sga heap(5,0)" desc=0x60082638Total heap size = 67108776HEAP DUMP heap name="sga heap(5,1)" desc=0x60083e90Total heap size = 67108776HEAP DUMP heap name="sga heap(5,2)" desc=0x600856e8Total heap size = 67108776HEAP DUMP heap name="sga heap(5,3)" desc=0x60086f40Total heap size = 67108776HEAP DUMP heap name="sga heap(6,0)" desc=0x6008bf00Total heap size =134217552HEAP DUMP heap name="sga heap(6,1)" desc=0x6008d758Total heap size = 67108776HEAP DUMP heap name="sga heap(6,2)" desc=0x6008efb0Total heap size = 67108776HEAP DUMP heap name="sga heap(6,3)" desc=0x60090808Total heap size = 67108776--//这样可以看到每个子池有几个子子池,并且每个的大小.总结:--//总之注意,如果手工管理内存,设置sga_target=0G的情况下,适当设置_kghdsidx_count,shared_pool_size,db_cache_size值.避免--//shared pool latch仅仅1个的情况.--//我个性喜欢手工管理内存设置sga_target=sga_max_size,设置shared_pool_size,db_cache_size基本不会转换.--//附上sgastatx.sql脚本.$ cat sgastatx.sql------------------------------------------------------------------------------------ File name: sgastatx-- Purpose: Show shared pool stats by sub-pool from X$KSMSS---- Author: Tanel Poder-- Copyright: (c) http://www.tanelpoder.com---- Usage: @sgastatx <statistic name>-- @sgastatx "free memory"-- @sgastatx cursor---- Other: The other script for querying V$SGASTAT is called sgastat.sql--------------------------------------------------------------------------------------COL sgastatx_subpool HEAD SUBPOOL FOR a30PROMPTPROMPT -- All allocations:SELECT 'shared pool ('||NVL(DECODE(TO_CHAR(ksmdsidx),'0','0 - Unused',ksmdsidx), 'Total')||'):' sgastatx_subpool , SUM(ksmsslen) bytes , ROUND(SUM(ksmsslen)/1048576,2) MBFROM x$ksmssWHERE ksmsslen > 0--AND ksmdsidx > 0GROUP BY ROLLUP ( ksmdsidx )ORDER BY sgastatx_subpool ASC/BREAK ON sgastatx_subpool SKIP 1PROMPT -- Allocations matching "&1":SELECT subpool sgastatx_subpool , name , SUM(bytes) , ROUND(SUM(bytes)/1048576,2) MBFROM ( SELECT 'shared pool ('||DECODE(TO_CHAR(ksmdsidx),'0','0 - Unused',ksmdsidx)||'):' subpool , ksmssnam name , ksmsslen bytes FROM x$ksmss WHERE ksmsslen > 0 AND LOWER(ksmssnam) LIKE LOWER('%&1%'))GROUP BY subpool , nameORDER BY subpool ASC , SUM(bytes) DESC/BREAK ON sgastatx_subpool DUP