[20181108]12c sqlplus rowfetch参数4.txt

[20181108]12c sqlplus rowfetch参数4.txt

–//12cr2 可以改变缺省rowfetch参数.11g之前缺省是1.通过一些测试说明问题.
–//前几天做的测试有点乱,链接http://blog.itpub.net/267265/viewspace-2219004/.
–//重新梳理看看.

1.环境:
scott@78> @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
–//数据库11.2.0.4.但是我使用12c sqlplus做为客户端,rowprefetch参数可以设置,测试一样有效.

scott@78>create table t as select rownum id1,1 id2 from dual connect by level<=23;
table created.

scott@78> show rowprefetch
rowprefetch 1
–//可以发现缺省就是1.11g之前无法设置.
–//另外rowprefetch不能设置为0.
scott@78> set rowprefetch 0
sp2-0267: rowprefetch option 0 out of range (1 through 32767)

grant execute on  dbms_lock to scott;

create or replace function scott.sleep (seconds in number)
   return number
as
begin
   sys.dbms_lock.sleep (seconds);
   return seconds;
end;
/

create or replace function get_dept (p_deptno dept.deptno%type)
   return dept.dname%type
   deterministic
is
   l_dname   dept.dname%type;
begin
   dbms_lock.sleep (1);

   select dname
     into l_dname
     from dept
    where deptno = p_deptno;

   return l_dname;
end;
/

2.建立测试脚本:
r:> cat aa.txt
set timing on
set arraysize &1
set rowprefetch &2
alter session set events ‘10046 trace name context forever, level 12’;
select rownum  ,t.*,sleep(id2) n10,&&1 arraysize ,&&2 rowprefetch from t;
–select rownum  ,emp.*,get_dept(deptno) c10,&&1 arraysize ,&&2 rowprefetch from emp;
alter session set events ‘10046 trace name context off’;
set timing off

–//执行脚本时,第1个参数表示arraysize,第2个参数表示rowprefetch.

3.测试:
scott@78> @ aa.txt 5 4
    rownum        id1        id2  n10  arraysize rowprefetch
———- ———- ———- —- ———- ———–
         1          1          1    1          5           4
         2          2          1    1          5           4
         3          3          1    1          5           4
         4          4          1    1          5           4
         5          5          1    1          5           4
         6          6          1    1          5           4
         7          7          1    1          5           4
         8          8          1    1          5           4
         9          9          1    1          5           4
        10         10          1    1          5           4
        11         11          1    1          5           4
        12         12          1    1          5           4
        13         13          1    1          5           4
        14         14          1    1          5           4
        15         15          1    1          5           4
        16         16          1    1          5           4
        17         17          1    1          5           4
        18         18          1    1          5           4
        19         19          1    1          5           4
        20         20          1    1          5           4
        21         21          1    1          5           4
        22         22          1    1          5           4
        23         23          1    1          5           4
23 rows selected.

fetch #140079390128648:c=1000,e=4004469,p=0,cr=3,cu=0,mis=0,r=4,dep=0,og=1,plh=2402761124,tim=1541647376650608
fetch #140079390128648:c=1000,e=5004986,p=0,cr=1,cu=0,mis=0,r=5,dep=0,og=1,plh=2402761124,tim=1541647381656601
fetch #140079390128648:c=999,e=5005185,p=0,cr=1,cu=0,mis=0,r=5,dep=0,og=1,plh=2402761124,tim=1541647386662564
fetch #140079390128648:c=1000,e=5005145,p=0,cr=1,cu=0,mis=0,r=5,dep=0,og=1,plh=2402761124,tim=1541647391668598
fetch #140079390128648:c=1000,e=4004133,p=0,cr=1,cu=0,mis=0,r=4,dep=0,og=1,plh=2402761124,tim=1541647395673629

–//fetch 4,5,5,5,4. 而显示行数5,5,5,8.
–//你可以发现在输出前等待至少9秒(我使用手机秒表测试).也就是在输出前已经fetch 9条记录.
–//可以理解为 先fetch=4(rowprefetch=4),不足arraysize=5,再fetch=5.必须等fetch完成了(这时已经过了9秒),才会显示输出5条,
–//剩余4条在输出缓存(不足),再fetch=5,显示输出5条.剩余4条在输出缓存.再fetch=5,显示输出5条,剩余4条在输出缓存,这时再
–//fetch=4仅仅剩下4条记录,这时已经fetch完成,缓存的8条一起输出,不再按照5条输出.

scott@78> @ aa.txt 5 9

–//结果不再贴出略.

fetch #140079394727376:c=2000,e=9009422,p=0,cr=3,cu=0,mis=0,r=9,dep=0,og=1,plh=2402761124,tim=1541649678068572
fetch #140079394727376:c=1000,e=10009690,p=0,cr=1,cu=0,mis=0,r=10,dep=0,og=1,plh=2402761124,tim=1541649688079548
fetch #140079394727376:c=0,e=4003868,p=0,cr=1,cu=0,mis=0,r=4,dep=0,og=1,plh=2402761124,tim=1541649692084551

–//fetch 9,10,4. 而显示行数5,10,8.
–//你可以发现第2次fetch不再等于arraysize,而是2*arraysize.

scott@78> @ aa.txt 6 11
..
–//结果不再贴出略.

fetch #140079389953128:c=2000,e=11010343,p=0,cr=3,cu=0,mis=0,r=11,dep=0,og=1,plh=2402761124,tim=1541660682428524
fetch #140079389953128:c=2000,e=12011616,p=0,cr=1,cu=0,mis=0,r=12,dep=0,og=1,plh=2402761124,tim=1541660694441553
fetch #140079389953128:c=0,e=14,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=2402761124,tim=1541660694442817

–//fetch 11,12,1. 而显示行数6,17
–//你可以发现第2次fetch不再等于arraysize,而是2*arraysize.
–//注:最后1次fetch r=0,很快,所以看到第2次显示17,实际上显示行数还是6,12,5.

scott@78> @ aa.txt 3 7
..
–//结果不再贴出略.
fetch #140079389953128:c=2000,e=7006724,p=0,cr=3,cu=0,mis=0,r=7,dep=0,og=1,plh=2402761124,tim=1541660899414555
fetch #140079389953128:c=2000,e=9008694,p=0,cr=1,cu=0,mis=0,r=9,dep=0,og=1,plh=2402761124,tim=1541660908424586
fetch #140079389953128:c=999,e=7006930,p=0,cr=1,cu=0,mis=0,r=7,dep=0,og=1,plh=2402761124,tim=1541660915432531

–//fetch 7,9,7. 而显示行数6,9,8
–//你可以发现第2次fetch不再等于arraysize,而是3*arraysize.

scott@78> @ aa.txt 2 7

–//结果不再贴出略.

fetch #140079390530944:c=1000,e=7007159,p=0,cr=3,cu=0,mis=0,r=7,dep=0,og=1,plh=2402761124,tim=1541661275532542
fetch #140079390530944:c=1000,e=8007723,p=0,cr=1,cu=0,mis=0,r=8,dep=0,og=1,plh=2402761124,tim=1541661283541636
fetch #140079390530944:c=2000,e=8007970,p=0,cr=1,cu=0,mis=0,r=8,dep=0,og=1,plh=2402761124,tim=1541661291550622
fetch #140079390530944:c=0,e=19,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=2402761124,tim=1541661291551655

–//fetch 7,8,8,0. 而显示行数6,8,9.
–//你可以发现第2次fetch不再等于arraysize,而是4*arraysize.
–//再次出现最后一次fetch r=0的情况,实际上显示行数是6,8,8,1.

–//这样可以得到一个规律:

–//你可以发现第2次fetch不再等于arraysize,而是n*arraysize.第2次fetch参数应该是 ceil(rowprefetch/arraysize)*arraysize.

3.小结:
–//可以看出一些规律:
–//1.fetch 第1次数量与参数rowprefetch相关,当然必须小于返回记录的数量.
–//2.fetch 第x次数量(x>=2)与参数arraysize的倍数n有关. n=ceil(rowprefetch/arraysize)
–//3.fetch 最后一次应该等于剩余记录.不会大于ceil(rowprefetch/arraysize)*arraysize.
–//4.fetch 最后一次有可能是0.

–//5.显示行数 第1次 floor(rowprefetch/arraysize)*arraysize.
–//6.显示行数 第x次(x>=2) ceil(rowprefetch/arraysize)*arraysize.
–//7.显示行数 最后一次等于剩余在缓存的行数.

–//这样链接中http://blog.itpub.net/267265/viewspace-2219004/.看到的情况就很好解析了.

–//实际上这其中细节不重要,你可以理解改变12c下改变sqlplus的rowprefetch参数,有可能隐含改变了fetch方式.
–//第1次fetch = rowpefetch, 第2次 fetch 等于 ceil(rowprefetch/arraysize)*arraysize就足够了.

–//最后再分析前面测试我遇到的一个问题.实际上某种巧合.最后一次fetch r=0导致的情况.
set arraysize 5
set rowprefetch 4
@ 10046on 12
select rownum  ,emp.*,get_dept(deptno) c10,5 arraysize ,4 rowprefetch from emp;
@ 10046off

fetch #140079390567504:c=1000,e=2002160,p=0,cr=10,cu=0,mis=0,r=4,dep=0,og=1,plh=1498225739,tim=1541661929029851
fetch #140079390567504:c=1000,e=3002851,p=0,cr=7,cu=0,mis=0,r=5,dep=0,og=1,plh=1498225739,tim=1541661932033872
fetch #140079390567504:c=2000,e=3002868,p=0,cr=7,cu=0,mis=0,r=5,dep=0,og=1,plh=1498225739,tim=1541661935037842
fetch #140079390567504:c=0,e=15,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=1498225739,tim=1541661935038827

–//fetch 4,5,5,0 而显示行数5,9.我当时的疑问是为什么显示行数不是5,5,4.
–//为什么最后9行全部一次显示出来.实际上问题在第4次fetch=0,第4次fetch就很快,这样看到的显示行数就是5,9.
–//实际上就是5,5,4.

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