详解Oracle数据库的三大索引类型

数据库 Oracle
今天主要介绍Oracle数据库的三大索引类型,仅供参考。下面,我们一起来看。

今天主要介绍Oracle数据库的三大索引类型,仅供参考。

详解Oracle数据库的三大索引类型

一、B-Tree索引

三大特点:高度较低、存储列值、结构有序

1. 利用索引特性进行优化

  • 外键上建立索引:不但可以提升查询效率,而且可以有效避免锁的竞争(外键所在表delete记录未提交,主键所在表会被锁住)。
  • 统计类查询SQL:count(), avg(), sum(), max(), min()
  • 排序操作:order by字段建立索引
  • 去重操作:distinct
  • UNION/UNION ALL:union all不需要去重,不需要排序

2. 联合索引

应用场景一:SQL查询列很少,建立查询列的联合索引可以有效消除回表,但一般超过3个字段的联合索引都是不合适的.

应用场景二:在字段A返回记录多,在字段B返回记录多,在字段A,B同时查询返回记录少,比如执行下面的查询,结果c1,c2都很多,c3却很少。

  1. select count(1) c1 from t where A = 1
  2. select count(1) c2 from t where B = 2
  3. select count(1) c3 from t where A = 1 and B = 2

联合索引的列谁在前?

普遍流行的观点:重复记录少的字段放在前面,重复记录多的放在后面,其实这样的结论并不准确。

  1. drop table t purge; 
  2. create table t as select * from dba_objects; 
  3. create index idx1_object_id on t(object_id,object_type); 
  4. create index idx2_object_id on t(object_type,object_id); 

等值查询: 

  1. select * from t where object_id = 20 and object_type = 'TABLE'
  2. select /*+ index(t,idx1_object_id) */ * from t where object_id = 20 and object_type = 'TABLE'
  3. select /*+ index(t,idx2_object_id) */ * from t where object_id = 20 and object_type = 'TABLE'

结论:等值查询情况下,组合索引的列无论哪一列在前,性能都一样。

范围查询:

  1. select * from t where object_id >=20 and object_id < 2000 and object_type = 'TABLE'
  2. select /*+ index(t,idx1_object_id) */ * from t where object_id >=20 and object_id < 2000 and object_type = 'TABLE'
  3. select /*+ index(t,idx2_object_id) */ * from t where object_id >=20 and object_id < 2000 and object_type = 'TABLE'

结论:组合索引的列,等值查询列在前,范围查询列在后。 但如果在实际生产环境要确定组合索引列谁在前,要综合考虑所有常用SQL使用索引情况,因为索引过多会影响入库性能。

3. 索引的危害

表上有过多索引主要会严重影响插入性能;

  • 对delete操作,删除少量数据索引可以有效快速定位,提升删除效率,但是如果删除大量数据就会有负面影响;
  • 对update操作类似delete,而且如果更新的是非索引列则无影响。

4. 索引的监控

  1. --监控 
  2. alter index [index_name] monitoring usage; 
  3. select * from v$object_usage; 
  4. --取消监控:  
  5. alter index [index_name] nomonitoring usage; 

根据对索引监控的结果,对长时间未使用的索引可以考虑将其删除。

5. 索引的常见执行计划

  • INDEX FULL SCAN:索引的全扫描,单块读,有序
  • INDEX RANGE SCAN:索引的范围扫描
  • INDEX FAST FULL SCAN:索引的快速全扫描,多块读,无序
  • INDEX FULL SCAN(MIN/MAX):针对MAX(),MIN()函数的查询
  • INDEX SKIP SCAN:查询条件没有用到组合索引的第一列,而组合索引的第一列重复度较高时,可能用到

二、位图索引

应用场景:表的更新操作极少,重复度很高的列。

优势:count(*) 效率高

  1. create table t( 
  2. name_id, 
  3. gender not null, 
  4. location not null, 
  5. age_range not null, 
  6. data 
  7. )as select  
  8. rownum, 
  9. decode(floor(dbms_random.value(0,2)),0,'M',1,'F') gender, 
  10. ceil(dbms_random.value(0,50)) location, 
  11. decode(floor(dbms_random.value(0,4)),0,'child',1,'young',2,'middle',3,'old') age_range, 
  12. rpad('*',20,'*') data 
  13. from dual connect by rownum <= 100000;  
  1. create index idx_t on t(gender,location,age_range); 
  2. create bitmap index gender_idx on t(gender); 
  3. create bitmap index location_idx on t(location); 
  4. create bitmap index age_range_idx on t(age_range); 
  1. select * from t where gender = 'M' and location in (1,10,30) and age_range = 'child'
  2. select /*+ index(t,idx_t) */* from t where gender = 'M' and location in (1,10,30) and age_range = 'child'

三、函数索引

应用场景:不得不对某一列进行函数运算的场景。

利用函数索引的效率要低于利用普通索引的。

oracle中创建函数索引即是 你用到了什么函数就建什么函数索引,比如substr

  1. select * from table where 11=1 and substr(field,0,2) in ('01') 

创建索引的语句就是

  1. create index indexname on table(substr(fileld,0,2)) online nologging ; 
责任编辑:赵宁宁 来源: 今日头条
相关推荐

2009-04-22 14:19:32

Oracle中文索引基础

2011-03-16 08:54:45

Oracle数据库索引

2010-03-30 17:40:59

Oracle数据库

2009-06-11 13:12:59

Oracle索引创建索引

2010-05-31 12:10:37

2011-05-19 13:25:14

Oracle数据库

2022-07-25 15:03:13

PandasPython

2010-04-02 13:59:08

Oracle数据库

2010-04-14 15:14:11

Oracle数据库

2010-04-12 16:35:15

Oracle数据库

2019-09-28 22:41:18

OracleMySQL隐式数据

2011-03-29 10:47:49

ORACLE数据库

2023-11-16 17:12:33

数据库oracle

2010-04-20 16:56:19

Oracle数据库

2011-04-11 13:19:41

Oracle数据库

2010-04-19 13:31:42

Oracle索引

2011-08-18 15:49:21

Oracle厉行计划

2011-05-17 15:02:15

ORACLE数据库备份

2011-08-11 16:55:34

Oracle数据库AWR

2010-04-07 14:22:46

点赞
收藏

51CTO技术栈公众号