Oracle index knowledge learning notes

A, Oracle index Introduction

Looking at the "harvest, more than SQL optimization," a book, and practice based on examples in the book, organized into notes

1.1 Classification Index

Oracle index is divided into BTree index, a bitmap index, reverse index, function index, full-text indexing and so on.

1.2 index data structure

Oracle is the most commonly used index BTree index, so the index to BTree example, talk about BTree index, index data structure is BTree structure of a binary tree, the root index block (Root), tubers (Branch), the leaf block (leaf), where the leaf block index column primarily stores a specific value (Key column value) and can be positioned to block Rowid particular location, tuber and root block corresponding to the main storage corresponding to the lower index

Here Insert Picture Description

1.3 index feature

Index features:

  • The index itself is orderly
  • The index itself can store the column values

Note the use of 1.4 index points

  • (1), not only the equivalent range queries in sequence without performance IMPACT
drop table t purge; 
create table t as select * from dba objects;
update t set object_id=rownum ;
commit;
create index idx_id_type on t(object_id, object_type) ; 
create index idx_type_id on t(object_type , object_id) ; 
set autotrace off; 
alter session set statistics_level=all ; 
select /*+index(t idx_id_type)*/ * from t where object_id=20 and object_type='TABLE';
select * from table(dbms_xplan.display cursor(null , null , 'allstats last')); 
select /*+index(t,idx_type id)*/ * from t where object_id=20 and object_type= 'TABLE'; 
select * from table(dbms_xplan.display cursor(null , null , 'allstats last')); 
  • (2), the range of the query, the index combination of the preferred sequence generally opposite the front column equivalence query
select /*+index (t, idx_id_type)*/ * from t where object_id>=20 and object_id<2000 and 
object_type='TABLE';

select /*+index (t , idx_type_id) */ * from t where object_id>=20 and object_id<2000 
and object type='TABLE';
  • (3), Oracle can not find the maximum and minimum values ​​of the two root indexes
set autotrace on
select max(object_id) , min(object_id) from t; 

Cartesian product wording:

set autotrace on

select max, min 
from (select max(object_id) max from t ) a , 
(select min(object_id) min from t ) b; 

  • (4), the latest index data blocks are generally in the far right

1.5 shortcomings, the index

  • Fast heat of competition: the latest index data blocks in the far right in general, but also the general access data is accessed relatively new, so easily lead to heat fast competition
  • Update new problem: the index itself is orderly, so the query time soon, but updated when in trouble, but make sure the new update the index to sort

1.6, the index failed

Failure Failure index into logical and physical failure

  • Logical failure
    logic fails because some sql syntax lead to failure of the index, such as adding a number of functions, and the index column is not functional index
  • Physical failures
    physical failure is really fail, such as unusable property is provided, the partition table index can also lead to abnormal operations like the case of failure
alter index index_name unusable;

Second, the index Category Description

Index Category: BTree index, a bitmap index, function index, reverse index, full-text indexing

2.1, bitmap indexes

Bitmap Index: bitmap index is stored bit value

Environmental ready, a bitmap index properties when applied to count, maximum efficiency

drop table t purge;
create table t as select * from dba_objects;
update t set object_id = rownum;
commit;

Not the index case:

SQL> set autotrace on
SQL> select count(*) from t;
  COUNT(*)
----------
     72016
执行计划
----------------------------------------------------------
Plan hash value: 2966233522
-------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |   288   (1)| 00:00:04 |
|   1 |  SORT AGGREGATE    |      |     1 |            |          |
|   2 |   TABLE ACCESS FULL| T    | 86565 |   288   (1)| 00:00:04 |
-------------------------------------------------------------------
Note
-----
   - dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
          4  recursive calls
          0  db block gets
       1111  consistent gets
          0  physical reads
          0  redo size
        432  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL>

Create a bitmap index:

create bitmap index idx_bitm_t_status on t(status);

Query again, go bitmap index query:

SQL> set autotrace on
SQL> select count(*) from t;
  COUNT(*)
----------
     72016
执行计划
----------------------------------------------------------
Plan hash value: 4272013625
--------------------------------------------------------------------------------
-----------
| Id  | Operation                     | Name              | Rows  | Cost (%CPU)|
 Time     |
--------------------------------------------------------------------------------
-----------
|   0 | SELECT STATEMENT              |                   |     1 |     5   (0)|
 00:00:01 |
|   1 |  SORT AGGREGATE               |                   |     1 |            |
          |
|   2 |   BITMAP CONVERSION COUNT     |                   | 86565 |     5   (0)|
 00:00:01 |
|   3 |    BITMAP INDEX FAST FULL SCAN| IDX_BITM_T_STATUS |       |            |
          |
--------------------------------------------------------------------------------
-----------
Note
-----
   - dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          6  consistent gets
          0  physical reads
          0  redo size
        432  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL>

Points to note:

Bitmap index update is likely to cause a deadlock column, the column was so queries are more suitable for building a bitmap index is updated more columns to try not to build index

1.2 Function Index

Function Index: is a function of the calculated results are stored in the row of column

Preparing the environment:

drop table t purge;
create table t (id int, status varchar2(2));
insert into t select 1,'N' from dual;
insert into t select rownum ,'Y' from dual connect by rownum <1000;
commit;

Not taking the index query:

SQL> set autotrace on
SQL> select * from t where (case when status='N' then 'No' end)='No';
        ID STAT
---------- ----
         1 N
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    16 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |     1 |    16 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(CASE "STATUS" WHEN 'N' THEN 'No' END ='No')
Note
-----
   - dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          8  consistent gets
          0  physical reads
          0  redo size
        486  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL>

Creating a functional index:

create index idx_status on t (case when status ='N' then 'No' end);

Go function index query:

SQL> select * from t where (case when status='N' then 'No' end)='No';
        ID STAT
---------- ----
         1 N
执行计划
----------------------------------------------------------
Plan hash value: 3908194542
--------------------------------------------------------------------------------
----------
| Id  | Operation                   | Name       | Rows  | Bytes | Cost (%CPU)|
Time     |
--------------------------------------------------------------------------------
----------
|   0 | SELECT STATEMENT            |            |    10 |   200 |     2   (0)|
00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T          |    10 |   200 |     2   (0)|
00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IDX_STATUS |     4 |       |     1   (0)|
00:00:01 |
--------------------------------------------------------------------------------
----------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access(CASE "STATUS" WHEN 'N' THEN 'No' END ='No')
Note
-----
   - dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          2  consistent gets
          0  physical reads
          0  redo size
        486  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL>

Points to note:

To add custom keywords deterministic function, or can not establish a functional index

Build a custom function:

create or replace function f_addusl(i int) return int is
begin
  return(i + 1);
end;

Try to establish a functional index:

create index idx_ljb_test on t(f_addusl(id));

Tip: ORA-30553: function can not be determined

With deterministic keywords, you can create a functional index

create or replace function f_addusl(i int) return int deterministic is
begin
  return(i + 1);
end;

When custom code update function, the function corresponding to the index will also rebuild, or can not use the original function index

1.3, inverted index

Reverse Index: inverted index is actually a special case BTree index, but in the column byte will be reversed (reverse index is to avoid fast heat of competition, such as the index column values ​​stored in the column are increasing, such as 250101,250102, according to the characteristics BTree index, typically in the order index stored in the right side, it is easy to form the heat fast competition, and the reverse index avoid this situation, because the inverted index is stored, such as 101052,201052 so that the column value is very far away, and avoid the fast heat of competition)

Inverted index range query can not be used

