Oracle 索引

来源:互联网 发布:windows loader下载 编辑:程序博客网 时间:2024/04/29 15:32

转自:http://lzkyo.iteye.com/blog/692680


索引和对应的表应该位于不同的表空间中(primary key  (id) using index tablespace mytable_index),oracle能够并行读取位于不同硬盘上的数据,可以避免产生I/O冲突 。

B树索引:在B树的叶节点中存储索引字段的值与ROWID。唯一索引和不唯一索引都只是针对B树索引而言。 Oracle最多允许包含32个字段的复合索引。

1.索引的创建方法
(1)*Tree索引
Create index indexname on tablename(columnname[columnname...])
(2)反向索引
Create index indexname on tablename(columnname[columnname...]) reverse
(3)降序索引
Create index indexname on tablename(columnname DESC[columnname...])
(4)位图索引
Create BITMAP index indexname on tablename(columnname[columnname...])
(5)函数索引
Create index indexname on tablename(functionname(columnname))

注意:创建索引后分析要索引才能起作用。
analyze index indexname compute statistics;

2.索引创建策略
(1).导入数据后再创建索引
(2).不需要为很小的表创建索引
(3).对于取值范围很小的字段(比如性别字段)应当建立位图索引
(4).限制表中的索引的数目
(5).为索引设置合适的PCTFREE值
(6).存储索引的表空间最好单独设定

3.索引使用场合及建议
(1)B*Tree索引
    常规索引,多用于oltp系统,快速定位行,应建立于高cardinality列(即列的唯一值除以行数为一个很大的值,存在很少的相同值)。
(2)反向索引
B*Tree的衍生产物,应用于特殊场合,在ops环境加序列增加的列上建立,不适合做区域扫描。
(3)降序索引
B*Tree的衍生产物,应用于有降序排列的搜索语句中,索引中储存了降序排列的索引码,提供了快速的降序搜索。
(4)位图索引
位图方式管理的索引,适用于OLAP(在线分析)和DSS(决策处理)系统,应建立于低cardinality列,适合集中读取,不适合插入和修改,提供比B*Tree索引更节省的空间。
(5)函数索引
B*Tree的衍生产物,应用于查询语句条件列上包含函数的情况,索引中储存了经过函数计算的索引码值。可以在不修改应用程序的基础上能提高查询效率。

4.索引例
创建不唯一索引
Sql代码 复制代码
  1. create index emp_ename on employees(ename)    
  2. tablespace users storage(......) pctfree 0;   
Sql代码  收藏代码
  1. create index emp_ename on employees(ename)   
  2. tablespace users storage(......) pctfree 0;   



创建唯一索引

Sql代码 复制代码
  1. create unique index emp_email on employees(email)    
  2. tablespace users;   
Sql代码  收藏代码
  1. create unique index emp_email on employees(email)   
  2. tablespace users;   



创建位图索引

Sql代码 复制代码
  1. create bitmap index emp_sex on employees(sex)    
  2. tablespace users;   
Sql代码  收藏代码
  1. create bitmap index emp_sex on employees(sex)   
  2. tablespace users;   



创建反序索引

Sql代码 复制代码
  1. create unique index order_reinx on orders(order_num,order_date)    
  2. tablespace users reverse;   
Sql代码  收藏代码
  1. create unique index order_reinx on orders(order_num,order_date)   
  2. tablespace users reverse;   



创建函数索引(函数索引即可以是普通的B树索引,也可以是位图索引)

Sql代码 复制代码
  1. create index emp_substr_empno on employees(substr(empno,1,2)) tablespace users;   
Sql代码  收藏代码
  1. create index emp_substr_empno on employees(substr(empno,1,2)) tablespace users;   



修改索引存储参数(与表类似,INITIAL和MINEXTENTS参数在索引建立以后不能再改变)
alter index emp_ename storage(pctincrease 50);

由于定义约束时由oracle自动建立的索引通常是不知道名称的,对这类索引的修改经常是利用alter table ..using index语句进行的,而不是alter index语句

利用下面的语句将employees表中primary key约束对应的索引的PCTFREE参数修改为5

Sql代码 复制代码
  1. alter table employees enable primary key using index pctfree 5;   
Sql代码  收藏代码
  1. alter table employees enable primary key using index pctfree 5;   



5.清理索引碎片
(1).合并索引(只是简单的将B树叶结点中的存储碎片合并在一起,并不会改变索引的物理组织结构)

Sql代码 复制代码
  1. alter index emp_pk coalesce;   
Sql代码  收藏代码
  1. alter index emp_pk coalesce;   



(2).重建索引(不仅能够消除存储碎片,还可以改变索引的全部存储参数设置,并且可以将索引移动到其它的表空间中,重建索引
实际上就是再指定的表空间中重新建立一个新的索引,然后删除原来的索引)

Sql代码 复制代码
  1. alter index emp_pk rebuild;   
Sql代码  收藏代码
  1. alter index emp_pk rebuild;   



6.删除索引

Sql代码 复制代码
  1. drop index emp_ename;   
Sql代码  收藏代码
  1. drop index emp_ename;   


如果索引中包含损坏的数据块,或者包含过多的存储碎片,需要首先删除这个索引,然后再重建它.
如果索引是在创建约束时由oracle自动产生的,可以通过禁用约束或删除约束的方法来删除对应的索引.
在删除一个表时,oracle会自动删除所有与该表相关的索引.

7.索引数据字典
all_indexes/dba_indexes/user_indexes 索引的基本信息
all_ind_columns/dba_ind_columns/user_ind_columns 索引对应的字段信息
原创粉丝点击