| Lesson 9 | Oracle table clustering conclusion |
| Objective | Summarize how to design, create, size, evaluate, and remove Oracle table clusters. |
An Oracle table cluster is a schema object that stores rows from one or more tables in shared data blocks according to a common cluster key. Its purpose is physical locality: rows that applications repeatedly retrieve together can be placed together, allowing Oracle to satisfy some joins and key-based lookups with fewer block visits. The SQL used to query clustered tables does not change. Applications continue to work with ordinary tables, rows, and columns while Oracle uses the cluster's physical organization underneath.
This module has shown that clustering is a specialized design technique, not a universal performance setting. A well-chosen cluster can reduce logical I/O, shorten access paths, and avoid repeatedly storing the same cluster key value. A poorly chosen or badly sized cluster can waste space, create overflow chains, slow full table scans, and make inserts or cluster-key updates more expensive. The decision therefore begins with measured workload behavior and realistic data distributions, not with the assumption that related tables should automatically be clustered.
The strongest candidates are tables that are primarily queried, are frequently joined on the same stable column or column group, and return several related rows for each key value. An order header and its order details grouped by order number, or departments and employees grouped by department number, illustrate the intended pattern. A single-table hash cluster can also be useful when one table is overwhelmingly accessed through exact equality predicates on a stable key.
A cluster is less suitable when tables are normally accessed separately, inserts and updates dominate the workload, cluster-key values change often, or full table scans are common. Changing a cluster key can require Oracle to relocate a row to the blocks associated with its new value. Scanning one table in a multi-table cluster can also require Oracle to pass through blocks that contain rows belonging to the other clustered tables.
Cardinality matters as well. A useful cluster key normally groups enough related rows to make shared storage worthwhile without grouping so many rows that each key value requires a long chain of blocks. A key with only a few shared rows may provide little benefit and waste space. A very low-cardinality key can create large row groups that require extra searching. Before choosing a cluster, compare representative execution plans, buffer gets, elapsed time, DML cost, and space consumption against a conventional heap-table design with appropriate indexes.
Oracle table clustering should not be confused with server clustering. Table clusters organize rows inside database blocks. Oracle Real Application Clusters (RAC) coordinates multiple database instances for availability and scale. The two technologies operate at different architectural levels and solve different problems.
The cluster key is the column or set of columns used to group rows. Each table placed in the cluster must contain corresponding columns with compatible datatypes and sizes, although the table column names do not have to match the names declared in the cluster. A composite key is appropriate when the workload consistently joins or filters on the same column combination.
The key should reflect the actual access path. If applications repeatedly retrieve all employees for one department, department_id is a plausible key. If most queries instead locate individual employees by employee_id, clustering by department may offer little value. Cluster-key selection is therefore inseparable from query analysis.
Cluster key definitions have datatype restrictions. Types such as LONG, LONG RAW, BLOB, and CLOB cannot be cluster key columns. Legacy LONG and LONG RAW data should normally be migrated to modern LOB types, but LOB columns still cannot serve as the cluster key. They can be handled through an appropriate table design outside the key itself.
An index cluster uses a separate cluster index to map each distinct cluster key value to the first block containing its rows. Unlike an ordinary table index, which commonly has entries that identify individual rows, the cluster index has one entry for each distinct cluster key value. This lets one lookup locate related rows from every table in the cluster.
An indexed cluster must have its cluster index before DML can be performed on its clustered tables. Creating the cluster alone is not enough. Oracle does not create the cluster index automatically.
A hash cluster applies a hash function to the cluster key and uses the result to determine the row's storage location. Because Oracle can calculate the target location, an equality lookup on the complete cluster key can avoid the separate cluster-index access. Hash clusters are best suited to predictable equality searches such as WHERE department_id = :department_id. They do not provide the ordered access needed for range predicates.
A hash cluster may use Oracle's internal hash function, or a valid numeric expression supplied with HASH IS. The cluster key itself does not have to be numeric when Oracle's internal function is used. Composite and character keys are permitted subject to the normal cluster datatype restrictions; however, a user-specified HASH IS expression must evaluate to a positive integer value.
A hash cluster has no cluster index, and Oracle does not permit one to be created. The direct calculation is the access mechanism. Hash clusters also allocate their planned space when they are created, so inaccurate estimates can have an immediate storage cost.
The indexed-cluster workflow has three structural steps: create the cluster, create tables that map their columns to the cluster key, and create the cluster index. The index must exist before rows are loaded or modified. The following compact example groups department and employee rows by department_id:
CREATE CLUSTER emp_dept_cluster
(department_id NUMBER(4))
SIZE 1024
TABLESPACE users;
CREATE TABLE departments (
department_id NUMBER(4) PRIMARY KEY,
department_name VARCHAR2(50)
) CLUSTER emp_dept_cluster (department_id);
CREATE TABLE employees (
employee_id NUMBER(6) PRIMARY KEY,
employee_name VARCHAR2(80),
department_id NUMBER(4)
) CLUSTER emp_dept_cluster (department_id);
CREATE INDEX emp_dept_cluster_ix
ON CLUSTER emp_dept_cluster
TABLESPACE users;
The cluster owns the shared storage characteristics. Storage attributes specified for individual tables in the cluster do not replace the cluster's storage settings. The tables can still have conventional indexes for other access paths; the cluster index serves only the shared cluster key.
The SIZE clause estimates the bytes required for all rows associated with an average cluster key value or hash value. Oracle uses this estimate to determine how many key groups can fit in a block. SIZE is not a hard limit on the data belonging to one key. When a key's rows need more room, Oracle can chain additional blocks; however, excessive chaining undermines the locality benefit that justified the cluster.
Estimate SIZE from real or representative data. For a single-table cluster, determine the average row size and the average number of rows per key, then allow for row overhead, block overhead, free space, and future growth. For a join cluster, include the contribution from every clustered table:
estimated_bytes_per_key =
parent_rows_per_key * average_parent_row_size
+ child_rows_per_key * average_child_row_size
+ overhead_and_growth_allowance
Averages alone can hide skew. If most departments have ten employees but one department has thousands, examine percentiles and outliers before accepting a single estimate. A value that is too small creates overflow and chained blocks. A value that is too large reserves unnecessary space and reduces packing efficiency. PCTFREE should also reflect expected row growth.
After deployment, gather optimizer statistics and monitor actual behavior. The USER_CLUSTERS, ALL_CLUSTERS, or DBA_CLUSTERS views describe cluster settings; related table and index views help confirm membership and access structures. If observed distributions differ materially from the original assumptions, test a rebuilt cluster with a revised size rather than allowing a poor physical design to become permanent.
A hash cluster adds the HASHKEYS clause. HASHKEYS represents the planned number of distinct hash values, not the total number of rows. Oracle rounds the requested value up to a prime number and allocates cluster space from the combined SIZE and HASHKEYS estimates. Leave reasonable capacity for growth rather than sizing only for today's distinct-key count.
CREATE CLUSTER product_hash_cluster
(product_id NUMBER)
SIZE 1024
HASHKEYS 2000
TABLESPACE users;
CREATE TABLE products (
product_id NUMBER PRIMARY KEY,
product_name VARCHAR2(100)
) CLUSTER product_hash_cluster (product_id);
No cluster-index statement follows this example. Oracle's internal hash function maps product_id values to the allocated hash locations.
A collision occurs when different cluster key values map to the same hash value. A collision is not a data-integrity error: Oracle still distinguishes the keys and returns the correct rows. It can, however, require extra searching or overflow blocks and reduce the expected performance advantage. Underestimating HASHKEYS, allowing the number of distinct values to grow beyond the plan, or choosing a poor custom hash expression can increase collisions.
Use a custom HASH IS expression only when the data distribution is well understood and the expression spreads values effectively. Dense integer identifiers can sometimes be used directly; a function such as MOD can fold a larger range into a fixed set of results. Oracle's internal function is usually the safer choice for character, composite, or irregularly distributed keys.
A cluster should be justified by evidence. Establish a baseline using the existing heap tables and indexes, then load representative data into a test cluster and compare the same statements. Review execution plans, consistent gets, physical reads, row counts, elapsed time, and the cost of representative inserts and updates. Test both favorable queries and unfavorable operations such as individual-table scans.
Useful metadata checks include:
SELECT cluster_name,
cluster_type,
tablespace_name,
key_size,
hashkeys
FROM user_clusters
ORDER BY cluster_name;
SELECT cluster_name,
clu_column_name,
table_name,
tab_column_name
FROM user_clu_columns
ORDER BY cluster_name, table_name, clu_column_name;
Statistics must be current before plan comparisons are trusted. Testing should also reflect concurrency and data growth. A design that performs well with a small uniform sample may behave differently when busy keys become much larger than average or DML volume increases.
Modern storage does not eliminate the relevance of block access. Even when the database runs on flash or cached cloud storage, reducing buffer gets and avoiding unnecessary block processing can still matter. At the same time, modern optimizers, indexing options, partitioning, and application caching often solve performance problems with less restrictive physical organization. Clustering should win a measured comparison against those alternatives.
An empty cluster can be removed with a basic DROP CLUSTER statement. If it still contains tables, INCLUDING TABLES is required. CASCADE CONSTRAINTS is needed when referential constraints outside the cluster depend on primary or unique keys belonging to tables that will be dropped:
DROP CLUSTER emp_dept_cluster
INCLUDING TABLES
CASCADE CONSTRAINTS;
This operation is destructive. Inventory the cluster's tables, indexes, constraints, privileges, dependent objects, and required data before execution. Export or copy any data that must survive, test the restoration path, and confirm the exact target schema and cluster name.
Dropping the entire cluster with INCLUDING TABLES lets Oracle remove the shared segment efficiently. Dropping one table while preserving other tables in the cluster can be slower because Oracle must remove that table's rows from shared blocks individually. When an index cluster is dropped, its cluster index is dropped with it. A hash cluster has no cluster index to remove.
Oracle does not provide an in-place command that turns a clustered table into an ordinary heap table. To migrate away from clustering, preserve the data, remove the clustered structure, recreate each required table without its CLUSTER clause, restore constraints and indexes, reload the data, gather statistics, and validate the application. This controlled rebuild is also the normal remedy when SIZE or HASHKEYS no longer reflects production data.
Before approving an Oracle table cluster, confirm all of the following:
SIZE includes rows from every table, overhead, free space, and expected growth.HASHKEYS value.After completing this module, you should be able to:
SIZE from row size, rows per key, overhead, skew, and growth.HASHKEYS affects allocation and collisions.The next module introduces index-organized tables, another Oracle physical design option. Unlike a table cluster, which groups rows by a shared cluster key or hash value, an index-organized table stores its rows in a primary-key B-tree structure. Keep the distinction clear: both features change physical storage, but they optimize different access patterns.
Use the quiz to check your understanding of hash clusters, space allocation, and the clauses required when dropping a cluster.
Hash Cluster and Dropping Clusters – Quiz