The principle of Oracle: Index

    Create an index on the table. The index is for the table, just like the directory is for the book. With the index, you can directly locate the data position in the table, which greatly speeds up the search speed. Indexes can reduce disk IO and are logically and physically independent of table data. Indexes can be stored on any disk. In addition, Oracle will automatically maintain indexes.

 1. Unique index, composite index, reverse key index, function index, bitmap index

    Indexes can be divided into B-tree (B-Tree) indexes and bitmap (Bitmap) indexes. B-tree indexes are further divided into unique indexes, composite indexes, reverse key indexes, and function-based indexes. Those who have learned the data structure know the B-tree, and all the leaf nodes are at the same depth. Each node block can contain keywords: For example, if a node block contains keywords of 100, 200 500, then it has four leaf nodes, and the data ranges are...~100, 100~200, 200~500, 500~.....

The simplest syntax to create an index: CREATE INDEX [index name] ON [table name] (table field name). Table names user_indexes and all_indexes can see index information. Table name user_ind_columns, all_ind_columns view index related information.

Index fragmentation refers to when the data associated with the index is deleted and the index information still exists, then the index of this data is useless index called index fragmentation. Useless information occupies memory, which will affect Oracle performance. One way to reduce fragmentation is to rebuild the index.

Analyze the index statement: ANALYZE INDEX [index name] VALIDATE STRUCTURE; Then query INDEX_STATS to see how many pct_used index fragments are.

First create a table of 1.300000 data. Let's take a look at the effect of the index.

--drop table salary_tbl;

create  table salary_tbl(
   employer_nm varchar(20),
   department varchar(20) not null,
   salary number not null,
   leader_nm varchar(20)
);
truncate table salary_tbl;
begin
 for i in  1..1300000
     loop
     insert into salary_tbl values('雇佣者'||i,'部门'||Mod(i,50),100+sqrt(i),'雇佣者'||Mod(i,20)); 
     if Mod(i,1000)=0 then 
       commit;
     end if;
   end loop;
end; 
/
commit;


Create a new index and query information:

create index idx1 on salary_tbl(DEPARTMENT);
ANALYZE INDEX IDX1 VALIDATE STRUCTURE;     --分析索引
select s.name,s.pct_used,s.blocks from  INDEX_STATS s ;

At this time, there is no index fragmentation, because the specified maximum value of PCT_USED is 90.

After deleting the data, analyze the index and check the index information:

The utilization rate of PCT_USED at this time dropped, which resulted in index fragmentation. One way to increase the usage rate is to rebuild the index:

ALTER INDEX IDX1 REBUILD;  -- ALTER INDEX [索引名] REBUILD
ANALYZE INDEX IDX1 VALIDATE STRUCTURE;
select s.name,s.pct_used,s.blocks from  INDEX_STATS s ;

After the index was rebuilt, the usage rate went up again.

Unique index: Ensure that there are no duplicate values ​​on the column associated with the index, which is called a unique index, create unique index [index name] on [table name] (column name); unique means unique and cannot be repeated. The field value associated with the index cannot be repeated, otherwise an error will be reported, but the associated field allows multiple null values.

 

Composite index: An index can be associated with multiple fields, and an index associated with multiple fields is called a composite index. create index [index name] on [table name] (column name 1, column name 2...), the statement that can speed up the query is select * from [table] where column name 1 = .. and column name 2.. . Such statements.

 

Reverse key index: In order to evenly distribute data to each index leaf node, Oracle provides a reverse key index to avoid the problem of too much data on some leaf nodes and too small for some leaf nodes. The key value is sorted in reverse order. Compared with the general index, if the key values ​​are consecutive, it means that they are all placed in the same data block. When two adjacent key values ​​are to be accessed, it will cause the same one to be grabbed at the same time. data block. In the case of reverse key value, two adjacent key values ​​will not be placed in the same data block, and there will be no mutual grabbing, but since the relationship between key values ​​is gone, the leaf will soon not have the function of two-way connection. Therefore, when the SQL syntax condition is =, it can have the greatest effect.

create index [index name] on [table name] (column name) REVERES;

For example, the associated field value is 1001 1002, 3211, 3212, 3213, then the reverse key value is 1001 2001 1123 2123 3123, use this value to build the index.

 

Function index: The associated key value is the value calculated by the function, in the where function (when the field) = ... can speed up the query. QUERY REWRITE permission is required when creating, and it cannot be created on a LOB type column

      create index [index name] on [table name] (function name (parameter));

 

Bitmap index (Bitmap index): Bitmap index does not belong to B-Tree index, there is a big difference between the two. The B-Tree index key value is followed by ROWID, and the BitMap index, the key value is followed by a string of bits (String of bit), which is a string of '0' and '1'. Bitmap index can effectively save index space, it is suitable for creating on low cardinality column, the so-called low cardinality column refers to the field with very few values. For example, gender=('male','female'); grade=('1','2','3') like this.

     create bitmap index [index name] on [table name] (column name);

    

B-Tree index Bitmap index
Suitable when there are many index key values, such as student ID, ID card, etc. Suitable for fields with relatively few index key values, such as gender, blood type, grade, etc.
Can often perform modification operations The cost of performing modifications is higher
Very inefficient for'or' expressions SQL syntax suitable for or expressions 
Suitable for online trading system (OTP), a system that changes frequently Applicable to data warehouses, systems with large amounts of data but infrequent changes

 

Rebuild index complete syntax: ALTER INDEX index_name REBULID [Online] [NOLOGING] [COMPUTE STATISTICS]

Online: Rebuild the index online. In the process of rebuilding the index, other users are allowed to add, delete and modify. If the number of indexes to be created is too large, the time to build the index will be longer. Under normal circumstances, it is forbidden to do this during this time. Additions, deletions and changes to the index may affect the business. This can be avoided by setting it to Online.

NOLOGGING: Represents the least redo entry Redo Entry during the reconstruction process.

COMPUTE STATISTICS: The statistical information required by the optimizer is generated during the reconstruction process, there is no need to manually analyze or dbms_stats after reconstruction.

 

2. Index partition:

       There are three types of index partitions: local partition index, global partition index, and global non-partition index. The partition index can be searched in the user_indexes and user_ind_partitions tables.

Local partition index: The index created on the partition table, the partition range of the index is the same as that of the table. create index [index name] on [table name] (field) local; How the table is partitioned, the local partition index is partitioned.

Global partition index: create index [index name] on [table name] (field) global Partition by .......: Manually customize the partition for the index. The partition status of the table has nothing to do with the index partition, and the partition of the index is customized.

Global non-partitioned index: A global ordinary index created on a partitioned table, the index is not partitioned. create index [index name] on [table name] (field) global;

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/104573097