1、OM emp E WHERE E.dept_no = D.dept_no);4.2.9 使用NOT EXISTS代替NOT IN语句 SELECT . FROM empWHERE dept_no NOT IN ( SELECT dept_no FROM dept WHERE dept_cat = A);SELECT . FROM emp E WHERE NOT EXISTS ( SELECT X FROM dept WHERE dept_no = E.dept_noAND dept_cat = A);4.2.10 使用union all 代替union 语句Union - 进行排序Union
2、All - 不排序4.2.11 使用Union和IN代替OR语句Loc_no, region上有索引 SELECT . FROM location WHERE loc_id = 10 OR region = MELBOURNE; SELECT . FROM location WHERE loc_id = 10UNION ALLSELECT . FROM location WHERE region = MELBOURNE;SELECT . FROM location WHERE loc_id = 10OR loc_id = 20 OR loc_id = 30; SELECT . FROM loc
3、ation WHERE loc_id IN (10, 20, 30);5 高效索引5.1 索引的使用使用索引时,要考虑以下因素:1) 索引列的计算2) 索引列的增加3) 索引列不要用NOT4) 索引中空值的使用IS NULL, IS NOT NULL5) 索引列的数据类型的变换EMP_TYPE为varchar2类型,下列语句使用索引SELECT . FROM emp WHERE emp_type = 123; SELECT . FROM emp WHERE TO_NUMBER(emp_type) = 123;SELECT . FROM emp WHERE emp_type = 123;5.2
4、增加索引增加索引要考虑多种因素,要考虑对更新、插入的影响等。1) 频繁查询不超过大表1%15%的场合。2) 列的不同值3) 列4) 列5) 同表6 解析表的使用方法6.1 SQLTRACE6.1.1 参数表的生成$sqlplus s /SQL$ORACLE_HOME/rdbms/admin/utlxplan.sqlSQL commitSQL exit6.1.2 AUTOTRACE的用法afe57gc2/users/home/ino 29 sqlplus sys/*SQL $ORACLE_HOME/sqlplus/admin/plustrce.sqlSQL drop role plustrace
5、;:SQL grant plustrace to ops$ino;SQL exit6.1.3 AUTOTRACE功能的使用afe57gc2/oracle/product/8.1.7 37 sqlplus /SQL set autotrace onSQL select * from wcattori;执行计划- 0 SELECT STATEMENT Optimizer=CHOOSE 1 0 TABLE ACCESS (FULL) OF WCATTORI統計- 0 recursive calls 12 db block gets 4 consistent gets 0 physical reads
6、 0 redo size 4779 bytes sent via SQL*Net to client 314 bytes received via SQL*Net from client 1 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 0 rows processed SQL6.2 TKPROF6.2.1 初始化afe57gc2/oracle/product/8.1.7 40 sqlplus /SQL ALTER SESSION SET TIMED_STATISTICS = true;SQL ALTER S
7、ESSION SET MAX_DUMP_FILE_SIZE = 500;6.2.2 SQL Trace开始SQL ALTER SESSION SET SQL_TRACE = true;6.2.3 实例SQL SELECT * FROM WCATTORI;6.2.4 SQL Trace结束SQL ALTER SESSION SET SQL_TRACE = false;SQL exit6.2.5 内容变换afe57gc2/users/home/ino 44 suPassword:# chmod 777 /oracle/product/8.1.7/rdbms/log/ora_14186_wca1.t
8、rc# exitafe57gc2/users/home/ino 46 tkprof $ORACLE_HOME/rdbms/log/ora_14186_wca1.trc output.txtTKPROF: Release 8.1.7.2.0 - Production on 木 Apr 18 15:55:17 2002(c) Copyright 2000 Oracle Corporation. All rights reserved.備考:出力先SQL column name format A20SQL column value format A30SQL select name, value from v$parameter where name like %dump_dest;NAME VALUE- -background_dump_dest ?/rdbms/loguser_dump_dest ?/rdbms/logcore_dump_dest ?/dbsSQL6.2.6 内容确认afe57gc2/users/home/ino 47 cat output.txt (一部抜粋)SELECT *FROM WCATTORIcall count cpu elapsed disk query current rows- - - - - - -