Clustering Tables   «Prev  Next»

Lesson 5 Creating Oracle Cluster Key
Objective Create a cluster key in Oracle using a cluster index

Creating a Cluster Key in Oracle

In Oracle, tables can be organized into clusters so that rows from different tables that share a common key value are stored together in the same data blocks. This module has been building that structure one piece at a time: creating the cluster itself, sizing it correctly, and now, in this lesson, creating the cluster index, the structure that actually makes the cluster key usable for locating data.

The cluster key is the column, or columns, that logically binds the tables placed in a cluster together. Oracle uses this key to locate the physical data blocks that hold every row associated with a given cluster key value, across every table sharing that cluster. A cluster without a working cluster index is, practically speaking, an unusable cluster: the key exists conceptually, but nothing yet resolves it to an actual storage location.

Role of the Cluster Index

Creating the cluster index is the third step in building an indexed cluster, after creating the cluster itself and sizing it. A cluster index is an index structure that contains the values of the cluster key, but unlike a standard table index, a cluster index does not point to individual rows. Instead, it points to the data block, or the first block in a chain of blocks, where every row sharing a given cluster key value resides.

That "chain of blocks" phrasing matters in practice, not just in theory. The previous lesson's SIZE clause estimates how much space one cluster key value and its associated rows will need, but an estimate is still just an estimate. If a particular key value turns out to need more space than SIZE allocated for it, perhaps one department simply has far more employees than a typical one, Oracle chains additional blocks onto that key's first block rather than failing the insert. The cluster index only ever needs to track the first block in that chain; Oracle follows the chain itself from there. This is also exactly why sizing a cluster reasonably well still matters even though chaining exists as a safety net: a cluster with many poorly sized keys ends up with long block chains, and walking a long chain costs more I/O than the whole point of clustering was meant to save.

An indexed cluster requires a cluster index before the first row can be inserted into any of its tables. Oracle does not create this index automatically: issue a separate CREATE INDEX ... ON CLUSTER statement. You can create empty clustered tables before or after the cluster index. If you later drop the cluster index, the tables and their rows remain intact, but access to the clustered data, including reads, is blocked until you recreate the index. See Oracle 26ai Managing Clusters.

Another important distinction is that a cluster index contains exactly one entry per cluster key value, not one entry per row. A department with forty employees contributes one entry to the cluster index, not forty. This design is what makes it possible to retrieve every row across every clustered table for a given key value with a single index lookup, rather than one lookup per row.


Expert Oracle Indexing and Access Paths

SQL Syntax for Creating a Cluster Index


CREATE INDEX index_name
ON CLUSTER cluster_name;
CREATE INDEX Required keywords to define an index.
index_name The name assigned to the cluster index.
ON CLUSTER Specifies that this index applies to a cluster, not a table.
cluster_name The name of an existing indexed cluster.

Oracle AI Database 26ai carries forward the same IF [NOT] EXISTS DDL convenience available on CREATE CLUSTER, current since Oracle 19.28. On CREATE INDEX, it works the same way, placed right after INDEX and before the index name, so a deployment script that may run more than once does not fail if the cluster index already exists:


CREATE INDEX IF NOT EXISTS index_name
ON CLUSTER cluster_name;

A cluster index also accepts the same storage and tablespace clauses available on an ordinary index, which is useful on a system where I/O for index structures is deliberately separated from the tablespace holding the cluster's own data segment:


CREATE INDEX coin_lot_idx
ON CLUSTER coin_lot
TABLESPACE index_ts
STORAGE (INITIAL 256K NEXT 128K);

None of this changes what the cluster index does; it only changes where its own blocks physically live, kept separate from wherever the coin_lot cluster segment itself was placed back when the cluster was created.


Loading Clustered Tables and Creating Secondary Indexes

Create the required cluster index before loading any rows. Advice about postponing optional secondary indexes until after a bulk load does not apply to the cluster index itself.

