zoukankan      html  css  js  c++  java
  • 【oracle11g ,19】索引管理

    一.索引的分类:
    1.逻辑上分为:
     单列索引和复合索引
     唯一索引和非唯一索引
     函数索引
    domain索引
    2.物理上分:
     分区索引和非分区索引
    b-tree 
    bitmap

    注意:表和索引最好不放在同一表空间。
    二.domain索引:(了解)

    一般的索引 %MI%'是不走的索引的,但有可能走域索引。
    域索引用于文本的检索。适合数据仓库。

    SQL> select * from scott.emp where ename  like '%MI%';    

         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK           7902 17-DEC-80        800                    20
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

    SQL> select * from scott.emp where ename  like 'MI%';

         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10


    三.b-tree和bitmap 索引:
    1.b-tree索引是默认的索引。
    #创建索引表空间 (uniform size:能够降低碎片)
    SQL> create tablespace indx datafile '/u01/app/oracle/oradata/PROD/disk4/indx01.dbf' size  50m autoextend on next 10m maxsize 500m uniform size 1m;

    Tablespace created.


    2.位图索引

    四.b-tree 和 bitmap的差别:
    1.b-tree索引使用场景:
    基数比較大(在一个大表上)
    建立在反复值比較少的列上 ,在做select查询时,返回记录的行数小于所有记录的4%,
    由于索引是有序的。所以能够在排序字段上建立索引。
    update 较多。
    oltp使用

    2.bitmap 索引使用场景:  (在生产环境中不使用)
    基数比較小
    建立在反复值很高的列上
    在做DML时代价高,所以在update较少的列上建立bitmap索引。
    一般使用在altp。


    bitmap缺点:当对一个有位图索引的数据表进行dml(包含insert)操作的时候。oracle会因为bitmap index 锁定过多的数据行。



    3.案例: 性别列上建立索引
    SQL> create table lxtb(id number(8),gender varchar2(2),name varchar2(30));

    SQL> declare
      2   v_num number(2);
      3  begin
      4   for i in 1..20000 loop
      5     v_num:=round(dbms_random.value(0,1),0);
      6     if v_num>0 then
      7       insert into lxtb values(i,'M','male'||i);
      8     else
      9       insert into lxtb values(i,'F','female'||i);
     10     end if;
     11     if mod(i,1000)=0 then
     12      commit;
     13     end if;
     14   end loop;
     15   commit;
     16  end;
     17  /


    PL/SQL procedure successfully completed.

    SQL> select count(*) from lxtb;

      COUNT(*)
    ----------
         20000

    SQL> select * from lxtb where rownum<=10;

            ID GE NAME
    ---------- -- --------------------------------------------------
             1 M  male1
             2 M  male2
             3 M  male3
             4 M  male4
             5 M  male5
             6 F  female6
             7 M  male7
             8 M  male8
             9 F  female9
            10 M  male10

    10 rows selected.

    SQL> col index_name for a20
    SQL> col index_type for a10
    SQL> select index_name,index_type,table_name,tablespace_name
      2  from dba_indexes where table_name='LXTB';

    no rows selected

    SQL> col column_name for a10
    SQL> select index_name,table_name,column_name from dba_ind_columns where table_name='LXTB';

    no rows selected
    #创建b-tree索引 (默认索引)
    SQL> create index i_gender on lxtb(gender) tablespace indx;

    Index created.
    #BLEVEL=1 表示b-tree为两层,LEAF_BLOCKS 表示页块数。
    SQL> select index_name,index_type,table_name,tablespace_name,blevel,leaf_blocks
      2  from dba_indexes where table_name='LXTB';

    INDEX_NAME           INDEX_TYPE TABLE_NAME TABLESPACE     BLEVEL LEAF_BLOCKS
    -------------------- ---------- ---------- ---------- ---------- -----------
    I_GENDER             NORMAL     LXTB       INDX                1          37

    SQL> drop index i_gender;

    Index dropped.
    #创建位图索引:
    SQL> create bitmap index i_gender on lxtb(gender) tablespace indx;

    Index created.

    SQL> col index_name for a20   
    SQL> col index_type for a10

    SQL> select index_name,index_type,table_name,tablespace_name,blevel,leaf_blocks
      2  from dba_indexes where table_name='LXTB';

    INDEX_NAME           INDEX_TYPE TABLE_NAME TABLESPACE     BLEVEL LEAF_BLOCKS
    -------------------- ---------- ---------- ---------- ---------- -----------
    I_GENDER             BITMAP     LXTB       INDX                0           1

    五.索引的管理操作:

    1.分析索引的命令:收集统计信息

    SQL> analyze index i_gender validate structure;

    Index analyzed.

    SQL> exec DBMS_STATS.GATHER_INDEX_STATS('SYS','I_GENDER');

    PL/SQL procedure successfully completed.

    2.对索引碎片的整理: 一般碎片整理不彻底。要重建索引。

    SQL> alter index i_gender coalesce;
    Index altered.


    3.将索引迁移到其它表空间:

    SQL> select index_name,index_type,table_name,tablespace_name,blevel,leaf_blocks
      2  from dba_indexes where table_name='LXTB';

    INDEX_NAME           INDEX_TYPE TABLE_NAME TABLESPACE     BLEVEL LEAF_BLOCKS
    -------------------- ---------- ---------- ---------- ---------- -----------
    I_GENDER             NORMAL     LXTB       INDX                1          37

    #迁移到其它表空间
    SQL> alter index i_gender rebuild tablespace users nologging online;

    Index altered.

    SQL> col index_type for a10
    SQL> select index_name,index_type,table_name,tablespace_name,blevel,leaf_blocks
      2  from dba_indexes where table_name='LXTB';

    INDEX_NAME           INDEX_TYPE TABLE_NAME TABLESPACE     BLEVEL LEAF_BLOCKS
    -------------------- ---------- ---------- ---------- ---------- -----------
    I_GENDER             NORMAL     LXTB       USERS               1          37


    4.监控索引: 查看查询是否走索引。

    SQL> select * from v$object_usage where index_name='I_GENDER';

    no rows selected
    #打开监控
    SQL> alter index i_gender monitoring usage;

    Index altered.
    MON:yes表示监控。no:表示未监控
    #use= NO表示查询没有走索引,use=yes表示查询走索引。
    SQL> select * from v$object_usage where index_name='I_GENDER';

    INDEX_NAME           TABLE_NAME MON USE START_MONITORING    END_MONITORING
    -------------------- ---------- --- --- ------------------- -------------------
    I_GENDER             LXTB       YES NO  02/10/2014 18:39:27

    #关闭监控
    SQL> alter index i_gender nomonitoring usage;

    Index altered.

    SQL> select * from v$object_usage where index_name='I_GENDER';

    INDEX_NAME           TABLE_NAME MON USE START_MONITORING    END_MONITORING
    -------------------- ---------- --- --- ------------------- -------------------
    I_GENDER             LXTB       NO  YES 02/10/2014 18:39:27 02/10/2014 18:41:43



    六.创建和重建索引:(重点)
    1.注意:在生成库上重建或创建索引,对索引的一切操作,一定要使用nologging online,
    nologging :少计日志,提高效率。
    online:不堵塞dml操作
    #创建索引
    SQL> create index i_gender on lxtb(gender) tablespace indx nologging online;

    Index created.
    #重建索引
    alter index xxx rebuild online;

    2.rebuild 和 rebuild online 差别:






    七.函数索引:

       (略) 详见:【sql,11】视图、序列、索引、同义词、权限和角色的管理



    八.反向索引:


    在生成库上不建议使用。

    #创建反向索引:
    SQL> create index i_id on lxtb(id) reverse tablespace indx;

    Index created.

    SQL> col index_name for a20
    SQL> col index_type for a10
    SQL> select index_name,index_type,table_name,tablespace_name,blevel,leaf_blocks
      2  from dba_indexes where table_name='LXTB';

    INDEX_NAME           INDEX_TYPE TABLE_NAME TABLESPACE     BLEVEL LEAF_BLOCKS
    -------------------- ---------- ---------- ---------- ---------- -----------
    I_NAME               NORMAL     LXTB       INDX                1          60
    I_GENDER             NORMAL     LXTB       USERS               1          37
    I_UPPER              FUNCTION-B LXTB       INDX                1          60
                         ASED NORMA
                         L

    I_ID                 NORMAL/REV LXTB       INDX                1          44


    八.HASH索引:(一般不使用)
    使用hash算法分散值。

    与反向索引相似,范围查询效率极低。

    #创建hash索引
    SQL> create index i_id on lxtb hash(id) tablespace indx; 

    Index created.

    九.复合索引:


    详见:【sql,11】视图、序列、索引、同义词、权限和角色的管理



    十.查询索引:
    SQL> select index_name,index_type,table_name,tablespace_name,blevel,leaf_blocks,buffer_pool
      2  from dba_indexes where table_name='LXTB';

    INDEX_NAME           INDEX_TYPE TABLE_NAME TABLESPACE     BLEVEL LEAF_BLOCKS BUFFER_
    -------------------- ---------- ---------- ---------- ---------- ----------- -------
    I_NAME               NORMAL     LXTB       INDX                1          60 DEFAULT
    I_GENDER             NORMAL     LXTB       USERS               1          37 DEFAULT
    I_UPPER              FUNCTION-B LXTB       INDX                1          60 DEFAULT
                         ASED NORMA
                         L

    I_ID                 NORMAL/REV LXTB       INDX                1          44 DEFAULT

    #切换缓存池
    SQL> alter index scott.pk_emp storage(buffer_pool keep);

    Index altered.

    SQL> col index_name for a20
    SQL> col index_type for a10
    SQL> select index_name,index_type,table_name,tablespace_name,blevel,leaf_blocks,buffer_pool
      2  from dba_indexes where table_name='LXTB';

    INDEX_NAME           INDEX_TYPE TABLE_NAME TABLESPACE     BLEVEL LEAF_BLOCKS BUFFER_
    -------------------- ---------- ---------- ---------- ---------- ----------- -------
    I_NAME               NORMAL     LXTB       INDX                1          60 DEFAULT
    I_GENDER             NORMAL     LXTB       USERS               1          37 DEFAULT
    I_UPPER              FUNCTION-B LXTB       INDX                1          60 DEFAULT
                         ASED NORMA
                         L

    I_ID                 NORMAL/REV LXTB       INDX                1          44 DEFAULT

    SQL> select object_id,object_name,object_type from dba_objects where owner='SCOTT';

     OBJECT_ID OBJECT_NAME          OBJECT_TYPE
    ---------- -------------------- --------------------
         10184 DEPT                 TABLE
         10185 PK_DEPT              INDEX
         10186 EMP                  TABLE
         10187 PK_EMP               INDEX
         10188 BONUS                TABLE
         10189 SALGRADE             TABLE

    6 rows selected.

    SQL> select segment_name,segment_type,tablespace_name,bytes/1024 k,extents,blocks 
      2  from dba_segments where owner='SCOTT';

    SEGMENT_NA SEGMENT_TY TABLESPACE          K    EXTENTS     BLOCKS
    ---------- ---------- ---------- ---------- ---------- ----------
    DEPT       TABLE      USERS              64          1          8
    PK_DEPT    INDEX      USERS              64          1          8
    EMP        TABLE      USERS              64          1          8
    PK_EMP     INDEX      USERS              64          1          8
    BONUS      TABLE      USERS              64          1          8
    SALGRADE   TABLE      USERS              64          1          8

    SQL> select constraint_name,table_name,column_name 
      2  from dba_cons_columns where owner='SCOTT';

    CONSTRAINT TABLE_NAME COLUMN_NAM
    ---------- ---------- ----------
    PK_DEPT    DEPT       DEPTNO
    PK_EMP     EMP        EMPNO
    FK_DEPTNO  EMP        DEPTNO


    十一.设置index 为invisible.


    An invisible index is an index that is ignored by the optimizer unless you explicitly set the OPTIMIZER_USE_INVISIBLE_INDEXES initialization parameter to TRUE at the session or system level.

    To create an invisible index: 

    • Use the CREATE INDEX statement with the INVISIBLE keyword.

      The following statement creates an invisible index named emp_ename for the ename column of the emp table:

      CREATE INDEX emp_ename ON emp(ename)
            TABLESPACE users
            STORAGE (INITIAL 20K
            NEXT 20k) INVISIBLE;

    隐藏索引
    scott@TESTDB> create index emp_ename_i on emp(ename) invisible;
     
    Index created.
     
     
    scott@TESTDB> select index_name,VISIBILITY from user_indexes; 
     
    INDEX_NAME           VISIBILIT
    -------------------- ---------
    PK_EMP               VISIBLE
    EMP_SAL_F            VISIBLE
    EMP_COMM_I           VISIBLE
    EMP_ENAME_I          INVISIBLE
    PK_DEPT              VISIBLE
     
    scott@TESTDB> select * from emp where ename='KING';
     
     
    没有走索引
     
    切换到系统用户,改动參数
    sys@TESTDB> alter session set optimizer_use_invisible_indexes=true;
     
    Session altered.
    sys@TESTDB> select * from scott.emp where ename='KING';
     
     
     
    隐藏索引变正常索引或反之
    sys@TESTDB> alter index scott.emp_ename_i visible;
     
    Index altered.
     
    scott@TESTDB>  select index_name,VISIBILITY from user_indexes;
     
    INDEX_NAME                     VISIBILIT
    ------------------------------ ---------
    PK_EMP                         VISIBLE
    EMP_SAL_F                      VISIBLE
    EMP_COMM_I                     VISIBLE
    EMP_ENAME_I                    VISIBLE
    PK_DEPT                        VISIBLE
     
     
    多个索引,把慢的索引隐藏点,让他走快的索引
     
     
    scott@TESTDB> alter index emp_ename_i visible;
     
    Index altered.
     
    scott@TESTDB> alter index emp_ename_i invisible;
     
    Index altered.


     








  • 相关阅读:
    网路连接出现意外错误
    linux install oracle jdk
    Chrome开发者工具详解(一)之使用断点来调试代码上
    jQuery对象与DOM对象的相互转换
    Velocity学习(二)之语法
    vscode学习(一)之vscode开发中绝对让你惊艳的插件!!!(个人在用) 持续更新。。。。
    css3新属性box-orient
    使用css控制文字显示几行并且剩余部分隐藏(移动端和PC端同样适用)
    移动端实现横滑
    移动端实1px细线方法
  • 原文地址:https://www.cnblogs.com/llguanli/p/7339225.html
Copyright © 2011-2022 走看看