浅谈Oracle绑定变量


绑定变量在OLTP环境下,被广泛的使用;这源于OLTP的特点和sql语句的执行过程,OLTP典型的事务短,类似的sql语句执行率高,并发大;Oracle在执行sql语句前会对sql语句进行hash运算,将得到的hash值和share pool中的library cache中对比,如果未命中,则这条sql语句需要执行硬解析,如果命中,则只需要进行软解析;硬解析的执行过程是先进行语义,语法分析,然后生成执行计划,最后执行sql语句,在OLTP系统中使用绑定变量可以很好的解决这个问题!

一:oltp环境下,使用绑定变量和不使用绑定变量对比
1:创建测试数据

  1. [oracle@dg53 ~]$ sqlplus hr/hr  
  2. SQL*Plus: Release 10.2.0.1.0 - Production on Thu Jun 14 16:54:46 2012  
  3. Copyright (c) 1982, 2005, Oracle.  All rights reserved.  
  4.  
  5. Connected to:  
  6. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  7. With the Partitioning, OLAP and Data Mining options  
  8.  
  9. SQL> create table t1 as select object_id,object_name from dba_objects;  
  10. Table created.  
  11.  
  12. SQL> create index i_t1 on t1(object_id);  
  13. Index created.  
  14.  
  15. SQL> exec dbms_stats.gather_table_stats('HR','T1',CASCADE=>TRUE);  
  16. PL/SQL procedure successfully completed. 

2:不使用绑定变量情况下,进行sql trace分析,执行1万次,需要硬解析10003次,其中包含递归解析,解析时间为19.37s,cpu消耗为17.62

  1. SQL> alter session set tracefile_identifier='HR01';  
  2. Session altered.  
  3.  
  4. SQL> alter session set sql_trace=TRUE;  
  5. Session altered.  
  6.  
  7. SQL> begin  
  8.   2  for i in 1..10000  
  9.   3  loop  
  10.   4  execute immediate 'select * from t1 where object_id='||i;  
  11.   5  end loop;  
  12.   6* end;  
  13.  
  14. PL/SQL procedure successfully completed.  
  15.  
  16. SQL> alter session set sql_trace=FALSE;  
  17. Session altered.  
  18.  
  19. OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS  
  20.  
  21. call     count       cpu    elapsed       disk      query    current        rows  
  22. ------- ------  -------- ---------- ---------- ---------- ----------  ----------  
  23. Parse    10003     17.62      19.37          0          0          0           0  
  24. Execute  10003      0.48       0.54          0          0          0           0  
  25. Fetch        7      0.00       0.01          1         13          0           4  
  26. ------- ------  -------- ---------- ---------- ---------- ----------  ----------  
  27. total    20013     18.10      19.92          1         13          0           4  
  28.  
  29. Misses in library cache during parse: 10000  
  30.  
  31. 10003  user  SQL statements in session.  
  32.     3  internal SQL statements in session.  
  33. 10006  SQL statements in session.  
  34.     0  statements EXPLAINed in this session.  
  35. ********************************************************************************  
  36. Trace file: dg53_ora_24818_HR01.trc  
  37. Trace file compatibility: 10.01.00  
  38. Sort options: default  
  39.  
  40.        0  session in tracefile.  
  41.    10003  user  SQL statements in trace file.  
  42.        3  internal SQL statements in trace file.  
  43.    10006  SQL statements in trace file.  
  44.    10006  unique SQL statements in trace file.  
  45.    80071  lines in trace file.  
  46.       78  elapsed seconds in trace file. 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 下一页

相关内容