Use conventional loading for clustered tables. SQL*Loader direct-path loading does not support them; select conventional path loading instead. Direct-path INSERT also excludes clustered tables, so an APPEND hint cannot enable direct-path loading for this example. Oracle documents conventional serial insertion as the fallback when this direct-path restriction is encountered.

Optional secondary indexes on individual clustered tables are separate objects. Creating these after a large load may reduce index-maintenance work during loading, but evaluate the workload and retain indexes needed for enabled primary-key or unique constraints. The primary-key indexes in the example are distinct from coin_lot_idx.

References: Oracle 26ai SQL*Loader conventional and direct path loads and INSERT restrictions.

Ordinary Indexes vs. the Cluster Index (Conceptual Review)

An ordinary index is a data structure that speeds up access to rows in a table. Indexes store column values in a compact, ordered structure that lets Oracle locate rows with far fewer I/O operations than a full table scan would require. The basic syntax for creating a standard table index looks like this:


CREATE INDEX emp_idx1 ON emp (ename, job);

Here, emp_idx1 is the index name, emp is the table, and ename and job are the indexed columns. Oracle automatically maintains this index's entries whenever the underlying table's data changes. An ordinary index like this one stores a ROWID for each indexed row, which lets Oracle jump directly to that exact row once the index lookup finds a match. Indexes can be unique or non-unique, and rows with only NULL values in every indexed column are left out of the index entirely.

This is exactly where a cluster index diverges from everything just described. An ordinary index answers "find me this one row." A cluster index answers a different question entirely: "find me this one block, which already holds every row across every clustered table sharing this key value." A cluster index stores no ROWID at all, because it has no individual row to point to; it stores a block address instead, and one entry serves every row that shares that key. That's the whole reason a single cluster index lookup can retrieve what would otherwise take several separate row lookups across several separate tables.

In Oracle terminology, an index refers to the physical structure, while a key refers to the logical value stored inside it. That distinction matters most right here: the cluster key is the logical value; the cluster index is the physical structure built to resolve that value to a block.

Creating the Cluster Key

When creating a cluster index, the cluster key columns are not specified again. The cluster key was already defined once, when the cluster itself was created with the CREATE CLUSTER statement; the cluster index is built on whatever key that statement established, and CREATE INDEX ... ON CLUSTER simply names the cluster it belongs to.

Additional clauses are still available on the CREATE INDEX statement itself, such as specifying a tablespace or storage attributes, exactly as they would be for a standard index. The following example creates a cluster index for a cluster named coin_lot:


CREATE INDEX coin_lot_idx
ON CLUSTER coin_lot;

Once this statement executes successfully, the cluster key becomes usable, data manipulation language is unblocked against the cluster's tables, and Oracle can efficiently retrieve every row sharing a given key value with a single index lookup.

A Complete Example: Cluster, Tables, and Index Together

The following four statements create the coin_lot cluster, its cluster index, and two tables that group coin collection records by lot. This is one valid creation order: create the cluster first, then create the index and empty tables in either order. Both the tables and cluster index must exist before inserting rows.


CREATE CLUSTER coin_lot
  (lot_id NUMBER(6))
  SIZE 512
  TABLESPACE users;

CREATE INDEX coin_lot_idx
ON CLUSTER coin_lot;

CREATE TABLE lots (
  lot_id    NUMBER(6) CONSTRAINT lots_pk PRIMARY KEY,
  lot_desc  VARCHAR2(100)
) CLUSTER coin_lot (lot_id);

CREATE TABLE coins (
  coin_id   NUMBER(8) CONSTRAINT coins_pk PRIMARY KEY,
  lot_id    NUMBER(6) CONSTRAINT coins_lot_fk REFERENCES lots(lot_id),
  grade     VARCHAR2(10)
) CLUSTER coin_lot (lot_id);

Creating either empty table before coin_lot_idx is valid; attempting to insert rows before the cluster index exists is not. Once all four statements succeed, rows sharing a lot_id are stored together in the cluster, using additional linked blocks if necessary. The cluster index locates the blocks for that key. When inserting the example data, create the parent row in lots before its referencing rows in coins to satisfy the foreign key.

