oracle索引学习手记

索引学习笔记

一、下列情况会使用索引失效
1、使用不等于运算符(<> !=)
2、使用 is null 或 is not null
3、使用like
4、使用函数(如:trunc substr to_date to_char instr等),解决方案:a、使用基于函数的索引;b、灵活书写SQL,避免在索引列上使用SQL函数。为了走索引,就不要在where子句中的索引列上使用函数。
5、比较不匹配的数据类型。这个比较难于发现问题。

二、创建索引
--创建索引
create index emp_temp_dept on emp_temp(deptno);
--查询索引信息
select index_name,index_type,table_name,tablespace_name from user_indexs;
--创建索引表空间
create tablespace index_tbs datafile '/u01/app/oracle/oradata/index_tbs1.dbf' size 100M autoextend on;
--创建索引语法
create [unique][bitmap] index [schema.]index_name
on [schema.]table_name(column_name[desc][asc])
[reverse] --反向索引
[tablespace tablespace_name]
[pctfree n] --保留空间比例
[initrans n] --分配事务数
[maxtrans n] --分配的最大事务数
[logging][nologging] --日志记录
[nosort] --不需要在创建索引时再按键值进行排序

--查询与索引列相关的信息
select index_name,table_name,column_name from user_ind_columns;

位图索引适用于没有大量更新、删除、插入任务的数据仓库。


--hash索引

--反向索引
--使用反向索引的好处是将值连续插入到索引中时能避免反向键急用。
--创建反向索引
create index emp_sal_reverse_idx on emp(sal) reverse;

--创建基于函数的索引
create index dept_dname_idx on dept(UPPER(dname));

三、监控索引的使用
1、启动对索引的监控
alter index emp_ename_bitmap_idx monitoring usage;

2、查看索引的使用情况
select index_name,table_name.monitoring,used from v$object_usage;

3、关闭索引用监控
alter index emp_ename_bitmap_idx nomonitoring usage;

四、重建索引
使用索引重建不影响用户使用索引,但是有一些限制条件,如不能使用DDL操作和DML操作。
重建索引以删除数据删除值占用的空间。
alter index emp_ename_bitmap_idx rebuild;
重建索引并迁移空间
alter index dept_dname_idx rebuild tablespace index_tbs1;

联机重建索引
alter index dept_dname_idx rebuild online;

合并索引碎片
alter index emp_job_bitmap_idx coalesce;
合并索引碎片可以释放磁盘空间。

五、删除索引
drop index dept_dname_idx;

猜你喜欢

转载自www.cnblogs.com/owenzou/p/9706677.html