欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[20190329]探究sql语句相关mutexes补充2.txt

程序员文章站 2023-10-17 11:38:36
[20190329]探究sql语句相关mutexes补充2.txt--//昨天测试sql语句相关mutexes,看看如果出现多个子光标的情况.1.环境:SCOTT@book> @ ver1PORT_STRING VERSION BANNER x86_64/Linux 2.4.xx 11.2.0.4. ......

[20190329]探究sql语句相关mutexes补充2.txt

--//昨天测试sql语句相关mutexes,看看如果出现多个子光标的情况.

1.环境:
scott@book> @ ver1
port_string                    version        banner
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/linux 2.4.xx            11.2.0.4.0     oracle database 11g enterprise edition release 11.2.0.4.0 - 64bit production

$ cat m2.txt
set verify off
column a noprint new_value v_a;
select mod ( &&3 ,3) a  from dual ;
alter session set optimizer_index_caching= &v_a;
host sleep $(echo &&3/50| bc -l )
insert into job_times values ( sys_context ('userenv', 'sid') ,dbms_utility.get_time ,'&&2') ;
commit ;
declare
v_id number;
v_d date;
begin
    for i in 1 .. &&1 loop
        --select  1 into v_id from dual ;
        --select  sysdate into v_d from dual ;
                select deptno into v_id from dept where deptno=10;
    end loop;
end ;
/
update job_times set time_ela = dbms_utility.get_time - time_ela where sid=sys_context ('userenv', 'sid') and method='&&2';
commit;
quit

--//这样建立3个子光标.

2.测试:
$ seq 150 | xargs -i {}  -p 150 bash -c  "sqlplus -s -l scott/book @m2.txt 1e6 f2_150 {} >/dev/null"
$ seq 150 | xargs -i {}  -p 150 bash -c  "sqlplus -s -l scott/book @m2.txt 1e6 g2_150 {} >/dev/null"

sys@book> @ mutexy 6
      hash sum_sleeps   sum_gets location                       mutex_type           mutex_addr       sqlid         kglnaown c100
---------- ---------- ---------- ------------------------------ -------------------- ---------------- ------------- -------- ---------------------------------------
1692266099   14495552 3.6572e+11 kkslockdelete [kkschlpin6]     cursor pin           000000007bd3ec98 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099    7893620 2.0592e+11 kksfbc [kkschlfsp2]            cursor pin           000000007bd3ec98 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099    4307308 8.8088e+10 kkslockdelete [kkschlpin6]     cursor pin           000000007bd3f260 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099    1997852 4.6240e+10 kksfbc [kkschlfsp2]            cursor pin           000000007bd3f260 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099    1345020 1.7428e+10 kksfbc [kkschlfsp2]            cursor pin           000000007c08d440 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099    1273888 1.9013e+10 kkslockdelete [kkschlpin6]     cursor pin           000000007c08d440 a31kd5tkdvvmm          select deptno from dept where deptno=10
6 rows selected.

sys@book> @ fcha 000000007bd3ec98
loc ksmchptr           ksmchidx   ksmchdur ksmchcom           ksmchsiz ksmchcls   ksmchtyp ksmchpar
--- ---------------- ---------- ---------- ---------------- ---------- -------- ---------- ----------------
sga 000000007bd3e5d0          1          1 kglh0^64ddee73         4096 recr           4095 000000007ca31368

sys@book> @ fcha 000000007bd3f260
loc ksmchptr           ksmchidx   ksmchdur ksmchcom           ksmchsiz ksmchcls   ksmchtyp ksmchpar
--- ---------------- ---------- ---------- ---------------- ---------- -------- ---------- ----------------
sga 000000007bd3e5d0          1          1 kglh0^64ddee73         4096 recr           4095 000000007ca31368

sys@book> @ fcha 000000007c08d440
loc ksmchptr           ksmchidx   ksmchdur ksmchcom           ksmchsiz ksmchcls   ksmchtyp ksmchpar
--- ---------------- ---------- ---------- ---------------- ---------- -------- ---------- ----------------
sga 000000007c08d168          1          1 kglh0^64ddee73         4096 freeabl           0 000000007ca31368

--//3个子光标的ksmchpar=000000007ca31368.不过位置不一样,1个在ksmchcls=recr.另外1个在ksmchcls=freeabl.

