cluster可以用來保存多個表的數據。cluster分爲兩種:
index cluster:
用索引來檢索cluster key。不過需要注意的是,創建聚集的時候不會創建索引,需要在create cluster之後另外創建索引,否則在此之前不能對聚集表進行DML操作。
hash cluster:
利用散列函數來檢索cluster key。不能對hash cluster 建立索引。
一、創建cluster
創建索引的時候需要注意,cluster key可以對應多個表的不同名稱的列,但是這些列必須具有相同的數據類型和數據長度。同時,不能對cluster key建立constraint。另外,cluster 的storage參數、transaction參數等會覆蓋table的對應參數。還有就是,創建索引需要create cluster的權限和表空間的quota。
1、查看用戶權限。
SQL>grant create cluster to sh;---授予權限;
SQL> select * from user_sys_privs where privilege ='CREATE CLUSTER';---查詢權限
USERNAME PRIVILEGE ADM
------------------------------ ---------------------------------------- ---
SH CREATE CLUSTER NO
2、創建cluster
----第一步,創建index cluster;
SQL>create cluster tt(
2 prod_id number)
3 size 512
4 storage(
5 initial 100k
6 next 50k);
此時並沒有創建索引,如果現在往cluster 中添加表的話,就會報錯:
SQL> create table tt1
2 cluster tt(prod_id) as
3 select * from sales where prod_id =13;
select * from sales where prod_id =13
*
ERROR at line 3:
ORA-02032: clustered tables cannot be used before the cluster index is built
另外創建索引的時候,需要注意的是索引是對所有的cluster key創建的,不能單獨創建。否則會報錯,從cluster index 的創建語法可以看出。
SQL> create index tt on cluster tt(prod_id);
create index tt on cluster tt(prod_id)
*
ERROR at line 1:
ORA-02158: invalid CREATE INDEX option
-----第二步,創建cluster index;
SQL> create index tt on cluster tt;
Index created.
然後,第三步就是往聚集中添加表。
SQL> create table tt1
2 cluster tt(prod_id)
3 as
4 select * from sales where prod_id =13;
Table created.
SQL> create table tt2
2 cluster tt(prod_id)
3 as
4 select * from sales where prod_id =15;
Table created.
最後,我們查看一下cluster tt在動態性能視圖中的信息。
----通過user_clusters 我們可以看到創建cluster的時候的參數。
SQL> select cluster_name,pct_free,pct_used,key_size,ini_trans,max_trans,initial_extent,next_extent,min_extents,max_extents,pct_increase,cluster_type,single_table,dependencies from user_clusters where
2 cluster_name ='TT';
CLUSTER_NAME PCT_FREE PCT_USED KEY_SIZE INI_TRANS MAX_TRANS INITIAL_EXTENT NEXT_EXTENT MIN_EXTENTS MAX_EXTENTS PCT_INCREASE
------------------------------ ---------- ---------- ---------- ---------- ---------- -------------- ----------- ----------- ----------- ------------
CLUST SINGLE_TAB DEPENDEN
----- ---------- --------
TT 10 512 2 255 106496 57344 1 2147483645
INDEX N DISABLED
-----通過查看user_clu_columns我們可以看到該cluster中有兩個表。
SQL> select * from user_clu_columns;
CLUST CLU_COLUMN TABLE TAB_COLUMN
----- ---------- ----- ----------
TT PROD_ID TT1 PROD_ID
TT PROD_ID TT2 PROD_ID
3、修改cluster
可以通過alter cluster來修改下面三種參數:
1、物理屬性(initrans和storage parameter)
2、SIZE(分配給每個cluster key的字節空間)
3、併發度
-----修改cluster key size
SQL> alter cluster tt size 560;
Cluster altered.
SQL> select cluster_name,key_size from user_clusters where cluster_name ='TT';
CLUST KEY_SIZE
----- ----------
TT 560
-----修改storage參數和parallel參數;
SQL> alter cluster tt storage(next 30k) parallel 3;
Cluster altered.
SQL> select cluster_name,next_extent,degree from user_clusters where cluster_name ='TT';
CLUST NEXT_EXTENT DEGREE
----- ----------- --------------------
TT 32768 3
另外需要注意的是,對於聚集表,alter table 不能修改該表對應上面的這些參數,否則會報錯:ORA-01771,
illegal option for a clustered table。因爲聚集表爲了方便管理,必須覆蓋所有聚集表的上面三種參數信息,以保持信息的一致性。爲此,alter table 只能修改邏輯參數信息,如column的增刪改操作、constraint的增刪改操作等信息。
4、刪除cluster
刪除cluster table:
SQL> drop table tt1;
Table dropped.
----刪除表TT1之後,會發現一個有趣的現象,就是user_clu_columns中依然有刪除表的信息,只是名字改變罷了。
----顯然這是表還在回收站中的緣故,清空一下回收站,然後再查看user_clu_columns,刪除的表就不見了!
SQL> select * from user_clu_columns;
CLUST CLU_COLUMN TABLE TAB_COLUMN
----- ---------- ----- ----------
TT PROD_ID BIN$l PROD_ID
5irW+
YqREa
GPiS+
9tpLN
w==$0
TT PROD_ID TT2 PROD_ID
SQL> purge recyclebin;
Recyclebin purged.
SQL> select * from user_clu_columns;
CLUST CLU_COLUMN TABLE TAB_COLUMN
----- ---------- ----- ----------
TT PROD_ID TT2 PROD_ID
刪除cluster index:
SQL>drop index tt;
刪除cluster:
SQL>DROP CLUSTER TT;---只是刪除聚集;
SQL>DROP CLUSTER TT INCLUDING TABLES CASCADE;----刪除聚集的同時,刪除聚集表;
SQL>DROP CLUSTER TT INCLUDING TABLES CASCADE CONSTRAINTS;--若是外鍵表,則連帶刪除外鍵表。