1、创建索引
-- 索引种类 -- 普通索引create index idx_emp_index_sal on emp_index(sal); -- 唯一索引create unique index uq_idx_emp_index_ename on emp_index(ename);-- 组合索引create index idx_emp_index_ename_job on emp_index(ename,job); -- 唯一组合索引create unique index idx_emp_index_empno_job on emp_index(empno,job); -- 位图索引 create bitmap index idx_emp_index_deptno on emp_index(deptno);-- 基于函数的索引create index idx_emp_index_comm on emp_index(nvl(comm,0)); -- 删除索引drop index idx_emp_index_comm;-- 数据字典 查看表的索引select * from user_indexes where table_name = 'emp_index';
2、索引失效
-- 索引失效-- 第一种情况 隐式转换导致索引失效-- 由于字段empno 的数据类型是 number 跟字符'7500'去比较select * from emp e where empno > '7500'-- 建表 select * from emp_index;drop table emp_index;create table emp_index as select * from emp;-- 唯一组合索引create index idx_emp_index_empno_job_sal on emp_index(job,sal); -- 唯一索引create unique index uq_idx_emp_index_ename on emp_index(empno);-- 对索引列进行算数运算导致索引失效select * from emp_index e where empno + 100 > 7500-- 函数导致索引失效select * from emp_index e where substr(empno,3,2) > 50;-- 总结:对字段做处理就会导致索引失效 因为它已经不是原来的字段了-- 以下使用会使索引失效,应避免使用a. 使用 <> 、not in 、not exist、!=b. like "%_"c. 单独引用复合索引里非第一位置的索引列select * from emp_index order by job,sal;-- 这个sql是用不到索引的 取数条件只用到组合索引的第二个字段select * from emp_index e where e.sal > 2000;-- 取数条件只用到组合索引的第一个字段 可以用到索引select * from emp_index e where e.job = clerk;-- 取数条件用到组合索引的两个字段 可以用到索引 select * from emp_index e where e.job = clerk and e.sal > 1500;-- 创建索引 表本身的数据是不会变的-- 可以看做有一张索引表 把索引字段与物理地址给保存起来 并排序select rowid,empno from emp_index order by empno;-- 取数的时候先去索引表里面找到对应的 rowid 再根据rowid去取对应的数据 select * from emp_index e where e.empno > 7500;