Tuesday, September 3, 2013

Oracle Cluster tables - an efficient way to reduce the I/O

If you two are more tables are joined together on a single column and most of the time you issue join queries on them, then consider creating a cluster of these tables.
 A cluster is a group tables that share the same data blocks i.e. all the tables are physically stored together.

For example EMP and DEPT table are joined on DEPTNO column. If you cluster them, Oracle physically stores all rows for each department from both the emp and dept tables in the same data blocks.
Since cluster stores related rows of different tables in same data blocks, Disk I/O is reduced and access time improves for joins of clustered tables.
Each cluster key value is stored only once each in the cluster and the cluster index, no matter how many rows of different tables contain the value.
Therefore, less storage might be required to store related table and index data in a cluster than is necessary in non-clustered table format.

Creating a Clustered Table 
 To create clustered tables. First, create a cluster and create index on it. Then create tables in it.
For example to create a cluster of EMP and DEPT tables in which the DEPTNO will be cluster key, first create the cluster by typing the following command.

SQL>create cluster emp_dept (deptno number(2));

Then create index on it.

SQL>create index on cluster emp_dept;

Now create table in the cluster like this

SQL>create table dept (deptno number(2),
                    name varchar2(20),
                    loc varchar2(20))
                    cluster emp_dept (deptno);

SQL>create table emp (empno number(5),
            name varchar2(20),
            sal number(10,2),
            deptno number(2)) cluster emp_dept (deptno)      

Dropping Clusters
To drop a cluster use DROP CLUSTER statement. For example to drop the emp_dept cluster give the following command.

SQL>drop cluster emp_dept;

This will drop the cluster, if the cluster is empty i.e. no tables are existing it it. If tables are there in the cluster first drop the tables and then drop the cluster. If you want to drop the cluster even when tables are there then give the following command.

SQL>drop cluster emp_dept including tables;

Listing Information about Clusters
To see how many clusters are there in your schema give the following statement.

SQL>select * from user_clusters;

To see which tables are part of a cluster. Give the following command.

SQL>select * from tab

TABLE_NAME    TYPE            CLUSTER_ID
----------             ----              -----------
EMP                  TABLE         1
SALGRADE        TABLE        
CUSTOMER        TABLE        
DEPT                TABLE         1
In the above example notice the CLUSTER_ID column, for EMP and DEPT table the cluster_id is 1. That means these tables are in cluster whose cluster_id is 1. You can see the cluster_id’s name in USER_CLUSTERS table.
Source:- http://www.oracle-dba-online.com/

No comments:

Post a Comment