oracle之sql语句优化

sql语句的优化
  1.在where子句中使用 is null 或 is not null 时,oracle优化器就不能使用索引了.


 2.对于有连接的列,即使最有一个是静态的值,优化器也不会使用索引
  比如: select * from employss where first_name||''||last_name='Beill cliton'
   要写成 :select * from employss where first_name='Beill' and last_name='Beill cliton'
   这时oracle 就会采用 基于 last_name 的索引


  3.带通配符 (%) 的 like 语句
   比如: select * from employee where last_name like '%cliton%';   
            %在词首出现oracle系统就不能使用 last_name 的索引;
     select * from employee where last_name like 'cliton%'


4:order by
     任何在order by语句的非索引项或者表达式都会降低查询效率;

5:NOT  
   <>是就相当于NOT
   ① select  * from  salary<>3000;
   ② select *  from salary >3000 or salary <3000
   ①和②的查询结果是一样的,但②允许oracle对salary列使用索引,效率高

NOT会产生在和在索引列上使用函数相同的影响. 当ORACLE”遇到”NOT,他就会停止使用索引转而执行全表扫描.


6:  exist 代替 in
     not exist  代替  not  in
     select * from A  where id in(select id from B)
   以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B表中的 id相等,如果相等则将  A表的记录加入结果集中,直到遍历完A表的所有记录.
     exist  比 in 的执行效率更高

   

7:



8.where条件的顺序




9:UNION ALL  和 UNION


猜你喜欢

转载自flyvszhb.iteye.com/blog/1827051