(1) 选择最有效率的表名顺序(只在基于规则的优化器中有效):
ORACLE的解析器按照从右到左的顺序处理
FROM
子句中的表名,
子句中写在最后的表(基础表 driving
table
)将被最先处理,在
子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表。如果有3个以上的表连接查询, 那就需要选择交叉表(intersection
)作为基础表, 交叉表是指那个被其他表所引用的表.
(2)
WHERE
子句中的连接顺序.:
(3)
SELECT
子句中避免使用 ‘ * ‘:
ORACLE在内部执行了许多工作: 解析sql语句, 估算索引的利用率, 绑定变量 , 读数据块等;
(6) 使用DECODE函数来减少处理时间:
使用DECODE函数可以避免重复扫描相同记录或重复连接相同的表.
(7) 整合简单,无关联的数据库访问:
(8) 删除重复记录:
DELETE
FROM
EMP E
WHERE
E.ROWID > (
SELECT
MIN
(X.ROWID)
EMP X
X.EMP_NO = E.EMP_NO);
当删除表中的记录时,在通常情况下, 回滚段(
rollback
segments ) 用来存放可以被恢复的信息. 如果你没有
COMMIT
事务,ORACLE会将数据恢复到删除之前的状态(准确地说是恢复到执行删除命令之前的状况) 而当运用
时, 回滚段不再存放任何可被恢复的信息.当命令运行后,数据不能被恢复.因此很少的资源被调用,执行时间也会很短. (译者按:
只在删除全表适用,
是DDL不是DML)
(10) 尽量多使用
:
所释放的资源:
a. 回滚段上用于恢复数据的信息.
b. 被程序语句获得的锁
c. redo log buffer 中的空间
d. ORACLE为管理上述3种资源中的内部花费
(11) 用
Where
子句替换
HAVING
子句:
避免使用
sql keyword" style="list-style-type:none; list-style-position:initial; margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-width:initial; border-color:initial!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; background-color:initial!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
HAVING
只会在检索出所有记录之后才对结果集进行过滤. 这个处理需要排序,总计等操作. 如果能通过
子句限制记录的数目,那就能减少这方面的开销. (非oracle中)
on
、
where
having
这三个都可以加条件的子句中,
是最先执行,
次之,
最后,因为
是先把不符合条件的记录过滤后才进行统计,它就可以减少中间运算要处理的数据,按理说应该速度是最快的,
也应该比
快点的,因为它过滤数据后才进行
sum
,在两个表联接时才用
的,所以在一个表的时候,就剩下
跟
比较了。在这单表查询统计的情况下,如果要过滤的条件没有涉及到要计算字段,那它们的结果是一样的,只是
可以使用rushmore技术,而
就不能,在速度上后者要慢如果要涉及到计算的字段,就表示在没计算之前,这个字段的值是不确定的,根据上篇写的工作流程,
的作用时间是在计算之前就完成的,而
就是在计算后才起作用的,所以在这种情况下,两者的结果会不同。在多表联接查询时,
比
更早起作用。系统首先根据各个表之间的联接条件,把多个表合成一个临时表后,再由
进行过滤,然后再计算,计算完后再由
进行过滤。由此可见,要想过滤条件起到正确的作用,首先要明白这个条件应该在什么时候起作用,然后再决定放在那里
(12) 减少对表的查询:
SELECT
TAB_NAME
TABLES
(TAB_NAME,DB_VER) = (
SELECT
TAB_NAME,DB_VER
TAB_COLUMNS
VERSION = 604)
(14) 使用表的别名(Alias):
(15) 用EXISTS替代
IN
、用
NOT
EXISTS替代
NOT
:
在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接.在这种情况下, 使用EXISTS(或
EXISTS)通常将提高查询的效率. 在子查询中,
子句将执行一个内部的排序和合并. 无论在哪种情况下,monospace!important; font-size:1em!important; min-height:inherit!important">都是最低效的 (因为它对子查询中的表执行了一个全表遍历). 为了避免使用
IN
sql plain" style="list-style-type:none; list-style-position:initial; margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-width:initial; border-color:initial!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; background-color:initial!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,我们可以把它改写成外连接(
Outer
Joins)或
EXISTS.
例子:
(高效)
*
EMP (基础表)
EMPNO > 0
AND
EXISTS (
‘X
' FROM DEPT WHERE DEPT.DEPTNO = EMP.DEPTNO AND LOC = ‘MELB'
)
(低效)
DEPTNO
(
DEPTNO
DEPT
LOC = ‘MELB
')
SELECT EXECUTIONS , disK_READS, BUFFER_GETS,
ROUND(disK_READS/EXECUTIONS,2) Reads_per_run,
sql_TEXT
FROM V$sqlAREA
WHERE EXECUTIONS>0
AND BUFFER_GETS > 0
AND (BUFFER_GETS-disK_READS)/BUFFER_GETS < 0.8
ORDER BY 4 DESC;
(17) 用索引提高效率:
索引是表的一个概念部分,用来提高检索数据的效率,ORACLE使用了一个复杂的自平衡B-tree结构. 通常,通过索引查询数据比全表扫描要快. 当ORACLE找出执行查询和Update语句的最佳路径时, ORACLE优化器将使用索引. 同样在联结多个表时使用索引也可以提高效率. 另一个使用索引的好处是,它提供了主键(primary key)的唯一性验证.。那些LONG或LONG RAW数据类型, 你可以索引几乎所有的列. 通常, 在大型表中使用索引特别有效. 当然,你也会发现, 在扫描小表时,使用索引同样能提高效率. 虽然使用索引能得到查询效率的提高,但是我们也必须注意到它的代价. 索引需要空间来存储,也需要定期维护, 每当有记录在表中增减或索引列被修改时, 索引本身也会被修改. 这意味着每条记录的INSERT , DELETE , UPDATE将为此多付出4 , 5 次的磁盘I/O . 因为索引需要额外的存储空间和处理,那些不必要的索引反而会使查询反应时间变慢.。定期的重构索引是有必要的.:
ALTER INDEX <INDEXNAME> REBUILD <TABLESPACENAME>
(18) 用EXISTS替换disTINCT:
当提交一个包含一对多表信息(比如部门表和雇员表)的查询时,避免在SELECT子句中使用disTINCT. 一般可以考虑用EXIST替换, EXISTS 使查询更为迅速,因为RDBMS核心模块将在子查询的条件一旦满足后,立刻返回结果. 例子:
(低效):
SELECT disTINCT DEPT_NO,DEPT_NAME FROM DEPT D , EMP E
WHERE D.DEPT_NO = E.DEPT_NO
(高效):
SELECT DEPT_NO,DEPT_NAME FROM DEPT D WHERE EXISTS ( SELECT ‘X'
E.DEPT_NO = D.DEPT_NO);
(20) 在java代码中尽量少用连接符“+”连接字符串!
(21) 避免在索引列上使用
通常,
我们要避免在索引列上使用
NOT
sql plain" style="list-style-type:none; list-style-position:initial; margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-width:initial; border-color:initial!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; background-color:initial!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
会产生在和在索引列上使用函数相同的影响. 当ORACLE”遇到”
sql color1" style="list-style-type:none; list-style-position:initial; margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-width:initial; border-color:initial!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; background-color:initial!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,他就会停止使用索引转而执行全表扫描.
|
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。