OLTP系统中尽量使用绑定变量

OLAP系统中不宜使用绑定变量,但如果是OLTP系统,就要尽量考虑使用绑定变量。

因为OLTP系统的特点是SQL执行非常频繁,并且用时非常短。

此时,重用shared pool中的执行计划,减少硬解析所节省的时间就非常可观了。

点击(此处)折叠或打开

  1. DROP TABLE t;
  2. CREATE TABLE t(col1 VARCHAR2(10));
  3.     
  4. INSERT INTO t SELECT to_char(ROWNUM) FROM dual CONNECT BY LEVEL <= 100000;


  5. EXECUTE runstats_pkg.rs_start;

  6. --未使用绑定变量
  7. DECLARE
  8.   TYPE rc IS REF CURSOR;
  9.   l_cursor rc;
  10. BEGIN
  11.   FOR i IN 1 .. 500 LOOP
  12.   OPEN l_cursor FOR \'SELECT col1 FROM t WHERE col1 = \' || to_char(i);
  13.   CLOSE l_cursor;
  14. END LOOP;
  15. END;
  16. /



  17. EXECUTE runstats_pkg.rs_middle;

  18. --使用绑定变量
  19. DECLARE
  20.   TYPE rc IS REF CURSOR;
  21.   l_cursor rc;
  22. BEGIN
  23.   FOR i IN 1 .. 500 LOOP
  24.   OPEN l_cursor FOR \'SELECT col1 FROM t WHERE col1 = :x\' USING i;
  25.   CLOSE l_cursor;
  26. END LOOP;
  27. END;
  28. /

  29. set serveroutput on size 10000;
  30. EXECUTE runstats_pkg.rs_stop(500);

  31. Run1 ran in 2936hsec
  32. Run2 ran in 1520hsec
  33. run 1 ran in 193.16% of the time
  34.         
  35. Name Run1 Run2 Diff
  36. STAT...enqueue releases 509 8 -501
  37. STAT...enqueue requests 509 8 -501
  38. STAT...db block gets from cach 557 50 -507
  39. STAT...db block gets 557 50 -507
  40. STAT...bytes received via SQL* 763 1,365 602
  41. LATCH.shared pool simulator 790 13 -777
  42. LATCH.call allocation 1,009 19 -990
  43. LATCH.session allocation 1,006 15 -991
  44. LATCH.enqueue hash chains 1,418 302 -1,116
  45. STAT...bytes sent via SQL*Net 342 1,459 1,117
  46. LATCH.simulator hash latch 1,501 33 -1,468
  47. STAT...recursive calls 3,506 1,519 -1,987
  48. STAT...calls to kcmgcs 6,022 37 -5,985
  49. LATCH.row cache objects 13,610 658 -12,952
  50. LATCH.shared pool 28,744 546 -28,198
  51. STAT...buffer is not pinned co 29,501 68 -29,433
  52. STAT...table scan blocks gotte 29,500 59 -29,441
  53. STAT...no work - consistent re 29,501 59 -29,442
  54. STAT...consistent gets from ca 34,509 78 -34,431
  55. STAT...consistent gets from ca 35,010 93 -34,917
  56. STAT...consistent gets 35,010 93 -34,917
  57. STAT...session logical reads 35,567 143 -35,424
  58. STAT...session uga memory 65,488 0 -65,488
  59. LATCH.cache buffers chains 70,767 1,214 -69,553
  60. STAT...table scan rows gotten ########## 31,680##########
  61. STAT...logical read bytes from########## 1,171,456##########
  62.         
  63. Run1 latches total versus runs ----difference and pct
  64. Run1 Run2 Diff Pct
  65. 121,445 4,247 -117,198#######%

  66. PL/SQL procedure successfully completed.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/17013648/viewspace-1071430/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/17013648/viewspace-1071430/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值