Using Clusters and Index-Organized Tables
Objectives Creating and maintaining clusters Using index-organized tables Retrieving information about clusters and tables from the data dictionary
Distribution of Rows Within a Table Cluster Index-organized table Ordering of Rows Random Grouped Ordered
Clusters Unclustered ORD and ITEM tables Clustered ORD and ITEM tables ORD_NO PROD QTY ... ----- ------ ------ 101 A4102 20 102 A2091 11 102 G7830 20 102 N9587 26 101 A5675 19 101 W0824 10 ORD_NO ORD_DT CUST_CD ------ ------ ------ 101 05-JAN-97 R01 102 07-JAN-97 N45 Cluster Key (ORD_NO) 101 ORD_DT CUST_CD 05-JAN-97 R01 PROD QTY A4102 20 A5675 19 W0824 10 102 ORD_DT CUST_CD 07-JAN-97 N45 A2091 11 G7830 20 N9587 26 Unclustered ORD and ITEM tables Clustered ORD and ITEM tables
Cluster Types Index cluster Hash cluster Hash function
Creating Index Clusters . Create a cluster. . Create a cluster index. CREATE CLUSTER scott.ord_clu (ord_no NUMBER(3)) SIZE 200 TABLESPACE DATA01 STORAGE(INITIAL 5M NEXT 5M PCTINCREASE 0); CREATE INDEX scott.ord_clu_idx ON CLUSTER scott.ord_clu TABLESPACE INDX01 STORAGE(INITIAL 1M NEXT 1M PCTINCREASE 0);
Creating Index Clusters . Create tables in the cluster. CREATE TABLE scott.ord (ord_no NUMBER(3) CONSTRAINT ord_pk PRIMARY KEY, ord_dt DATE, cust_cd VARCHAR2(3)) CLUSTER scott.ord_clu(ord_no); CREATE TABLE scott.item (ord_no NUMBER(3) CONSTRAINT item_ord_fk REFERENCES scott.ord, prod VARCHAR2(5), qty NUMBER(3), CONSTRAINT item_pk PRIMARY KEY(ord_no,prod)) CLUSTER scott.ord_clu(ord_no);
Creating Hash Clusters . Create a cluster. . Create tables in a cluster. CREATE CLUSTER scott.off_clu (country VARCHAR2(2),postcode VARCHAR2(8)) SIZE 500 HASHKEYS 1000 TABLESPACE DATA01 STORAGE(INITIAL 5M NEXT 5M PCTINCREASE 0); CREATE TABLE scott.office( office_cd NUMBER(3), cost_ctr NUMBER(3), country VARCHAR2(2), postcode VARCHAR2(8)) CLUSTER scott.off_clu(country,postcode);
Parameters Specific to Hash Clusters HASHKEYS: Number of key values HASH IS: Optional user-defined hash function Key 21 Key 12 Key 11 Key 1 Key 2 Key 3 Key 22 Overflow block Preallocated blocks
Dropping Clusters Use INCLUDING TABLES to drop tables and cluster or drop tables before dropping cluster. DROP CLUSTER scott.ord_clu INCLUDING TABLES; DROP TABLE scott.ord; DROP TABLE scott.item; DROP CLUSTER scott.ord_clu;
Index-Organized Tables Indexed access on table Accessing index- organized table ROWID Non-key columns Key column Row header
Creating Index-Organized Tables CREATE TABLE scott.sales ( office_cd NUMBER(3), qtr_end DATE, revenue NUMBER(10,2), review VARCHAR2(1000), CONSTRAINT sales_pk PRIMARY KEY(office_code, qtr_end)) ORGANIZATION INDEX TABLESPACE data01 PCTTHRESHOLD 20 OVERFLOW TABLESPACE data02;
Summary Identifying situations where clusters are useful Using index-organized tables