課程實踐(二)

LuiseDalian發表於2014-01-18
使用不同的訪問路徑

案例一

點選(此處)摺疊或開啟

  1. --執行ap_setup.sh指令碼搭建環境
  2. # ap_setup.sh指令碼
  3. # !/bin/bash
  4. sqlplus / as sysdba <<EOF

  5. alter user sh identified by sh account unlock;
  6. grant dba to sh;
  7. exit;
  8. EOF

  9. --以SH登入,執行指令碼idx_setup.sql
  10. sh@TESTDB11>@idx_setup.sql
  11. -- idx_setup.sql
  12. DROP TABLE mysales PURGE;
  13. CREATE TABLE mysales AS SELECT * FROM sh.sales;
  14. INSERT INTO mysales SELECT * FROM mysales;
  15. COMMIT;
  16. INSERT INTO mysales SELECT * FROM mysales;
  17. COMMIT;
  18. INSERT INTO mysales SELECT * FROM mysales;
  19. COMMIT;
  20. INSERT INTO mysales SELECT * FROM mysales;
  21. COMMIT;
  22. INSERT INTO mysales SELECT * FROM mysales;
  23. COMMIT;
  24. INSERT INTO mysales VALUES (0, 0, SYSDATE, 0, 0, 0, 0);
  25. COMMIT;

  26. EXEC dbms_stats.gather_schema_stats(\'SH\');

  27. --with_and_without_index.sql
  28. SET ECHO ON
  29. SET TIMING ON
  30. SET AUTOTRACE TRACE ONLY
  31. SET PAGESIZE 1000

  32. ALTER SYSTEM FLUSH SHARED_POOL;
  33. ALTER SYSTEM FLUSH BUFFER_CACHE;

  34. select * from mysales where prod_id = 0;

  35. SET TIMING OFF;
  36. SET AUTOTRACE OFF;

  37. --執行with_and_without_index.sql指令碼
  38. sh@TESTDB11>@with_and_without_index.sql

  39. Elapsed: 00:00:41.19

  40. Execution Plan
  41. ----------------------------------------------------------
  42. Plan hash value: 3597614299

  43. -----------------------------------------------------------------------------
  44. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  45. -----------------------------------------------------------------------------
  46. | 0 | SELECT STATEMENT | | 402K| 11M| 40161 (1)| 00:08:02 |
  47. |* 1 | TABLE ACCESS FULL| MYSALES | 402K| 11M| 40161 (1)| 00:08:02 |
  48. -----------------------------------------------------------------------------

  49. Predicate Information (identified by operation id):
  50. ---------------------------------------------------

  51.    1 - filter(\"PROD_ID\"=0)


  52. Statistics
  53. ----------------------------------------------------------
  54.          45 recursive calls
  55.           1 db block gets
  56.      142137 consistent gets
  57.      142055 physical reads
  58.           0 redo size
  59.         970 bytes sent via SQL*Net to client
  60.         523 bytes received via SQL*Net from client
  61.           2 SQL*Net roundtrips to/from client
  62.           5 sorts (memory)
  63.           0 sorts (disk)
  64.           1 rows processed
  65.           

  66. -- create_mysales_index.sql

  67. SET ECHO ON
  68. CREATE INDEX idx_mysales_prodid on mysales(prod_id) nologging compute statistics;

  69. --再次執行with_and_without_index.sql指令碼
  70. sh@TESTDB11>@with_and_without_index.sql

  71. Elapsed: 00:00:00.49

  72. Execution Plan
  73. ----------------------------------------------------------
  74. Plan hash value: 2732191377

  75. --------------------------------------------------------------------------------------------------
  76. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  77. --------------------------------------------------------------------------------------------------
  78. | 0 | SELECT STATEMENT | | 402K| 11M| 6003 (1)| 00:01:13 |
  79. | 1 | TABLE ACCESS BY INDEX ROWID| MYSALES | 402K| 11M| 6003 (1)| 00:01:13 |
  80. |* 2 | INDEX RANGE SCAN | IDX_MYSALES_PRODID | 402K| | 821 (1)| 00:00:10 |
  81. --------------------------------------------------------------------------------------------------

  82. Predicate Information (identified by operation id):
  83. ---------------------------------------------------

  84.    2 - access(\"PROD_ID\"=0)


  85. Statistics
  86. ----------------------------------------------------------
  87.          51 recursive calls
  88.           0 db block gets
  89.         110 consistent gets
  90.          21 physical reads
  91.           0 redo size
  92.         974 bytes sent via SQL*Net to client
  93.         523 bytes received via SQL*Net from client
  94.           2 SQL*Net roundtrips to/from client
  95.           6 sorts (memory)
  96.           0 sorts (disk)
  97.           
  98. --建立idx_cleanup.sql指令碼,清理環境
  99. --idx_cleanup.sql
  100. SET ECHO ON
  101. DROP TABLE mysales purge;

  102. --執行idx_cleanup.sql
  103. sh@TESTDB11>@idx_cleanup.sql
結論:當資料量大,而檢索的資料又比較少時,使用索引的效率會更好。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17013648/viewspace-1072456/,如需轉載,請註明出處,否則將追究法律責任。

相關文章