3.对比共享池的情况看看.
select a.* from x$ksmsp a where a.ksmchpar=hextoraw('000000007ca31368')
addr                   indx    inst_id   ksmchidx   ksmchdur ksmchcom         ksmchptr           ksmchsiz ksmchcls   ksmchtyp ksmchpar
---------------- ---------- ---------- ---------- ---------- ---------------- ---------------- ---------- -------- ---------- ----------------
00007fdd4b32be98      11124          1          1          1 kglh0^64ddee73   000000007c08d168       4096 freeabl           0 000000007ca31368
00007fdd4b44a178      11921          1          1          1 kglh0^64ddee73   000000007bd3e5d0       4096 recr           4095 000000007ca31368
--//可以发现如果子光标很多,会大量消耗父游标堆0的空间,也就是父游标堆0会变大.
--//参考链接:http://blog.itpub.net/267265/viewspace-2212504/

sys@book> @ sharepool/shp4 a31kd5tkdvvmm 0
text           kglhdadr         kglhdpar         c40                                        kglhdlmd   kglhdpmd   kglhdivc kglobhd0         kglobhd6           kglobhs0   kglobhs6   kglobt16   n0_6_16        n20   kglnahsh kglobt03        kglobt09
-------------- ---------------- ---------------- ---------------------------------------- ---------- ---------- ---------- ---------------- ---------------- ---------- ---------- ---------- --------- ---------- ---------- ------------- ----------
子游标句柄地址 000000007bd26060 000000007cd3e1c0 select deptno from dept where deptno=10           0          0          0 000000007bf848c0 000000007bd3ed40       4528       8088       6080     18696      18696 1692266099 a31kd5tkdvvmm          0
子游标句柄地址 000000007ce19ee0 000000007cd3e1c0 select deptno from dept where deptno=10           0          0          0 000000007c4be340 000000007bd3f308       4528       8088       6080     18696      18696 1692266099 a31kd5tkdvvmm          1
子游标句柄地址 000000007cd3b6d0 000000007cd3e1c0 select deptno from dept where deptno=10           0          0          0 000000007cbf7ac8 000000007c08d4e8       4544       8088       6080     18712      18712 1692266099 a31kd5tkdvvmm          2
父游标句柄地址 000000007cd3e1c0 000000007cd3e1c0 select deptno from dept where deptno=10           0          0          0 000000007ca31368 00                     8792          0          0      8792       8792 1692266099 a31kd5tkdvvmm      65535

--//kglobhd0=000000007ca31368.
--//也就是metux的结构体在父游标的堆0中,为什么放在父游标的堆0中,而不是各个子游标的堆0中.
--//集中管理吗?不知道....

scott@book> select method,count(*),round(avg(time_ela),0),sum(time_ela) from job_times  group by method order by to_number(substr(method,4)),3;
method                 count(*) round(avg(time_ela),0) sum(time_ela)
-------------------- ---------- ---------------------- -------------
g2_150                      150                  19804       2970658
f2_150                      150                  19871       2980723
d2_150                      150                  20575       3086315
--//这样因为参数不同,争用相对减少,反而快一点点.

4.如果脚本修改如下:
$ cat m2.txt
set verify off
column a noprint new_value v_a;
--select mod ( &&3 ,3) a  from dual ;
alter session set optimizer_index_cost_adj= &&3;
host sleep $(echo &&3/50| bc -l )
insert into job_times values ( sys_context ('userenv', 'sid') ,dbms_utility.get_time ,'&&2') ;
commit ;
declare
v_id number;
v_d date;
begin
    for i in 1 .. &&1 loop
        --select  1 into v_id from dual ;
        --select  sysdate into v_d from dual ;
                select deptno into v_id from dept where deptno=10;
    end loop;
end ;
/
update job_times set time_ela = dbms_utility.get_time - time_ela where sid=sys_context ('userenv', 'sid') and method='&&2';
commit;
quit

--//这样产生150个子光标.看看情况如何?重启数据库测试看看.

$ seq 150 | xargs -i {}  -p 150 bash -c  "sqlplus -s -l scott/book @m2.txt 1e6 h2_150 {} >/dev/null"

--//看到等待事件主要有:
cursor: mutex x
cursor: pin s
latch: shared pool
library cache lock
library cache: mutex x
--//主要是library cache lock

