一.索引的分类:
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. python运算符优先级表

    运算符 描述 lambda Lambda表达式 or 布尔“或” and 布尔“与” not x 布尔“非” in,not in 成员测试 is,is not 同一性测试 <,<=,> ...

  2. 【Codeforces Round #452 (Div. 2) D】Shovel Sale

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让N乘2->a 然后看一下位数是多少. 假设有x位(x>=2) 则(0..(a%10-1) ) + (99..9)[x- ...

  3. Vijos——T1279 Leave-绿光

    https://vijos.org/p/1279 背景 期待这一份幸运,和一份冲劲,多么奇妙的际遇…….燕姿在演唱完绿光这首歌后,出给了姿迷一个考题. 北欧有一个传说!人一生中能看见绿光!他就一生都可 ...

  4. (cocos2d-js游戏)測试你的反应速度----------基本逻辑(上)

    游戏玩法:点击開始游戏.等待一个随机时间.然后背景颜色会变(在t1时刻),这时候你须要点击屏幕(在t2时刻),游戏结束.你的反应时间就是天t2-t1. 游戏逻辑: 游戏逻辑非常easy,如上图所看到的 ...

  5. 字串乱序 PHP&JS

    <?php /** * 字串乱序 PHP&JS * * php 中把字串乱序后输出给客户机的 JAVASCRIPT , JAVASCRIPT 中恢复 * 在指定长度提取一个字符,并把这一 ...

  6. 1.1 Introduction中 Kafka as a Storage System官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Storage System kafka作为一个存储系统 An ...

  7. Eclipse Class Decompiler——Java反编译插件手工配置方法

    最近在eclipse上配置了java反编译插件,但是不好用,原因是我的eclipse之前有手动配置过一些类似的java反编译插件,当我将原来的插件完全卸载后重新配置才正常配置上去,自动配置java反编 ...

  8. UVA - 590Always on the run(递推)

    题目:UVA - 590Always on the run(递推) 题目大意:有一个小偷如今在计划着逃跑的路线,可是又想省机票费. 他刚開始在城市1,必须K天都在这N个城市里跑来跑去.最后一天达到城市 ...

  9. 2016最热门的PHP框架(一共五款)

    摘要: 兄弟连IT教育作为全国最大的PHP培训机构,迄今已有10年的教育历史.6大特色课程:PHP编程.安卓培训.JAVAEE+大数据.UI设计.HTML5培训.云计算架构师,在目前IT市场特别火,每 ...

  10. amazeui学习笔记--css(常用组件14)--缩略图Thumbnail

    amazeui学习笔记--css(常用组件14)--缩略图Thumbnail 一.总结 1.基本样式:在 <img> 添加 .am-thumbnail 类:也可以在 <img> ...