ITPub博客

首页 > 数据库 > Oracle > 课程实践(一)续1

课程实践(一)续1

原创 Oracle 作者:luisedalian 时间:2014-01-18 07:37:57 0 删除 编辑
案例二

点击(此处)折叠或打开

  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/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 课程实践(一)
请登录后发表评论 登录
全部评论

注册时间:2012-02-06

  • 博文量
    1986
  • 访问量
    5642793