[Posts] B-tree indexes, hash indexes and bitmap index

B-tree indexes, hash indexes and bitmap index

The index data structure may be divided into three B-tree indexes, hash indexes and bitmap index

 

B-tree index

 

structure:




 

 
 

 

 

Features:

 

1. The index does not store a null value.

 

   More precisely, a separate index does not store null values, the composite index is not stored whole null value

 

    Null when the index can not be stored, so the use of this column is null condition, because no Null value on the index, you can not use the index, only

 

You can be a full table scan.

 

   Why had not the index column Null value it? The contribution index column values, wherein the comparison operation necessarily involves many. Null value

 

Particularity lies mostly involved in the operation value is null. In this case, null values ​​are actually built into the index can not participate

 

process. In other words, null values ​​are not the same as other value appears on the index tree leaf node.

 

B-tree index Test 1: if there is a NULL index.

 

 

create table btree_test(id number,code varchar2(10));

create index idx_btree_test_id on btree_test(id,code);

select object_id from user_objects where object_name='IDX_BTREE_TEST_ID';

alter session set events 'immediate trace name treedump level 59097';

insert into btree_test values(null,null);

alter session set events 'immediate trace name treedump level 59097';

insert into btree_test values(null,'1');

alter session set events 'immediate trace name treedump level 59097';

insert into btree_test values(1,null);

alter session set events 'immediate trace name treedump level 59097';

 

 

And then view the dump file, admin \ database name \ udump

 

I found this information:

 

*** 2013-07-19 14:56:41.827

----- begin tree dump

leaf: 0x140142c 20976684 (0: nrow: 0 rrow: 0)

----- end tree dump

*** 2013-07-19 14:56:54.480

----- begin tree dump

leaf: 0x140142c 20976684 (0: nrow: 1 rrow: 1)

----- end tree dump

*** 2013-07-19 14:57:08.139

----- begin tree dump

leaf: 0x140142c 20976684 (0: nrow: 2 rrow: 2)

----- end tree dump

 

Nrow number of index entries included in the current node (including delete of entry)

 

Rrow effective amount of index entries

 

it can be discovered:

 

When inserting null, null, 0 is the effective index entry

Inserting null, 1, 1 is the effective index entry

When inserting 1, null, effective index entry 2

 

Therefore, the composite index only when you want to insert the full value is Null can not be put into the index.

 

 You can also look like this:

 

 SELECT num_rows  FROM user_indexes t   WHERE t.index_name ='btree_test';

 

 

2. is not suitable for small key column (column more duplicate data).

 

     If there are five indexed columns TYPE keys, if there are 10,000 data, WHERE TYPE = 2000 access a block of data in the table.

 

Coupled with access index block, the data block to be accessed is greater than the total of 200 in.

 

    If the full table scan, assuming a block of data 10, then only access the data block 1000, the data block is now full table scan access

 

Less, certainly we will not use the index.

 

3. Fuzzy queries can not use the preamble index (like '% XX' or like '% XX%')

 

   If there is such a code value of 'AAA', 'AAB', 'BAA', 'BAB', if where code like '% AB' conditions, since the front

 

Blurred, so you can not use the index of order, you must go one by one to see if conditions are met. This will result in a full scan or a full table scan index

 

Description. If this is the condition where code like 'A%', the CODE CODE can find the beginning position A, when it comes to the beginning of B

 

When the data, you can stop searching, because the back of the data does not meet certain requirements. So that you can take advantage of the index.

 

 

Bitmap Index

 

 

    It is to use a bitmap representation of the index, the establishment of a bitmap for each key column.

 

    The test table has such a state, the following data:

 

        10    20    30    20    10    30    10    30    20    30

 

Then creates three bitmap, as follows:

 

BLOCK1    KEY=10  1    0    0    0    1    0    1    0    0    0   

BLOCK2    KEY=20  1    0    0    0    1    0    1    0    0    0 

BLOCK3    KEY=30  1    0    0    0    1    0    1    0    0    0 

 

Bitmap index features:

 

 

1. relative to the B * Tree index, taking up very little space, create and use very quickly.

 

    Since the start-stop Rowid bitmap indexes and bitmap stores only keys, taking up very little space.

 

 

2. not suitable for many key columns.

 

3. unsuitable update, insert, delete columns frequently.

 

4 can store a null value.

 

    B * Tree index due to null values ​​are recorded, is null when the query-based, using a full table scan of the bitmap index column into

 

When the line is null query, you can use the index.

 

5. When the select count (XX), can be accessed directly on a bitmap index statistics obtained quickly.

 

6. When this is done and, or based on a key or in (x, y, ..) query, directly or bitmap index calculation, the number of rows obtain fast results

 

according to.

 

 

Bitmap Test 1: bitmap index query efficiency (omitted).
 
Bitmap Test 2: lock when modifying data range.
 
create table test_bitmap(id number,state number);
insert into test_bitmap values (1,10);
insert into test_bitmap values (2,10);
insert into test_bitmap values (3,20);
insert into test_bitmap values (4,20);
insert into test_bitmap values (5,10);
insert into test_bitmap values (6,30);
insert into test_bitmap values (7,30);
insert into test_bitmap values (8,20);
insert into test_bitmap values (9,30);
insert into test_bitmap values (10,20);
 
 
CREATE BITMAP INDEX INDEX_TESTBITMAP_STATE ON TEST_BITMAP(STATE);
 
PLSQL open a window (SESSION1), execution
update test_bitmap set state = 20 where id = 1;
 
PLSQL open another window (SESSION2), execution
update test_bitmap set state = 20 where id = 2;
or
update test_bitmap set state = 10 where id = 4;
Can be found, the status for all 20 rows are locked.
 

 

 

Hash index

 

The index is based on a hash algorithm HASH to build the index, so fast retrieval, but not the scope of the query.

 

Hash index features

 

 

1. Only for the equivalent query (including = <> and in), or is not suitable for fuzzy range queries

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11840428.html