課程實踐(一)續1

LuiseDalian發表於2014-01-18
案例二

點選(此處)摺疊或開啟

  1. --建立setup_rest.sh指令碼
  2. # setup_rest.sh
  3. # !/bin/bash
  4. cd $HOME/solutions/Common_Mistakes
  5. sqlplus / as sysdba <<EOF

  6. SET ECHO ON
  7. DROP USER jfv CASCADE;
  8. CREATE USER jfv IDENTIFIED BY jfv DEFAULT TABLESPACE users TEMPORARY TABLESPACE temp;
  9. GRANT CONNECT, RESOURCE, DBA TO jfv;
  10. CONN jfv/jfv

  11. DROP TABLE orders PURGE;
  12. CREATE TABLE orders
  13. (
  14.   order_id_char VARCHAR2(50) PRIMARY KEY,
  15.   order_total_number NUMBER,
  16.   customer_name VARCHAR2(300)
  17. );

  18. BEGIN
  19.   FOR i IN 1..500000 LOOP
  20.     INSERT INTO orders VALUES
  21.       (i, 100, \'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  22.                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  23.                 aaaaaaaaaaaaaaaaaa\');
  24.   END LOOP;
  25.   COMMIT;
  26. END;
  27. /

  28. BEGIN
  29.   FOR i IN 1..500000 LOOP
  30.     INSERT INTO orders VALUES
  31.       (500000 + i, 100, \'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  32.                          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  33.                          aaaaaaaaaaaaaaaaaa\');
  34.   END LOOP;
  35.   COMMIT;
  36. END;
  37. /

  38. DROP TABLE employees PURGE;
  39. DROP TABLE job_history PURGE;
  40. CREATE TABLE employees
  41. (
  42.   employee_id NUMBER PRIMARY KEY,
  43.   NAME VARCHAR2(500)
  44. );
  45. BEGIN
  46.   FOR i IN 1..500000 LOOP
  47.     INSERT INTO employees VALUES
  48.       (i, \'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  49.            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  50.            aaaaaaaaaaaaaaaaaa\');
  51.   END LOOP;
  52.   COMMIT;
  53. END;
  54. /

  55. CREATE TABLE job_history
  56. (
  57.   employee_id NUMBER,
  58.   job VARCHAR2(500)
  59. );
  60. BEGIN
  61.   FOR i IN 1..500000 LOOP
  62.     INSERT INTO job_history VALUES
  63.       (MOD(i, 1000), \'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  64.                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  65.                       aaaaaaaaaaaaaaaaaa\');
  66.   END LOOP;
  67.   COMMIT;
  68. END;
  69. /
  70. CREATE INDEX idx_job_history_empid ON job_history(employee_id);

  71. DROP TABLE OLD PURGE;
  72. DROP TABLE NEW PURGE;
  73. CREATE TABLE OLD
  74. (
  75.   NAME VARCHAR2(10),
  76.   other VARCHAR2(500)
  77. );

  78. CREATE TABLE NEW
  79. (
  80.   NAME VARCHAR2(10),
  81.   other VARCHAR2(500)
  82. );

  83. BEGIN
  84.   FOR i IN 1..500000 LOOP
  85.     INSERT INTO OLD
  86.       VALUES(i, \'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  87.                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  88.                  aaaaaaaaaaaaaaaaaa\');
  89.   END LOOP;
  90.   COMMIT;
  91. END;
  92. /

  93. BEGIN
  94.   FOR i IN 1..500000 LOOP
  95.     INSERT INTO NEW
  96.       VALUES(i + 500000, \'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  97.                           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  98.                           aaaaaaaaaaaaaaaaaa\');
  99.   END LOOP;
  100.   COMMIT;
  101. END;
  102. /
  103. EOF
  104. --執行setup_rest.sh指令碼建立環境, 以jfv使用者進入SQL*Plus
  105. --2.1 bad
  106. select count(*) from job_history jh, employees e
  107. where substr(to_char(e.employee_id), 1) = substr(to_char(jh.employee_id), 1);

  108.   COUNT(*)
  109. ----------
  110.     499500

  111. Elapsed: 00:00:08.19

  112. --2.2 good
  113. select count(*) from job_history jh, employees e where e.employee_id = jh.employee_id;

  114.   COUNT(*)
  115. ----------
  116.     499500

  117. Elapsed: 00:00:02.40
結論:在WHERE條件中不要在索引列上進行計算和使用函式

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

相關文章