sys@book> @ mutexy 20 a31kd5tkdvvmm
old  14:          and kglobt03= decode('&&2','',kglobt03,lower('&&2'))
new  14:          and kglobt03= decode('a31kd5tkdvvmm','',kglobt03,lower('a31kd5tkdvvmm'))
old  22: order by sum_sleeps desc ) where rownum<= &1
new  22: order by sum_sleeps desc ) where rownum<= 20
      hash sum_sleeps   sum_gets location                       mutex_type           mutex_addr       sqlid         kglnaown c100
---------- ---------- ---------- ------------------------------ -------------------- ---------------- ------------- -------- ---------------------------------------
1692266099      86674      76708 kkslce [kkschlpin2]            cursor pin           000000007d93f0c8 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099      44998       1963 kkslce [kkschlpin2]            cursor pin           000000007dcc25d0 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099      21442       3322 kkslce [kkschlpin2]            cursor pin           000000007dd04320 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099      21291      10570 kkslce [kkschlpin2]            cursor pin           000000007d87dca0 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099      10268      21744 kkslce [kkschlpin2]            cursor pin           000000007dd039a0 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099      10268   18062620 kkscsprunechild [kksprtloc35]  cursor parent        000000007d176240 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       6493     837144 kkslce [kkschlpin2]            cursor pin           000000007c506250 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       4228      21744 kkslce [kkschlpin2]            cursor pin           000000007d20c438 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       4077      15704 kkslce [kkschlpin2]            cursor pin           000000007c11ad58 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       4077        453 kkslce [kkschlpin2]            cursor pin           000000007d20ce38 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       3775     259720 kkslce [kkschlpin2]            cursor pin           000000007cb89de0 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       3322     388674 kkslce [kkschlpin2]            cursor pin           000000007ca2f9d0 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       3322        151 kkslce [kkschlpin2]            cursor pin           000000007d1d6a10 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       3322      56625 kkslce [kkschlpin2]            cursor pin           000000007dcc2a90 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       3020      57078 kkslce [kkschlpin2]            cursor pin           000000007e248738 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       3020      29143 kkslce [kkschlpin2]            cursor pin           000000007c11a3d8 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       2567      10268 kkslce [kkschlpin2]            cursor pin           000000007dcc2f50 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       2416       8154 kkslce [kkschlpin2]            cursor pin           000000007d93f588 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       1963      92412 kkslce [kkschlpin2]            cursor pin           000000007ce74e10 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099       1963     850885 kgllkdl1  85                   library cache        000000007d1701d0 a31kd5tkdvvmm          select deptno from dept where deptno=10
20 rows selected.

scott@book> select method,count(*),round(avg(time_ela),0),sum(time_ela) from job_times  group by method order by to_number(substr(method,4)),3;
method                 count(*) round(avg(time_ela),0) sum(time_ela)
-------------------- ---------- ---------------------- -------------
h2_150                      150                  17556       2633437
g2_150                      150                  19804       2970658
f2_150                      150                  19871       2980723
c2_150                      150                  20481       3072117
d2_150                      150                  20575       3086315
e2_150                      150                  20589       3088339
6 rows selected.
--//执行更快一些.

--//另外我看到仅仅是cursor: pin s,还有许多与mutex有关的等待事件.自己还不熟悉.
sys@book> @ ev_name cursor
old   1: select * from v$event_name where lower(name) like lower('%&&1%')
new   1: select * from v$event_name where lower(name) like lower('%cursor%')
    event#   event_id name                                     parameter1           parameter2           parameter3           wait_class_id wait_class# wait_class
---------- ---------- ---------------------------------------- -------------------- -------------------- -------------------- ------------- ----------- --------------------
       278  877525844 cursor: mutex x                          idn                  value                where                   3875070507           4 concurrency
       279 1575214430 cursor: mutex s                          idn                  value                where                   3875070507           4 concurrency
       280 3085815766 cursor: pin x                            idn                  value                where                   3875070507           4 concurrency
       281  352301881 cursor: pin s                            idn                  value                where                   3875070507           4 concurrency
       282 1729366244 cursor: pin s wait on x                  idn                  value                where                   3875070507           4 concurrency

sys@book> @ ev_name mutex
old   1: select * from v$event_name where lower(name) like lower('%&&1%')
new   1: select * from v$event_name where lower(name) like lower('%mutex%')
    event#   event_id name                                     parameter1           parameter2           parameter3           wait_class_id wait_class# wait_class
