ORACLE indexes Summary

create or replace unique|bitmap index <schema>.<index_name> on <schema>.<table_name>
(<column_name>|<expression> asc|desc ,
<column_name>|<expression> asc|desc ...)
tablespace <tablespace_name>
storage <storage_settings>
logging|nologging
compute statistics
nocompress|compress<nn>
nosort|reverse
partition|global partition<partition_setting>

/*
unique|bitmap : unique表示唯一值索引,bitmap表示位图索引,为空则默认为B-tree索引
column_name|expression asc|desc , ... :可以单列索引,也可以多列进行联合索引,当为expression的时候,为“基于函数的索引”
tablespace : 制定存放索引的表空间(当表和索引在不同的表空间的时候,效率更高)
storage : 可以设置表空间的存储参数
logging|nologging : 是否对索引产生redolog(对于大表来说,可以设置为nologging从而来减少空间占用,提高效率)
compute statistics : 设置为创建索引时,收集统计信息
nocompress|compressnn : 是否使用“键压缩”(使用键压缩可以删除一个键列中出现的重复值)
nosort|reverse : nosort表示与表中相同的顺序进行创建索引,reverse表示使用与表中相反的顺序进行创建索引
partition|nopartition|global partition : 可以在分区表上和未分区表上对创建的索引进行分区
*/

Database space unit is Block, Extent and Segment
Block: Oracle is the use and distribution of the smallest unit of storage. It was decided to set DB_BLOCK_SIZE the establishment by the database. Once the database is generated, the data block size can not be changed. To change only to re-establish the database
Extent: by a group consisting of continuous Block. Extent a composition of one or more Segment. When all space in a Segment is used up, Oracle allocate a new extent for it.
Segment: Extent is made of one or more thereof. It contains all the data for a particular tablespace logical storage structure. Extent of a segment may be discontinuous, even in different data files.

--创建索引
CREATE INDEX IDX_ONE ON DEPT(DNAME);
CREATE INDEX IDX_TWO ON DEPT(DEPTNO,DNAME);

--查看索引,指定表的索引
SELECT SEGMENT_NAME,COUNT(*),SUM(BYTES) FROM DBA_EXTENTS 
WHERE SEGMENT_TYPE='INDEX' AND OWNER='SCOTT' GROUP BY SEGMENT_NAME;
SELECT * FROM ALL_INDEXES WHERE TABLE_NAME='DEPT';
SELECT * FROM USER_INDEXES WHERE TABLE_NAME='DEPT';--DISTINCT_KEYS索引里面不同关键字(值)的数目

--查看索引了哪些字段等
SELECT * FROM DBA_IND_COLUMNS WHERE INDEX_NAME='IDX_TWO';

--修改索引名称
ALTER INDEX IDX_ONE RENAME TO IDX_DEPT_DNAME;

--重建索引
ALTER INDEX IDX_DEPT_DNAME REBUILD TABLESPACE USERS NOLOGGING;

--合并索引(碎片多了,索引效率会降低,可选择重建索引或者合并索引,合并索引方式更好些,无需额外存储空间,代价较低)
ALTER INDEX IDX_NEW COALESCE;

--删除索引
DROP INDEX IDX_DEPT_DNAME;

--索引的选择性=索引里不同关键字的数目/表中行的总数,越接近1越好
SELECT i.distinct_keys / t.num_rows FROM user_indexes i, user_tables t
WHERE i.table_name = 'DEPT' AND i.index_name = 'PK_DEPT' AND i.table_name = t.table_name;

--查看索引扩展次数,索引的extent太多,检索时的速度和效率就会降低。
SELECT COUNT(*), owner, segment_name, tablespace_name FROM dba_extents
WHERE segment_type = 'INDEX' AND owner NOT IN ('SYS', 'SYSTEM')
GROUP BY owner, segment_name, tablespace_name
HAVING COUNT(*) > 10
ORDER BY COUNT(*) DESC;

--查询索引表空间的剩余空间
SELECT TABLESPACE_NAME, ROUND(BYTES / (1024 * 1024), 2) "FREE(M)" FROM SM$TS_FREE;
--SM$TS_USED(已使用空间)
SELECT TABLESPACE_NAME, ROUND(BYTES / (1024 * 1024), 2) "USED(M)" FROM SM$TS_USED;

--查看表与索引的大小
SELECT (SUM(BYTES)/1024/1024) "大小(MB)" FROM DBA_SEGMENTS WHERE SEGMENT_NAME='DEPT';
SELECT (SUM(BYTES)/1024/1024) "大小(MB)" FROM DBA_SEGMENTS WHERE SEGMENT_NAME='IDX_TWO';

 

Published 46 original articles · won praise 9 · views 3654

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/101906543