聚簇(Cluster)和聚簇表(Cluster Table)
时间:2010-03-13 23:12来源:OralanDBA.CN 作者:AlanSawyer 点击:157次
1.创建聚簇
icmadmin@icmnlsdb> create cluster cluster_test(id int);
Cluster created.
2.创建聚簇表
icmadmin@icmnlsdb> create table tab_cluster1(id int primary key, name1 varchar2(30)) cluster cluster_test(id);
Table created.
icmadmin@icmnlsdb> create table tab_cluster2(id int primary key, name2 varchar2(30)) cluster cluster_test(id);
Table created.
3.创建聚簇索引
icmadmin@icmnlsdb> create index idx_cluster_test on cluster cluster_test;
Index created.
4.查看聚簇表所属的聚簇
icmadmin@icmnlsdb> select owner,table_name,cluster_name from dba_tables where wner='icmadmin' and table_name in ('TAB_CLUSTER1','TAB_CLUSTER2');
OWNER TABLE_NAME CLUSTER_NAME
-------------- ------------------- ---------------
icmadmin TAB_CLUSTER2 CLUSTER_TEST
icmadmin TAB_CLUSTER1 CLUSTER_TEST
5.初始化几行聚簇表的数据
icmadmin@icmnlsdb> insert into TAB_CLUSTER1 values (1,'AAA');
icmadmin@icmnlsdb> insert into TAB_CLUSTER1 values (2,'BBB');
icmadmin@icmnlsdb> insert into TAB_CLUSTER1 values (3,'CCC');
icmadmin@icmnlsdb> insert into TAB_CLUSTER2 values (1,'AA1');
icmadmin@icmnlsdb> insert into TAB_CLUSTER2 values (2,'BB2');
icmadmin@icmnlsdb> insert into TAB_CLUSTER2 values (3,'CC3');
icmadmin@icmnlsdb> commit;
6.模拟truncate表TAB_CLUSTER1,可以看到报错了,聚簇中的表不允许truncate,原因很简单,聚簇在一个块上存储了多个表,必须删除聚簇表中的行来实现删除
icmadmin@icmnlsdb> truncate table TAB_CLUSTER1;
truncate table TAB_CLUSTER1
*
ERROR at line 1:
ORA-03292: Table to be truncated is part of a cluster
icmadmin@icmnlsdb> truncate table TAB_CLUSTER2;
truncate table TAB_CLUSTER2
*
ERROR at line 1:
ORA-03292: Table to be truncated is part of a cluster
7.清除聚簇表第一种方法:delete方式删除
8.清除聚簇表第二种方法:通过truncate cluster的方式清除cluster中表数据
icmadmin@icmnlsdb> truncate cluster CLUSTER_TEST;
Cluster truncated.
icmadmin@icmnlsdb> select * from TAB_CLUSTER1;
no rows selected
icmadmin@icmnlsdb> select * from TAB_CLUSTER2;
no rows selected
9.删除聚簇
icmadmin@icmnlsdb> drop cluster cluster_test including tables;
Cluster dropped.
聚簇(Cluster)和聚簇表(Clustered Table)的优点:
1.表中的数据一起存储在簇中,连接这些表的查询就可能执行更少的I/O,改善系统性能
2.对于经常一同查询的表可以明显的加速表的连接(Join)
3.非常适合查询频繁的环境
聚簇(Cluster)和聚簇表(Clustered Table)的缺点:
1.因为是一类比较特殊的对象,所以增加了数据库的管理负担
2.非常不适合更改频繁的环境
3.如果聚簇表总是在一起查询,考虑是否可以将他们合并为一个表
聚簇(Cluster)和聚簇表(Clustered Table)的特点:
1.聚簇由多个表组成
2.几个表共享相同的数据块
3.一个聚簇有一个或者多个公共的列,多个表共享这些列,这样的列叫做:聚簇关键字或簇键(Cluster Key)
4.Oracle把多个表的数据物理地存储在一起
聚簇表中插入数据之前,聚簇上需先有聚簇索引。否则会出现如下的错误:
icmadmin@icmnlsdb> select * from tab_cluster1;
select * from tab_cluster1
*
ERROR at line 1:
ORA-02032: clustered tables cannot be used before the cluster index is built