:
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 |