1. 减少I/O操作:
SELECT COUNT(CASE WHEN empno>20 THEN 1 END) c1,COUNT(CASE WHEN empno<20 THEN 1 END) c2
FROM emp;
2. 通过rowid访问
SELECT ROWID,emp.* FROM emp
WHERE ROWID=chartorowid('AAAHW7AABAAAMUiAAA')
3. 使用索引唯一扫描
SELECT empno,ename FROM emp
WHERE empno='2000'
4. 使用并连接符号会使oracle忽略使用索用,即使是唯一索引
SELECT empno,ename FROM emp
WHERE empno||ename='2000naem'
改成这样就可使用索引了
SELECT * FROM emp
WHERE empno=2000 AND ename='dd'
5. 索引范围扫描
SELECT * FROM emp
WHERE empno<7000
6. where条件子句的解析顺序是从下到上的
SELECT a.empno,b.dname FROM emp a,dept b
WHERE a.ename<'CLERK'
AND a.deptno=b.deptno;
耗时1.016秒
SELECT a.empno,b.dname FROM emp a,dept b
WHERE a.deptno=b.deptno
AND a.ename<'CLERK';
耗时0.813秒
7. 使用通配符会使oracle不去使用索引
SELECT ename FROM emp
WHERE ename LIKE '%C%'
应改成
SELECT ename FROM emp
WHERE ename LIKE 'C%'
8. 使用唯一索引查找精确值是最快的,而索引范围扫描比较适合查找>=,<=的数据
SELECT a.itemid
FROM pt_sche_detail a,
pt_post_role b
WHERE a.itemid = b.taskid
AND a.docid = 2281
AND a.itemid != 1169015
AND a.status != 0
AND b.posttype = 1
AND b.roleid = 1022
AND b.roletype = 1
上面的语句改成:
SELECT a.itemid
FROM pt_sche_detail a,
pt_post_role b
WHERE a.itemid = b.taskid
AND a.docid = 2281
AND a.itemid != 1169015
AND a.status != 0
AND b.taskid IN
(SELECT itemid
FROM pt_sche_detail temp
WHERE temp.docid = 2281
AND rownum <= (SELECT COUNT(itemid) FROM pt_sche_detail temp WHERE temp.docid = 2281))
AND b.roleid = 1022
AND b.roletype = 1
AND b.posttype = 1