自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Oracle數(shù)據(jù)庫(kù)的三大索引類型

數(shù)據(jù)庫(kù) Oracle
今天主要介紹Oracle數(shù)據(jù)庫(kù)的三大索引類型,僅供參考。下面,我們一起來看。

今天主要介紹Oracle數(shù)據(jù)庫(kù)的三大索引類型,僅供參考。

詳解Oracle數(shù)據(jù)庫(kù)的三大索引類型

一、B-Tree索引

三大特點(diǎn):高度較低、存儲(chǔ)列值、結(jié)構(gòu)有序

1. 利用索引特性進(jìn)行優(yōu)化

  • 外鍵上建立索引:不但可以提升查詢效率,而且可以有效避免鎖的競(jìng)爭(zhēng)(外鍵所在表delete記錄未提交,主鍵所在表會(huì)被鎖住)。
  • 統(tǒng)計(jì)類查詢SQL:count(), avg(), sum(), max(), min()
  • 排序操作:order by字段建立索引
  • 去重操作:distinct
  • UNION/UNION ALL:union all不需要去重,不需要排序

2. 聯(lián)合索引

應(yīng)用場(chǎng)景一:SQL查詢列很少,建立查詢列的聯(lián)合索引可以有效消除回表,但一般超過3個(gè)字段的聯(lián)合索引都是不合適的.

應(yīng)用場(chǎng)景二:在字段A返回記錄多,在字段B返回記錄多,在字段A,B同時(shí)查詢返回記錄少,比如執(zhí)行下面的查詢,結(jié)果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

聯(lián)合索引的列誰在前?

普遍流行的觀點(diǎn):重復(fù)記錄少的字段放在前面,重復(fù)記錄多的放在后面,其實(shí)這樣的結(jié)論并不準(zhǔn)確。

  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'

結(jié)論:等值查詢情況下,組合索引的列無論哪一列在前,性能都一樣。

范圍查詢:

  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'

結(jié)論:組合索引的列,等值查詢列在前,范圍查詢列在后。 但如果在實(shí)際生產(chǎn)環(huán)境要確定組合索引列誰在前,要綜合考慮所有常用SQL使用索引情況,因?yàn)樗饕^多會(huì)影響入庫(kù)性能。

3. 索引的危害

表上有過多索引主要會(huì)嚴(yán)重影響插入性能;

  • 對(duì)delete操作,刪除少量數(shù)據(jù)索引可以有效快速定位,提升刪除效率,但是如果刪除大量數(shù)據(jù)就會(huì)有負(fù)面影響;
  • 對(duì)update操作類似delete,而且如果更新的是非索引列則無影響。

4. 索引的監(jiān)控

  1. --監(jiān)控 
  2. alter index [index_name] monitoring usage; 
  3. select * from v$object_usage; 
  4. --取消監(jiān)控:  
  5. alter index [index_name] nomonitoring usage; 

根據(jù)對(duì)索引監(jiān)控的結(jié)果,對(duì)長(zhǎng)時(shí)間未使用的索引可以考慮將其刪除。

5. 索引的常見執(zhí)行計(jì)劃

  • INDEX FULL SCAN:索引的全掃描,單塊讀,有序
  • INDEX RANGE SCAN:索引的范圍掃描
  • INDEX FAST FULL SCAN:索引的快速全掃描,多塊讀,無序
  • INDEX FULL SCAN(MIN/MAX):針對(duì)MAX(),MIN()函數(shù)的查詢
  • INDEX SKIP SCAN:查詢條件沒有用到組合索引的第一列,而組合索引的第一列重復(fù)度較高時(shí),可能用到

二、位圖索引

應(yīng)用場(chǎng)景:表的更新操作極少,重復(fù)度很高的列。

優(yōu)勢(shì):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'

三、函數(shù)索引

應(yīng)用場(chǎng)景:不得不對(duì)某一列進(jìn)行函數(shù)運(yùn)算的場(chǎng)景。

利用函數(shù)索引的效率要低于利用普通索引的。

oracle中創(chuàng)建函數(shù)索引即是 你用到了什么函數(shù)就建什么函數(shù)索引,比如substr

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

創(chuàng)建索引的語(yǔ)句就是

  1. create index indexname on table(substr(fileld,0,2)) online nologging ; 
責(zé)任編輯:趙寧寧 來源: 今日頭條
相關(guān)推薦

2009-04-22 14:19:32

Oracle中文索引基礎(chǔ)

2011-03-16 08:54:45

Oracle數(shù)據(jù)庫(kù)索引

2010-03-30 17:40:59

Oracle數(shù)據(jù)庫(kù)

2009-06-11 13:12:59

Oracle索引創(chuàng)建索引

2011-05-19 13:25:14

Oracle數(shù)據(jù)庫(kù)

2010-04-02 13:59:08

Oracle數(shù)據(jù)庫(kù)

2010-04-14 15:14:11

Oracle數(shù)據(jù)庫(kù)

2011-03-29 10:47:49

ORACLE數(shù)據(jù)庫(kù)

2010-05-31 12:10:37

2022-07-25 15:03:13

PandasPython

2019-09-28 22:41:18

OracleMySQL隱式數(shù)據(jù)

2010-04-12 16:35:15

Oracle數(shù)據(jù)庫(kù)

2011-04-11 13:19:41

Oracle數(shù)據(jù)庫(kù)

2023-11-16 17:12:33

數(shù)據(jù)庫(kù)oracle

2011-05-17 15:02:15

ORACLE數(shù)據(jù)庫(kù)備份

2011-08-18 15:49:21

Oracle厲行計(jì)劃

2010-04-20 16:56:19

Oracle數(shù)據(jù)庫(kù)

2010-04-19 13:31:42

Oracle索引

2010-04-07 14:22:46

2011-08-11 16:55:34

Oracle數(shù)據(jù)庫(kù)AWR
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)