SQL> set autotrace on
SQL> select * from t where created=sysdate;
未选定行
执行计划
----------------------------------------------------------
Plan hash value: 913247507
--------------------------------------------------------------------------------
---------------
| Id  | Operation                   | Name            | Rows  | Bytes | Cost (%C
PU)| Time     |
--------------------------------------------------------------------------------
---------------
|   0 | SELECT STATEMENT            |                 |    12 |  2484 |   286
(0)| 00:00:04 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T               |    12 |  2484 |   286
(0)| 00:00:04 |
|*  2 |   INDEX RANGE SCAN          | IDX_REV_CREATED |   346 |       |     1
(0)| 00:00:01 |
--------------------------------------------------------------------------------
---------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("CREATED"=SYSDATE@!)
Note
-----
   - dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          2  consistent gets
          0  physical reads
          0  redo size
       1191  bytes sent via SQL*Net to client
        408  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed
SQL>

Range queries, do not take the reverse index query found

SQL> select * from t where created>= sysdate-10 and created <= sysdate-1;
OWNER
------------------------------------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SUBOBJECT_NAME                                                OBJECT_ID
------------------------------------------------------------ ----------
DATA_OBJECT_ID OBJECT_TYPE                            CREATED
-------------- -------------------------------------- --------------
LAST_DDL_TIME  TIMESTAMP                              STATUS         TE GE SE
-------------- -------------------------------------- -------------- -- -- --
 NAMESPACE EDITION_NAME
---------- ------------------------------------------------------------
SYS
ICOL$
                                                                     20
             2 TABLE                                  15-6月 -19
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
         1
SYS
I_USER1
                                                                     46
            46 INDEX                                  14-6月 -19
OWNER
------------------------------------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SUBOBJECT_NAME                                                OBJECT_ID
------------------------------------------------------------ ----------
DATA_OBJECT_ID OBJECT_TYPE                            CREATED
-------------- -------------------------------------- --------------
LAST_DDL_TIME  TIMESTAMP                              STATUS         TE GE SE
-------------- -------------------------------------- -------------- -- -- --
 NAMESPACE EDITION_NAME
---------- ------------------------------------------------------------
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
         4
SYS
CON$
                                                                     28
            28 TABLE                                  13-6月 -19
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
         1
SYS
OWNER
------------------------------------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SUBOBJECT_NAME                                                OBJECT_ID
------------------------------------------------------------ ----------
DATA_OBJECT_ID OBJECT_TYPE                            CREATED
-------------- -------------------------------------- --------------
LAST_DDL_TIME  TIMESTAMP                              STATUS         TE GE SE
-------------- -------------------------------------- -------------- -- -- --
 NAMESPACE EDITION_NAME
---------- ------------------------------------------------------------
UNDO$
                                                                     15
            15 TABLE                                  12-6月 -19
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
         1
SYS
C_COBJ#
                                                                     29
            29 CLUSTER                                11-6月 -19
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
OWNER
------------------------------------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SUBOBJECT_NAME                                                OBJECT_ID
------------------------------------------------------------ ----------
DATA_OBJECT_ID OBJECT_TYPE                            CREATED
-------------- -------------------------------------- --------------
LAST_DDL_TIME  TIMESTAMP                              STATUS         TE GE SE
-------------- -------------------------------------- -------------- -- -- --
 NAMESPACE EDITION_NAME
---------- ------------------------------------------------------------
         5
SYS
I_OBJ#
                                                                      3
             3 INDEX                                  10-6月 -19
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
         4
SYS
PROXY_ROLE_DATA$
OWNER
------------------------------------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SUBOBJECT_NAME                                                OBJECT_ID
------------------------------------------------------------ ----------
DATA_OBJECT_ID OBJECT_TYPE                            CREATED
-------------- -------------------------------------- --------------
LAST_DDL_TIME  TIMESTAMP                              STATUS         TE GE SE
-------------- -------------------------------------- -------------- -- -- --
 NAMESPACE EDITION_NAME
---------- ------------------------------------------------------------
                                                                     25
            25 TABLE                                  09-6月 -19
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
         1
SYS
I_IND1
                                                                     41
            41 INDEX                                  08-6月 -19
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
         4
