Oracle index details (index)

1 Concept

1. 索引具有两个功能:
	(1) 强制实施 "主键约束""唯一性约束"
	(2) 提高性能
	
2. 提示:
	(1) 对于使用 where 子句的 selectupdatedeletemerge 语句而言,
	    索引可以起到辅助作用
	(2) 但对于 insert 而言,索引会降低处理速度。
	    原因:每次在表中插入一行时,必须在表的每个索引中插入一个新键。

2 Index type

2.1 B-Tree Index

1. B-Tree 索引(B 代表 "平衡(balanced)") 是一种树结构 -- 不是 "二叉树" 哦
2. 树的 '根节点' 指向 '第二级别' 的多个节点,'第二级别' 的节点又指向 '第三级别' 的多个节点,以此类推。
3. B-Tree 索引是 Oracle 默认的索引类型

B-Tree index applicable scenarios:

1. 列的基数很大(不同值的个数很多)
2. 一般认为,如果查询要检索 "低于" 2%~4% 的行,'B-Tree 索引' 合适,
	        如果查询要检测 "高于" 2%~4% 的行,则 '全表扫描' 更快

Principle diagram: Demonstration of the process of reading the data block.
Insert picture description hereCase study: The process of locating the value 82

  1. Read the root node, judge that 82 is between 0-120, and take the left branch
  2. Read the left branch node, judge that 82 is between 80-120, go to the right branch
  3. Read the leat node on the right, find the data 82 and the corresponding rowid in the node
  4. Use rowid to read the record database block in the physical table (if it is count or select rowid only, you do not need to read this time)

In the above case:
1. In the entire index positioning process, the data block is read only 3 times. Only 三次I/Oafter positioning to rowid.
2. During the entire expansion process, B-Tree itself can always maintain a balance, and the depth of Leaf nodes can always be consistent. (Therefore “平衡树”)

2.2 Bitmap Index (bitmap)

Applicable scene:

1. 列的基数很小(不同值的个数少)
2. 列用于布尔代数运算(andornot

Principle diagram:

Name Gender ID card (ID_Card) Marital status (Marital)
n1 male 421… unmarried
n2 Female 422… married
n3 Female 421… unmarried
n4 male 422… divorce
n5 Female 422… unmarried
。。。 。。。 。。。 。。。

Case study: The following queries are available

SELECT * FROM table t WHERE t.Gender = "女" AND t.Marital = "未婚";

Analysis of the usage of full table scan FULL and B-Tree indexes:

1. 不适用索引
	(1) 不使用索引时,数据库只能一行行扫描所有记录,然后判断该记录是否满足查询条件
	(2) 一般情况下,当取出来的数据超过表 2%~4% 时,比较合适

2. B-Tree 索引
	(1) 对于 "性别(Gender)",可取值的范围只有 '男','女',并且男和女可能各站该表的 '50%' 的数据,
	    这时添加 'B-Tree 索引' 还是需要取出一半的数据, 因此完全没有必要。
	(2) 相反,如果某个字段的取值 "范围很广,几乎没有重复",比如 "身份证号",使用 'B-Tree 索引' 比较合适
	(3) 事实上,当取出来的数据超过表 2%~4% 时,即使添加了 'B-Tree 索引', 
	    Oracle 也不一定会使用 'B-Tree 索引',很可能 Oracle 解析器认为 '全表扫描' 更加好

位图索引原理:

  • If the gender (Gender) established in the column 位图索引, for gender (Gender) the column, for each row of rowid(rowid可以理解为每行的物理位置)forming two vectors, a bitmap index,
  • Male: 10010, Female: 01101
  • Among them, 1: means male, 0: means female.
Rowid 1 2 3 4 5
male 1 0 0 1 0
Female 0 1 1 0 1

The same is true: the bitmap index of marital status (Marital) is as follows

Rowid 1 2 3 4 5
married 0 1 0 0 0
unmarried 1 0 1 0 1
divorce 0 0 0 1 0

位图检索过程: (And operation, all 1 gets 1)

Rowid 1 2 3 4 5
Female 0 1 1 0 1
and
unmarried 1 0 1 0 1
result 0 0 1 0 1

3 Grammar

Data preparation: (hint: the primary key comes with a unique constraint index: duplicate values ​​are not allowed)

CREATE TABLE odsdata.student_info (
   student_no VARCHAR2(10),
   NAME       VARCHAR2(30),
   sex        VARCHAR2(2),
   age        NUMBER(3)
);

ALTER TABLE odsdata.student_info ADD CONSTRAINT fk_student_info_student_no PRIMARY KEY(student_no);

Query constraint information:

SELECT t.*
  FROM all_constraints t -- dba_constraints
 WHERE t.owner = 'ODSDATA'
   AND t.table_name = 'STUDENT_INFO';

3.1 Create Index

grammar:

create [unique | bitmap] index [schema.] 索引名
on [schema.] 表名 (列名1, .., 列名N);
-- 创建一般索引(B-Tree 索引)
CREATE INDEX odsdata.idx_student_info_name ON odsdata.student_info (name);

-- 创建位图索引
CREATE BITMAP INDEX odsdata.bidx_student_info_sex ON odsdata.student_info (sex);

3.2 Delete index

drop index [schema.] 索引名;

DROP INDEX odsdata.idx_student_info_name;

3.3 modify index

-- 修改索引名称 idx_student_info_name -> idx_student_info_new_name
ALTER INDEX odsdata.idx_student_info_name RENAME TO odsdata.idx_student_info_new_name;

-- 修改索引为无效
ALTER INDEX odsdata.idx_student_info_name UNUSABLE;

-- 重建索引
ALTER INDEX odsdata.idx_student_info_name REBUILD [ONLINE];

3.4 Query Index

SELECT t.*
  FROM dba_indexes t -- all_indexes, user_indexes
 WHERE t.owner = 'ODSDATA'
   AND t.table_name = 'STUDENT_INFO';

4 extension

How to query the information in the index, such as height?

> ANALYZE INDEX odsdata.idx_student_info_name VALIDATE STRUCTURE;
>
> SELECT t.* FROM index_stats t;

Guess you like

Origin blog.csdn.net/qq_34745941/article/details/83649859