`

oracle 调优

阅读更多

 

1   Oracle 调优:

       1 在数据量比较大的表上建立相关索引   ----create    index  abc_b_index  on abc b ;

 

   2 sql 语句的调优:

            1 > where 语句查询数据的时候将数据量大的条件放在 where 最后   oracle 查询自上向下 where name='john' and  sal>1000 交换后就会影响效率

            2 > select 语句避免使用 *

     3> 减少访问数据库的次数

          select ename ,job, sal from emp where empno=34; select ename ,job,sal from emp where=35;

         ->select ename,job,sal from emp where empno=34 or empno=35;

     4> 采用 decode 函数 :decode( 条件 , 1, 翻译值 1, 2, 翻译值 2……, 缺省值 )

           if( 条件 = 1){

return 翻译值 1;}else if( 条件 = 2){

return 2}else{

      return 缺省值 }

 

Select count(*),sum(sal) from emp where ename='smith' and deptno=20;

Select count(*),sum(sal) from emp where ename='smith' and deptno=10;

 

->select count(decode(deptno,10,'x',null)) d10_count,

         count(decode(deptno,20,'x',null)) d20_count,

    sum(decode(deptno,10,sal,null)) d10_sum,

    sum(decode(deptno,20,sal,null)) d20_sum,

    from emp where ename='smith'

5> 删除重复记录      

      oracle 中在查询的结果集中没条数据都有一个 rowid 是唯一的,及时所有数据字段内容都相同 rowid 也不会相同只要找到最大的 rowid 就能删除全

       部重复数据                      

        方法一   -> 创建一个相同备份表          create table table_test_temp as select * from tbl_test;

   -> 在原有表中加记录               insert into tbl_test select * from tbl_test;

  -> 查询重复记录信息               select id,name,pid ,count(*) from tbl_test group by id,name ,pid having count(*)>1

  -> 删除重复记录                      delete from tbl_test_temp x where x.rowid <(select max(rowid) from tbl_test_temp e

                                                      where  x.id=e.id)            

 

     方法二   -> 临时表保存不重复数据           create table  temp as select  distinct * from tbl_test_temp

-> 丢弃原来的表                                 drop  table tbl_test_temp;

-> 创建原来的表                                create table tbl_test_temp as select * from temp;

-> 丢弃临时表                                    drop  table  temp

 

6> truncate  代替 Delete

                    当用 delete 删除记录的时候,如果没有执行 commit 提交事务,回滚段( rollback segments )可以保存 delete 的数据

                    oracle 可以恢复到 delete 之前的状态,运用 truncate 时,回滚段不再放任何可以被恢复的数据   当执行命令是立即彻底

                    删除,占很少的资源

         7> 尽量多用 commit        

                   commit 可以释放资源     包括贵滚段恢复数据的空间   ;被程序语句获得的锁; redo  log  buffer 中的空间

         8> count(*) 要比 count(1)

         9> where 子句代替 having 子句   能减少 having 的开销   having 是在 groupby 的基础上进行查询的

        10> 在子查询语句中尽量减少对表的查询

                   select * from emp where deptno in(select deptno form dept where loc like 'new york' ) or deptno in (select deptno

from dept where dname like 'sales')

select *  from emp where deptno in (select deptno from dept where loc like 'newyork' or dname like 'sales')

11> Exist in(not exist not in)

in 是把外表和那表作 hash join ,而 exists 是对外表作 loop ,每次 loop 再对那表进行查询。这样的话, in 适合内外表都很大的情况, exists 适合外表结果集很小的情况。 in 是把外表和那表作 hash join ,而 exists 是对外表作 loop ,每次 loop 再对那表进行查询。

如: select * from emp where deptno in (select deptno from dept)

(或者) select * from emp where exists(select 'x' from dept where dept.deptno=emp.deptno)

分析如下:

Select * from T1 where x in ( select y from T2 )

执行的过程相当于 :

select *   from t1, ( select distinct y from t2 ) t2

where t1.x = t2.y;

select * from t1 where exists ( select null from t2 where y = x )

执行的过程相当于 :

for x in ( select * from t1 )

   loop

      if ( exists ( select null from t2 where y = x.x )

      then

         OUTPUT THE RECORD

      end if

end loop

  T1 不可避免的要被完全扫描一遍

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics