一.索引的分类:
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';
 

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcmxodWE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" style="border:0px none; max-width:100%">

 
没有走索引
 
切换到系统用户,改动參数
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.









posted @ 2017-08-10 14:41 llguanli 阅读(...) 评论(...) 编辑 收藏

【oracle11g ,19】索引管理的更多相关文章

  1. 手动刷新magento的索引管理方法

    当我们网站商品很多的时候,比如有几千件,我们刷新Magento的索引管理(Index Management)经常会失败.那么后台刷新不了,我们还可以通过命令行来刷新. 使用命令行来刷新索引管理会极大降 ...

  2. 转载 SQL Server中索引管理之六大铁律

    转载原地址 http://jingyan.baidu.com/article/48a42057c03bd7a924250429.html 索引是以表列为基础的数据库对象.索引中保存着表中排序的索引列, ...

  3. MySQL 索引管理与执行计划

    1.1 索引的介绍 索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息.如果想按特定职员的姓来查找他或她,则与在表中搜索所有的行相比,索引有助于更快地获取信息. ...

  4. MySQL索引管理

    一.索引介绍 1.什么是索引 1.索引好比一本书的目录,它能让你更快的找到自己想要的内容. 2.让获取的数据更有目的性,从而提高数据库索引数据的性能. 2.索引类型介绍 1.BTREE:B+树索引 2 ...

  5. ElasticSearch权威指南学习(索引管理)

    创建索引 当我们需要确保索引被创建在适当数量的分片上,在索引数据之前设置好分析器和类型映射. 手动创建索引,在请求中加入所有设置和类型映射,如下所示: PUT /my_index { "se ...

  6. elasticsearch系列二:索引详解(快速入门、索引管理、映射详解、索引别名)

    一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...

  7. MySQL学习【第七篇索引管理及执行计划】

    一.索引介绍 1.什么是索引? 索引由如字典,目的就是为了更快寻找到要找的内容. 令搜索查询的数据更有目的性,从而提高数据检索的能力 2.索引类型介绍 1.BTREE: B+树索引 2.HASH: H ...

  8. mongodb数据库索引管理

    1:ensureIndex() 方法 MongoDB使用 ensureIndex() 方法来创建索引. 语法 ensureIndex()方法基本语法格式如下所示: }) 语法中 Key 值为你要创建的 ...

  9. Atitit.index manager api design 索引管理api设计

    Atitit.index manager api design 索引管理api设计 1. kw 1 1.1. 索引类型 unique,normal,fulltxt 1 1.2. 聚集索引(cluste ...

  10. Atitit.index manager api design 索引管理api设计

    Atitit.index manager api design 索引管理api设计 1. kw1 1.1. 索引类型 unique,normal,fulltxt1 1.2. 聚集索引(clustere ...

随机推荐

  1. codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)

    传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个 ...

  2. JS数组排序技巧汇总(冒泡、sort、快速、希尔等排序)

    本文实例总结了JS数组排序技巧.分享给大家供大家参考,具体如下: 1.冒泡排序 var temp = 0; for (var i = 0; i < array.length; i++) { fo ...

  3. Direct2D 如何关闭抗锯齿

    // Each pixel is rendered if its pixel center is contained by the geometry. // D2D1_ANTIALIAS_MODE_A ...

  4. 【2017"百度之星"程序设计大赛 - 初赛(A)】度度熊的01世界

    [链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=775&pid=1006 [题意] 在这里写题意 [题 ...

  5. React组件之间通过Props传值的技巧(小案例,帮助体会理解props、state、受控组件和非受控组件等)

    本文重要是根据react小书上的一个很简单的例子改编的,加上自己的学习理解,希望可以通过实际案例让大家对概念有更清晰的理解,当然也希望能一块学习. import React,{Component} f ...

  6. query中prop()方法和attr()方法的区别

    query1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值. 官方例举的例子感觉和attr()差不多,也不知道有什么区别,既然有了prop ...

  7. JSP学习 —— 开篇:JSP,servlet容器,Tomcat,servlet容器之间的关系

    JSP(JAVA SERVER PAGE)的缩写,其本身就是servlet的简化,是一种动态网页标准,其特点是在HTML代码中嵌入JAVA代码,JSP标签或用户标签来生成网页.至于它为什么会出现,主要 ...

  8. 洛谷 P2430 严酷的训练

    P2430 严酷的训练 题目背景 Lj的朋友WKY是一名神奇的少年,在同龄人之中有着极高的地位... 题目描述 他的老师老王对他的程序水平赞叹不已,于是下决心培养这名小子. 老王的训练方式很奇怪,他会 ...

  9. 关于python的二维数组

    test =[ [1, 2, 3], [4, 5, 6], [7, 8, 9]]   #这个就可以看做是二维数组了,直接创建print(test)print(test[:][1])           ...

  10. 学习笔记:mpvue开发小程序——入门

    接下来可能要开发一个小程序,同事推荐使用mpvue,那么我提前熟悉下. 官网地址:http://mpvue.com/ 1.快速上手 http://mpvue.com/mpvue/quickstart/ ...