OWNER
------------------------------------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SUBOBJECT_NAME                                                OBJECT_ID
------------------------------------------------------------ ----------
DATA_OBJECT_ID OBJECT_TYPE                            CREATED
-------------- -------------------------------------- --------------
LAST_DDL_TIME  TIMESTAMP                              STATUS         TE GE SE
-------------- -------------------------------------- -------------- -- -- --
 NAMESPACE EDITION_NAME
---------- ------------------------------------------------------------
SYS
I_CDEF2
                                                                     54
            54 INDEX                                  07-6月 -19
02-4月 -10     2010-04-02:13:18:38                    VALID          N  N  N
         4
已选择9行。
执行计划
----------------------------------------------------------
Plan hash value: 1322348184
---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |    12 |  2484 |   292   (2)| 00:00:04 |
|*  1 |  FILTER            |      |       |       |            |          |
|*  2 |   TABLE ACCESS FULL| T    |    12 |  2484 |   292   (2)| 00:00:04 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(SYSDATE@!-10<=SYSDATE@!-1)
   2 - filter("CREATED">=SYSDATE@!-10 AND "CREATED"<=SYSDATE@!-1)
Note
-----
   - dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
          5  recursive calls
          0  db block gets
       1112  consistent gets
          0  physical reads
          0  redo size
       1770  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed
SQL>

1.4, full-text indexing

Full-text index: the so-called full-text index Oracle by Oracle lexical analyzer (lexer) The term memory dr $ beginning table of all ideographic term storage unit to appear, the number, hash value information and so on, Oracle provides basic_lexer (for English), chinese_vgram_lexer (Chinese analyzer), chinese_lexer (new Chinese analyzer).

  • basic_lexer: a parser for English, according to the word spaces or punctuation cell separation, whether for the Chinese is no space, so this analyzer is not suitable for Chinese
  • chinese_vgram_lexer: This is a special Chinese original parser, support for all Chinese character set, such as zhs16gbk single point. This analyzer to analyze the process is analyzed by means of a word, for example, "the index itself is ordered", according to this analyzer, will be divided into lexical unit "cable", "Index", "primer present "" per se "," the body is, "" there "," order "," order "and" in "these words yuan, and then you find like" sequence "of these words in Chinese are basically not true , but this in itself does not know Chinese Oracle analyzer, it can only all analysis, it is clear that efficiency is not good
  • chinese_lexer: This is a new Chinese analyzer, this analyzer mentioned chinese_vgram_lexer although support for all Chinese character set, but the efficiency is not high, so chinese_lexer is an improved version of it, this analyzer know a lot of Chinese vocabulary can query faster, more efficient, but this analyzer can only support utf-8 character set

Oracle full-text indexing concrete can be used to find a wildcard, fuzzy matching, related to classification, approximate search, word meaning and conditions of the weighted expansion and other methods

Preparing the Environment

drop table t purge;
create table t as select * from dba_objects where object_name is not null;
update t set object_name ='高兴' where rownum<=2;
commit;
select * from t where object_name like '%高兴%';

Setting the lexical analyzer

//设置词法分析器
BEGIN
  ctx_ddl.create_preference ('lexer1', 'chinese_vgram_lexer');
END;

Unlock ctxsys user, while giving you a test account (I use scott) authorized the use of ctx_ddl

//解锁ctxsys用户同时授权
grant ctxapp to scott;
alter user ctxsys account unlock;
alter user ctxsys identified by ctxsys;
connect ctxsys/ctxsys;
grant execute on ctx_ddl to scott;
connect scott/11;

Full-text indexed

//删除全文索引
drop index idx_content;
//查看数据文件信息
select * from v$datafile; 
//建立全文索引
CREATE INDEX  idx_content ON t(object_name) indextype is ctxsys.context  parameters('lexer lexer1');

Points to note: Always try to update the data synchronization commands to perform full-text indexing, or they will not see the update data



exec ctx_ddl.sync_index('idx_content','20M');

Guess you like

Origin blog.csdn.net/u014427391/article/details/90706124