OLTP系統中儘量使用繫結變數

LuiseDalian發表於2014-01-16

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章