1. 避免使用“*”替代所有列
2. 删除表所有行时用 TRUNCATE 替代 DELETE
3. 在确保完整性的情况下多用 COMMIT 语句
4. 减少表的查询次数 一次查询获得的数据尽量不要去通过更多次的查询获得。
5. 用 EXISTS 替代 IN
select ENAME, JOB, SAL, DEPTNO
from EMP
where DEPTNOin (select DEPTNO from DEPT
where LOC ='NEW YORK');
select ENAME, JOB, SAL, DEPTNO
from EMP
where exists(select 1 from DEPT
where DEPT.DEPTNO = EMP.DEPTNO
and DEPT.LOC = 'NEW YORK')
6. 用 WHERE 替代 HAVING(若使用having可减少查询次数,则使用having)
Select deptno, count(1)
From emp
Group by deptno having(deptno<20);
Select deptno, count(1)
From emp
Where deptno<30
Group by deptno;
7. 使用“<=”或“>=”替代“<”或“>”
select name from student where score < 70;
select name from student where score<=69;
8.表的连接
(1) FROM 表的顺序 大表往前放,小表往后放。(大小指的是数据量)
(2) WHERE 子句的连接顺序
Oracle 解析器解析 WHERE 子句的顺序是从下到
上,所以应该将那些可以过滤掉最大数量数据的条件
放在 WHERE 子句末尾。
(3)尽量使用表的别名