oraclebind variable

绑定变量是为了减少解析的,比如你有个语句这样 select aaa,bbb from ccc where ddd=eee;
 如果经常通过改变eee这个谓词赋值来查询,像如下 
select aaa,bbb from ccc where ddd=fff; 
select aaa,bbb from ccc where ddd=ggg; 
select aaa,bbb from ccc where ddd=hhh; 
每条语句都要被数据库解析一次,这样比较浪费资源,如果把eee换成“:1”这样的绑定变量形式,无论ddd后面是什么值,都不需要重复解析

下面有个例子:
    从Oracle的SGA的构成来看,它是推崇使用 参数绑定的。使用参数绑定可以有效的使用Share Pool,对已经缓存的SQL不用再硬解析,能明显的提高性能。

  具体实践如下:

SQL>create table test (a number(10));

再创建一个存储过程:

create or replace procedure p_test is
i number(10);
begin
i := 0;
  while i <= 100000 loop
   execute immediate ' insert into test values (' || to_char(i) || ')';
   i := i + 1;
end loop;

commit;

end p_test;

先测试没有使用参数绑定的:

运行p_test 后,用时91.111秒

再创建一个使用参数绑定的:

create or replace procedure p_test is
i number(10);
begin
i := 0;
while i <= 100000 loop
   execute immediate ' insert into test values (:a)'
     using i;
   i := i + 1;
end loop;
commit;

end p_test;

运行p_test 后,用时55.099秒.

从上面的运行时间可以看出,两者性相差 39.525%,可见,用不用参数绑定在性能上相差是比较大的。

原文地址:https://www.cnblogs.com/kelin1314/p/1931466.html