Checking the Cluster Index in the Data Dictionary

A cluster index is a real index object, and it shows up in the same data dictionary views as any other index. USER_INDEXES reports it with an INDEX_TYPE of CLUSTER, which distinguishes it from the NORMAL or BITMAP values an ordinary table index would carry:


SELECT index_name, index_type, table_name, status
FROM user_indexes
WHERE index_type = 'CLUSTER';

TABLE_NAME in this result actually reports the cluster's own name, not a table's name, since a cluster index belongs to the cluster rather than to any one of the tables stored inside it. Pairing this with USER_CLUSTERS, covered in this module's opening lesson, confirms both halves of an indexed cluster at once: whether the cluster itself exists with the expected key size, and whether its cluster index exists, is valid, and is ready to accept data.

USER_IND_COLUMNS answers a narrower, related question: exactly which column or columns a given index, cluster index included, is actually built on. For the example above, this confirms coin_lot_idx is indeed keyed on lot_id:


SELECT index_name, column_name, column_position
FROM user_ind_columns
WHERE index_name = 'COIN_LOT_IDX';

STATUS in that same USER_INDEXES query reports whether the cluster index itself is VALID; an index that has gone UNUSABLE, through certain maintenance operations or a failed rebuild, is worth checking first whenever an indexed cluster unexpectedly starts rejecting inserts.

What's Next

Everything built across this module so far, creating the cluster, sizing it, and now indexing it, applies specifically to indexed clusters. The next lesson introduces a different kind of cluster entirely: the hash cluster, which replaces the cluster index with a hash function and uses a fundamentally different access strategy, one built for exact-match lookups rather than the range and join-heavy access patterns an indexed cluster handles well.

Frequently Asked Questions

Can the same cluster index serve more than one cluster? No. A cluster index belongs to exactly one cluster, the one named in its ON CLUSTER clause. Two different clusters, even ones with identically typed keys, each need their own separate CREATE INDEX ... ON CLUSTER statement and their own cluster index.

Does the cluster index need to live in the same tablespace as the cluster itself? No, and it often doesn't on a production system. A cluster index is created and managed independently, the same as any other index, and can be placed in its own tablespace with its own storage attributes via the usual TABLESPACE and storage clauses on CREATE INDEX.

What happens if a table is added to the cluster after the cluster index already exists? Nothing special is required. The cluster index is built on the cluster's key, not on any individual table, so a new table created with CLUSTER cluster_name (key) is immediately covered by the existing cluster index without needing to rebuild or recreate anything.

Is a cluster index unique or non-unique? Non-unique, always. Multiple rows, even across different tables in the cluster, legitimately share the same cluster key value; that's the entire point of clustering them together. A uniqueness constraint would contradict the structure's purpose.

Can a cluster index be a composite index on more than one column? Yes, whenever the cluster key itself is composite. The cluster index's column list always matches the cluster key exactly, as defined back when the cluster was created with CREATE CLUSTER; a single-column key produces a single-column cluster index, and a multi-column key produces a matching multi-column one.

Does analyzing a cluster with DBMS_STATS also gather statistics on its cluster index? Gathering statistics on a cluster's tables and gathering statistics on the cluster index itself are handled the same way they are for an ordinary table and its indexes: statistics collection on the cluster index happens through the standard index-statistics path, keyed to the index's own name, exactly as it would for emp_idx1 or any other index in USER_INDEXES.

Why does the example in this lesson create the cluster index before the tables, rather than after? It is a convenient valid order, not a requirement to create the index before the tables. After creating the cluster, you may create its empty tables and cluster index in either order. The cluster index must exist before the first insert.

Creating Cluster Key – Quiz

Use the quiz below to test your understanding of clusters and cluster indexes.

Creating Cluster Key – Quiz


SEMrush Software 5 SEMrush Banner 5