码迷,mamicode.com
首页 > 数据库 > 详细

oracle sql优化技巧

时间:2017-03-23 00:54:04      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:size   varchar2   unique   通过   extend   重复   --   程序   union   

数据库方面一直是自己的薄弱项,现在以本文慢慢积累总结oracle sql优化的一些技巧。

1.首先大家很容易想到的一切优化技巧--索引,索引有啥用?索引在表数据量很大时添加索引确实能加快查询速度,通过索引查询能很好地避免全表扫描。

   但应该也要注意的时这是在数据量较大的时候。同时数据较小时,反而浪费索引空间。另外,添加索引之后数据的插入,更新反而会变慢,在插入或修改记录

 时需要新建索引并排序。

   索引创建语句:

   create [unique] index xxx on A(column 1,column 2)

   关于唯一索引和普通索引这里不得不提一句 唯一索引要求字段值不能重复,而普通索引则不要求

   另外唯一索引与主键:大家都知道主键是使用来表示记录的唯一性,只能有一个字段表示,不允许为空。而唯一索引则可以在多个值唯一的字段上建,且允许为空。

2.大家显而易见避免使用select * 而用select 字段

3.避免在在添加索引的字段使用计算 例如 select * from A where num*2>6;-->优化:select * from B where num>6/2;

4.避免在添加索引的字段使用函数  例如 select * from where B where to_char(num)= ‘c‘;-->若在程序大量此类语句,建议修改索引添加函数

   修改索引字段语句:drop index xxx; create index xxx on B(to_char(num));

5.能使用>=就用 因为>=4会直接定位到4,而>3 会先定位到3 在定位到4

6.使用exist 代替 in 例如:select e.deptno from employee e where e.deptno in(select d.deptno from dept d);

  -->优化:select e.deptno from employee e where exist (select d.deptno from dept d where d.deptno = e.deptno);

7.使用Union all代替 or,num索引字段段在使用or后,将放弃索引引擎而改为全表扫描。 例如:select * from A where num=1 or num=2

  -->优化:select * from A where num = 1 Union all select * from B where num = 2;

8.在数据量很大的时候,除了使用索引也可以用表分区。分散存储,同时分散IO,加快查询。

 表分区语句

   create table A (

   AId varchar2,

    AName varchar2,

    Score  int   

  )

 partition by Score(Score) (

   partition jige values less(maxvalue) tablespace a1,

      partition bujige values less(maxvalue) tablespace a2

 )

 这里顺带提一下创建表空间语句:

 第一步:创建临时表空间

 create temporary tablespace A

 tempfile  ‘D:\oracledata\a.dbf‘

 size 50m 

 autoextend on

 next 50m maxsize 20480m

 extent management local;

第一步:创建表空间

create tablespace B logging

 datafile  ‘D:\oracledata\b.dbf‘

 size 50m 

 autoextend on

 next 50m maxsize 20480m

 extent management local;

oracle sql优化技巧

标签:size   varchar2   unique   通过   extend   重复   --   程序   union   

原文地址:http://www.cnblogs.com/kimoyoyo21/p/6602541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!