[20171019]绑定变量的分配长度7.txt
--//如果绑定变量中字符串分配占用空间的长度变化,oracle会建立子光标。
--//参考连接:
http://blog.itpub.net/267265/viewspace-1993495/
--//oracle 可以通过一个10503事件设置大的缓存,测试看看:
$ oerr ora 10503
10503, 00000, "enable user-specified graduated bind lengths"
// *Cause:
// *Action:
--//参考链接: http://blog.itpub.net/267265/viewspace-2024389/
--//按照长度分级 :1-32,33-128,129-2000,2001-4000.
--//我前面的测试ALTER SESSION SET EVENTS '10503 trace name context level <buffer length>, forever';
--//我以前测试<buffer length> = 2000,如果不是对齐边界看看会是什么情况?
1.环境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0
create table t (c1 varchar2(4000));
2.测试:
alter system flush shared_pool;
ALTER SESSION SET EVENTS '10503 trace name context level 1000, forever';
declare
v_c1 varchar2(4000);
begin
for i in 1..4000 loop
v_c1 := rpad('X',i);
execute immediate 'select count(*) from t where c1=:instring' using v_c1 ;
end loop;
end;
/
--确定sql_id='9mrd273576n14'
SCOTT@test01p> select sql_id, child_number, executions from v$sql where sql_id = '9mrd273576n14';
SQL_ID CHILD_NUMBER EXECUTIONS
------------- ------------ ----------
9mrd273576n14 0 2000
9mrd273576n14 1 2000
--//你可以发现设置1000,实际上是对齐2000.
3.再重复看看:
alter system flush shared_pool;
ALTER SESSION SET EVENTS '10503 trace name context level 50, forever';
--//执行如上过程略.
SCOTT@test01p> select sql_id, child_number, executions from v$sql where sql_id = '9mrd273576n14';
SQL_ID CHILD_NUMBER EXECUTIONS
------------- ------------ ----------
9mrd273576n14 0 128
1 1872
2 2000
--//你可以发现设置50,实际上对齐边界128.
SCOTT@test01p> @ bind_cap 9mrd273576n14 ''
C200
------------------------------------------
select count(*) from t where c1=:instring
SQL_ID CHILD_NUMBER WAS NAME POSITION MAX_LENGTH LAST_CAPTURED DATATYPE_STRING VALUE_STRING
------------- ------------ --- -------------------- ---------- ---------- ------------------- --------------- -------------------------------
9mrd273576n14 0 YES :INSTRING 1 128 2017-10-19 22:08:16 VARCHAR2(128) X
1 YES :INSTRING 1 2000 2017-10-19 22:08:16 VARCHAR2(2000) X
4.总结:
--//仅仅作为前面测试的补充.如果应用存在大量绑定变量的分配长度引起的子光标太多,查看视图v$sql_shared_cursor主要BIND_LENGTH_UPGRADEABLE这个原因引起.
--//可以适当设置调整这个值.
ALTER SESSION SET EVENTS '10503 trace name context level <buffer length>, forever';
--//而这种情况主要出现在dml语句,主要集中在insert,update语句 比如.
insert into t values(:1,:2,....);
--//个人估计,如果32长度很容易突破,适当设置128可以一定程度减少子光标的产生.
--//如果绑定变量中字符串分配占用空间的长度变化,oracle会建立子光标。
--//参考连接:
http://blog.itpub.net/267265/viewspace-1993495/
--//oracle 可以通过一个10503事件设置大的缓存,测试看看:
$ oerr ora 10503
10503, 00000, "enable user-specified graduated bind lengths"
// *Cause:
// *Action:
--//参考链接: http://blog.itpub.net/267265/viewspace-2024389/
--//按照长度分级 :1-32,33-128,129-2000,2001-4000.
--//我前面的测试ALTER SESSION SET EVENTS '10503 trace name context level <buffer length>, forever';
--//我以前测试<buffer length> = 2000,如果不是对齐边界看看会是什么情况?
1.环境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0
create table t (c1 varchar2(4000));
2.测试:
alter system flush shared_pool;
ALTER SESSION SET EVENTS '10503 trace name context level 1000, forever';
declare
v_c1 varchar2(4000);
begin
for i in 1..4000 loop
v_c1 := rpad('X',i);
execute immediate 'select count(*) from t where c1=:instring' using v_c1 ;
end loop;
end;
/
--确定sql_id='9mrd273576n14'
SCOTT@test01p> select sql_id, child_number, executions from v$sql where sql_id = '9mrd273576n14';
SQL_ID CHILD_NUMBER EXECUTIONS
------------- ------------ ----------
9mrd273576n14 0 2000
9mrd273576n14 1 2000
--//你可以发现设置1000,实际上是对齐2000.
3.再重复看看:
alter system flush shared_pool;
ALTER SESSION SET EVENTS '10503 trace name context level 50, forever';
--//执行如上过程略.
SCOTT@test01p> select sql_id, child_number, executions from v$sql where sql_id = '9mrd273576n14';
SQL_ID CHILD_NUMBER EXECUTIONS
------------- ------------ ----------
9mrd273576n14 0 128
1 1872
2 2000
--//你可以发现设置50,实际上对齐边界128.
SCOTT@test01p> @ bind_cap 9mrd273576n14 ''
C200
------------------------------------------
select count(*) from t where c1=:instring
SQL_ID CHILD_NUMBER WAS NAME POSITION MAX_LENGTH LAST_CAPTURED DATATYPE_STRING VALUE_STRING
------------- ------------ --- -------------------- ---------- ---------- ------------------- --------------- -------------------------------
9mrd273576n14 0 YES :INSTRING 1 128 2017-10-19 22:08:16 VARCHAR2(128) X
1 YES :INSTRING 1 2000 2017-10-19 22:08:16 VARCHAR2(2000) X
4.总结:
--//仅仅作为前面测试的补充.如果应用存在大量绑定变量的分配长度引起的子光标太多,查看视图v$sql_shared_cursor主要BIND_LENGTH_UPGRADEABLE这个原因引起.
--//可以适当设置调整这个值.
ALTER SESSION SET EVENTS '10503 trace name context level <buffer length>, forever';
--//而这种情况主要出现在dml语句,主要集中在insert,update语句 比如.
insert into t values(:1,:2,....);
--//个人估计,如果32长度很容易突破,适当设置128可以一定程度减少子光标的产生.