---------- ---------- ---------------------------------------- -------------------- -------------------- -------------------- ------------- ----------- --------------------
       272   39333034 securefile mutex                                                                                           3875070507           4 concurrency
       278  877525844 cursor: mutex x                          idn                  value                where                   3875070507           4 concurrency
       279 1575214430 cursor: mutex s                          idn                  value                where                   3875070507           4 concurrency
       289 1646780882 library cache: mutex x                   idn                  value                where                   3875070507           4 concurrency
       290 2446268751 library cache: mutex s                   idn                  value                where                   3875070507           4 concurrency

5.附上相关脚本:
$ cat sharepool/shp4.sql
column n0_6_16 format 99999999
select decode (kglhdadr,
               kglhdpar, '父游标句柄地址',
               '子游标句柄地址')
          text,
       kglhdadr,
       kglhdpar,
       substr(kglnaobj,1,40) c40,
           kglhdlmd,
           kglhdpmd,
           kglhdivc,
       kglobhd0,
       kglobhd6,
       kglobhs0,kglobhs6,kglobt16,
       kglobhs0+kglobhs6+kglobt16 n0_6_16,
           kglobhs0+kglobhs1+kglobhs2+kglobhs3+kglobhs4+kglobhs5+kglobhs6+kglobt16 n20,
           kglnahsh,
           kglobt03 ,
           kglobt09
  from x$kglob
 where kglobt03 = '&1'  or kglhdpar='&1' or kglhdadr='&1' or kglnahsh= &2;


$ cat tpt/fcha.sql
--------------------------------------------------------------------------------
--
-- file name:   fcha.sql (find chunk address) v0.2
-- purpose:     find in which heap (uga, pga or shared pool) a memory address resides
--
-- author:      tanel poder
-- copyright:   (c) http://blog.tanelpoder.com | @tanelpoder
--
-- usage:       @fcha <addr_hex>
--              @fcha f6a14448
--
-- other:       this would only report an uga/pga chunk address if it belongs
--              to *your* process/session (x$ksmup and x$ksmpp do not see other
--              session/process memory)
--
--------------------------------------------------------------------------------
prompt find in which heap (uga, pga or shared pool) the memory address &1 resides...
prompt
prompt warning!!! this script will query x$ksmsp, which will cause heavy shared pool latch contention
prompt in systems under load and with large shared pool. this may even completely hang
prompt your instance until the query has finished! you probably do not want to run this in production!
prompt
pause  press enter to continue, ctrl+c to cancel...


select
    'sga' loc,
    ksmchptr,
    ksmchidx,
    ksmchdur,
    ksmchcom,
    ksmchsiz,
    ksmchcls,
    ksmchtyp,
    ksmchpar
from
    x$ksmsp
where
    to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
    between
        to_number(ksmchptr,'xxxxxxxxxxxxxxxx')
    and to_number(ksmchptr,'xxxxxxxxxxxxxxxx') + ksmchsiz - 1
union all
select
    'uga',
    ksmchptr,
    null,
    null,
    ksmchcom,
    ksmchsiz,
    ksmchcls,
    ksmchtyp,
    ksmchpar
from
    x$ksmup
where
    to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
    between
        to_number(ksmchptr,'xxxxxxxxxxxxxxxx')
    and to_number(ksmchptr,'xxxxxxxxxxxxxxxx') + ksmchsiz - 1
union all
select
    'pga',
    ksmchptr,
    null,
    null,
    ksmchcom,
    ksmchsiz,
    ksmchcls,
    ksmchtyp,
    ksmchpar
from
    x$ksmpp
where
    to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
    between
        to_number(ksmchptr,'xxxxxxxxxxxxxxxx')
    and to_number(ksmchptr,'xxxxxxxxxxxxxxxx') + ksmchsiz - 1
/

$ cat mutexy.sql
column kglnaown format a20
column mutex_type format a20
column kglnaobj format a100
column location format a30
  select * from (
  select kglnahsh hash
        ,sum (sleeps) sum_sleeps
        ,sum (gets) sum_gets
        ,location
        ,mutex_type
                ,mutex_addr
        ,kglobt03 sqlid
        ,kglnaown
        ,replace(kglnaobj,chr(13)) c100
    --,substr (kglnaobj, 1, 140) object
    from x$kglob, x$mutex_sleep_history
   where kglnahsh = mutex_identifier
         and kglobt03= decode('&&2','',kglobt03,lower('&&2'))
group by kglnaobj
        ,kglobt03
        ,kglnaown
        ,kglnahsh
        ,location
        ,mutex_type
                ,mutex_addr
order by sum_sleeps desc ) where rownum<= &1;