-------------------------------------sql语句优化-----------------------------------------------------------
1、使用表别名
2、sql语句尽量用大写
4、oracle采用自下而上的顺序解析where子句,根据这个原理,那些可以滤掉最大数量记录的条件必须写在where子句的末尾
select * from emp
where sal>1000 and deptno=10; --deptno=10 sal>1000
5、select子句中避免只用*
6、删除重复记录 rowid
7、避免使用消耗资源的操作:
带有 distinct、union、minus、intersect、order by
8、先过滤在分组
9、根据需要用union all代替union
11、用exists代替in(分情况说),用 not exists 代替 not in
select deptno from dept d
where exists (select 1 from emp e where d.deptno=e.deptno)
select deptno from dept d
where deptno in (select deptno from emp)
12、尽量多使用 commit
13、用truncate代替delete
13、用索引提高效率
注:使用索引虽能得到查询效率的提高,但必须注意到他的代价。索引需要空间来存储,也需要定期维护,
每当有记录在表中增减或索引列被修改时,索引本身也会被修改。这意味着每条记录的insert、delete、update将为此付出更多的磁盘i/o,
一些不必要的索引反而会使查询反应时间变慢。
定期的重构索引:
alter index <indexname> rebuild <tablespacename>
14、用>=代替>
5 6
15、避免在索引列上使用 not
not 会产生和在索引列上使用函数相同的影响。当oracle遇到not,就会停止使用索引转而使用全表扫描
16、避免在索引列上使用计算和逻辑运算,理由同15
17、索引列上使用 is null 和 is not null 会导致索引失效
18、用 union 代替 or (适用索引列)
19、如果索引是建立在多个列上,只有在他的第一个列被where子句引用时,优化器才会只用该索引,当引用索引第二个列时,
优化器使用全表扫描而忽略索引扫描
20、where子句中使用‘!=’、‘||’、‘+’会停用索引