Oracle 索引
一、B树索引
1. 创建方式
格式: create index index_name on table_name(column_name, ...); 示例: create index idx_emp_name on emp(emp_name);
“ , ... ”:标识创建多列复合索引以逗号隔开
2. 删除索引
drop index index_name
ps:Oracle 扫描方式、假如字段含有 null 值,导致索引失效
二、位图索引
格式: create bitmap index index_name on table_name(column_name, ...); 示例: create bitmap index idx_emp_gender on emp(emp_gender);
位图索引一般用于基数小的列,基数和数据行数比例为 1% 时,Oracle推荐使用位图索引
位图索引最后是组合使用,效果最佳
三、函数索引
1. 为什么使用函数索引
假设,此时 emp 表中存在 emp_name 字段,此字段原本添加过B树索引。
emp_name 存在一个值为 “jack”,此时查询语句:
select * from emp where emp_name='JACK';
这样并不能,查询出数据来,需要用到一个函数 “upper()”
select * from emp where upper(emp_name)='JACK';
但这样一来,B树索引就失效了。此时,就需要用到函数索引了。
2. 函数索引的创建
格式: create index index_name on table_name(function_name(column_name)); 示例: create index idx_emp_name on emp(upper(emp_name));
3. 如何使 like 语句百分号在前也使用索引
1> 针对使用 like 语句的列创建 reverse() 函数的函数索引,这里假设列面为 emp_name
2> 使用:select * from emp where reverse(emp_name) like reverse('%xx');
四、索引扫描类型
1. 全表扫描 (FULL TABLE SCAN)
2. 索引唯一扫描(index unique scan)
3. 索引范围扫描(index range scan)
4. 索引快速全扫描(index fast full scan) (默认id索引和字段1索引以建立)
当select 字段和where后面的条件查询字段都出现在索引列里面的时候 这时就会触发索引快速全扫描
例如: select w.字段1 from 表名 w where id=?
五、查看SQL使用的索引扫描类型
方法一
explain plan for select count(*) from table_name where to_char(created_date,'yyyy-mm-dd') > '2010-01-01' and to_char(created_date,'yyyy-mm-dd') < '2010-01-11'; select * from table(DBMS_XPLAN.display);
方法二:使用 Explain plan Window 窗口执行 SQL 脚本
*注:个人笔记