时间:2021-07-01 10:21:17 帮助过:7人阅读
对于存在大量DML操作的表,不适合建立表簇。
对于需要经常进行全表扫描的表,不适合建立表簇。(不再像非簇表那样,一个数据块中仅包含一张表中的数据,还包含了与簇键相关的其他表数据行的数据,这意味着扫描簇中单独的一张表,需要扫描更多的数据块)
对于需要频繁进行TRUNCATE操作的表,不适合建立表簇。(簇表中无法针对单独的被簇表进行truncate操作)
此外,对于哈希表簇,不用也不能创建索引
--创建一个表簇
Yumiko@Sunny >create cluster clu_info_employee (deptno number) size 1024; Cluster created. Yumiko@Sunny >select * from tab where TNAME like ‘%CLU%‘; TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------- CLU_INFO_EMPLOYEE CLUSTER
--通过查询user_clusters视图(或者dba_clusters视图),可以看到创建的表簇为index cluster,且SIZE设置为1024 Yumiko@Sunny >select cluster_name, tablespace_name, cluster_type, key_size from user_clusters; CLUSTER_NAME TABLESPACE_NAME CLUST KEY_SIZE ------------------------------ ------------------------------ ----- ---------- CLU_INFO_EMPLOYEE USERS INDEX 1024
--通过user_objects视图(或者dba_objects视图),同样可以查阅cluster的信息。
--需要注意,同一个cluster下的对象,其DATA_OBJECT_ID的值一致。 Yumiko@Sunny >select OBJECT_ID,OBJECT_NAME,DATA_OBJECT_ID,OBJECT_TYPE from user_objects where OBJECT_NAME like ‘%CLU%‘; OBJECT_ID OBJECT_NAME DATA_OBJECT_ID OBJECT_TYPE ---------- ------------------------- -------------- ------------------- 52626 CLU_INFO_EMPLOYEE 52626 CLUSTER
--通过user_clu_columns视图可以看到,此时未显示刚刚创建的cluster信息,表明该簇目前为空簇。 Yumiko@Sunny >select * from USER_CLU_COLUMNS; no rows selected
--此处应该注意到,通过dba_segments视图查看,虽然当前是空簇,但已出现刚刚创建的cluster,证明此时已占用了空间。 Yumiko@Sunny >select SEGMENT_NAME,SEGMENT_TYPE, extents, HEADER_FILE, HEADER_BLOCK, BYTES, BLOCKS from dba_segments where owner=‘SCOTT‘ and segment_name like ‘%CLU%‘; SEGMENT_NAME SEGMENT_TYPE EXTENTS HEADER_FILE HEADER_BLOCK BYTES BLOCKS ---------------------------------------------------------------------------------------- CLU_INFO_EMPLOYEE CLUSTER 1 4 395 65536 8
--创建簇表 Yumiko@Sunny >create table clu_info_dept(DEPTNO number,DNAME VARCHAR2(14),LOC VARCHAR2(13)) cluster CLU_INFO_EMPLOYEE(deptno); Table created. Yumiko@Sunny >create table clu_info_emp(DEPTNO number,ENAME VARCHAR2(10),JOB VARCHAR2(9)) cluster CLU_INFO_EMPLOYEE(deptno); Table created.
--查询user_clu_columns视图可以注意到,此时出现了簇及簇表的相应信息,说明此时,簇已不再是空簇。 Yumiko@Sunny >select * from USER_CLU_COLUMNS; CLUSTER_NAME CLU_COLUMN_NAME TABLE_NAME TAB_COLUMN_NAME ----------------------------------------------------------------------------------------- CLU_INFO_EMPLOYEE DEPTNO CLU_INFO_DEPT DEPTNO CLU_INFO_EMPLOYEE DEPTNO CLU_INFO_EMP DEPTNO Yumiko@Sunny >select * from tab where TNAME like ‘%CLU%‘; TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------- CLU_INFO_DEPT TABLE 1 CLU_INFO_EMP TABLE 2 CLU_INFO_EMPLOYEE CLUSTER
--同上面所说,查询user_objects视图可以看到簇的所有信息,此外可以注意到DATA_OBJECT_ID列是一致的,如前所说 Yumiko@Sunny >select OBJECT_ID,OBJECT_NAME,DATA_OBJECT_ID,OBJECT_TYPE from user_objects where OBJECT_NAME like ‘%CLU%‘; OBJECT_ID OBJECT_NAME DATA_OBJECT_ID OBJECT_TYPE ---------- ------------------------- -------------- ------------------- 52627 CLU_INFO_DEPT 52626 TABLE 52628 CLU_INFO_EMP 52626 TABLE 52626 CLU_INFO_EMPLOYEE 52626 CLUSTER --查询此时的dba_segments视图,并未发现添加的两张簇表 Yumiko@Sunny >select SEGMENT_NAME,SEGMENT_TYPE, extents, HEADER_FILE, HEADER_BLOCK, BYTES, BLOCKS from dba_segments where owner=‘SCOTT‘ and segment_name like ‘%CLU%‘; SEGMENT_NAME SEGMENT_TYPE EXTENTS HEADER_FILE HEADER_BLOCK BYTES BLOCKS ------------------------------------------------------------------------------------ CLU_INFO_EMPLOYEE CLUSTER 1 4 395 65536 8
--查询此时的dba_tables视图,却可以发现刚刚建立的表 Yumiko@Sunny >select TABLE_NAME,CLUSTER_NAME,STATUS from dba_tables where owner=‘SCOTT‘ and table_name like ‘%CLU%‘; TABLE_NAME CLUSTER_NAME STATUS ------------------------------ ------------------------------ -------- CLU_INFO_EMP CLU_INFO_EMPLOYEE VALID CLU_INFO_DEPT CLU_INFO_EMPLOYEE VALID
--尝试加载数据失败,报错明显提示了未建立簇索引,也符合之前说过的创建顺序 Yumiko@Sunny >insert into clu_info_dept select * from dept; insert into clu_info_dept select * from dept * ERROR at line 1: ORA-02032: clustered tables cannot be used before the cluster index is built
-- 创建簇索引,注意此时的关键字on cluster Yumiko@Sunny >create index CLU_INFO_index on cluster CLU_INFO_EMPLOYEE; Index created. --查询此时的dba_segments视图,同样有索引的segments信息。 Yumiko@Sunny >select SEGMENT_NAME,SEGMENT_TYPE, extents, HEADER_FILE, HEADER_BLOCK, BYTES, BLOCKS from dba_segments where owner=‘SCOTT‘ and segment_name like ‘%CLU%‘; SEGMENT_NAME SEGMENT_TYPE EXTENTS HEADER_FILE HEADER_BLOCK BYTES BLOCKS -------------------------------------------------------------------------------------- CLU_INFO_INDEX INDEX 1 4 403 65536 8 CLU_INFO_EMPLOYEE CLUSTER 1 4 395 65536 8 --加载数据 Yumiko@Sunny >insert into clu_info_dept select * from dept; 4 rows created.
--再次查询dba_segments视图,依然没有cluster table的信息
Yumiko@Sunny >select SEGMENT_NAME,SEGMENT_TYPE, extents, HEADER_FILE, HEADER_BLOCK, BYTES, BLOCKS from dba_segments where owner=‘SCOTT‘ and segment_name like ‘%CLU%‘;
SEGMENT_NAME SEGMENT_TYPE EXTENTS HEADER_FILE HEADER_BLOCK BYTES BLOCKS -------------------------------------------------------------------------------------- CLU_INFO_INDEX INDEX 1 4 403 65536 8 CLU_INFO_EMPLOYEE CLUSTER 1 4 395 65536 8
此外,对于簇表本身的删除,按照普通表方法即可。
下面引用oracle官方文档的例子:
CREATE CLUSTER call_detail_cluster (
telephone_number NUMBER,
call_timestamp NUMBER SORT,
call_duration NUMBER SORT )
HASHKEYS 10000
HASH IS telephone_number
SIZE 256;
其中:
HASH IS参数:
HASHKEYS参数:
SIZE参数:
该参数越大,虽然单独的数据块可以容纳的簇键会减少,甚至可能会由于单个簇键占用比实际需求更多的空间造成空间的浪费,但由于有更多的空间存储更多的相关数据,只要设置合理,一
定程度上却可以降低物理读。
Oracle索引总结(五)- Oracle索引种类之表簇索引(cluster index)
标签: