Clustering Tables   «Prev  Next»

Lesson 6 Hash clusters
Objective Know when to use an Oracle hash cluster

Oracle 26ai Hash Clusters: When Equality Lookups Benefit

A hash cluster is an Oracle storage structure that places rows according to a hash value calculated from their cluster key. It offers a different way to find data: instead of following a separate cluster index, Oracle uses the key to determine where to begin looking. For a suitable workload, removing that index lookup can reduce the work required to retrieve related rows.

The decision to use a hash cluster depends on the questions an application asks and how its data will grow. A table being read frequently is not enough. The strongest candidates have repeated equality searches on a known key, reasonably predictable storage requirements, and a distribution of rows that can be accommodated without extensive overflow. This lesson explains those conditions for Oracle AI Database 26ai. The next lesson covers creation in more detail.

From a Cluster Key to Stored Rows

Keep three concepts separate: the cluster key, the hash value, and the stored rows. The cluster key consists of the column or columns chosen to organize data. An example is department_id. A hash function transforms a particular key value into a hash value. Oracle uses that result to identify candidate storage in the cluster, where it checks rows against the query's conditions.

For example, suppose an employee table is stored in a hash cluster keyed by department. A request for department 20 supplies the key needed for hashing:

SELECT employee_id, employee_name, department_id
FROM employees
WHERE department_id = 20;

This query assumes that the table and its columns already exist. It illustrates an access requirement, not a complete installation script. The number 20 is the department identifier; it is not necessarily the resulting hash value or a physical block number. There is no need for application code to calculate Oracle's internal hash result before submitting the query.

The same organization applies when storing rows. Oracle uses the cluster key to determine their placement. Retrieval and storage therefore depend on the same key definition. This is a physical design choice, not simply a query hint that can be added to an arbitrary heap table. Ordinary SQL still addresses the table by name.

Reading the Hash-Lookup Diagram

Oracle 26ai hash lookup: TYPE 22 maps to illustrative bucket 2, which contains matching TYPE 22 rows and a TYPE 37 collision; predicate filtering returns only TYPE 22 rows. Overflow blocks may be needed.
Equality lookup in a hash cluster: Oracle hashes the cluster key, locates candidate storage, and checks the predicate. The TYPE = 37 row illustrates a collision and is excluded from the result. The mapping to bucket 2 is illustrative, not a verified Oracle hash result. Overflow may require additional blocks and reads.
SELECT * FROM hash_table WHERE TYPE = 22;

In this illustration, assume that TYPE is the table's hash-cluster key. The diagram follows four operations: receive an equality condition, compute a hash value, locate candidate storage, and check the predicate. Bucket 2 contains two matching TYPE = 22 rows and one TYPE = 37 row that illustrates a collision. Only the two matching rows appear in the illustrated result. These sample values demonstrate the mechanism, not an executed Oracle query.

Reaching candidate storage is not the same as proving that every row there matches. Different keys can produce the same hash value, and a key's data can require additional blocks. Those details explain both the potential speed of hashing and why a simple diagram cannot promise a fixed number of reads.

Expert Oracle Indexing and Access Paths

Hash Clusters and Indexed Clusters

The previous lesson on cluster indexes describes an indexed cluster. That structure requires a cluster index before rows can be inserted. Empty clustered tables may be created before or after that index, but loading must wait until the index exists. A hash cluster uses a different access mechanism and does not require this cluster-index step.

Oracle does not allow a cluster index on a hash cluster. However, this does not mean that tables stored in a hash cluster can never have indexes. Separate table indexes can support other access requirements or constraints. An index on employee name, for example, serves a different purpose from organizing employees by department. Additional indexes also bring their own storage and maintenance costs.

Do not confuse either kind of Oracle table cluster with a server cluster or a hash join. A table cluster concerns how table data is stored. A hash join is an execution technique for combining row sources. Seeing hashing in an execution plan does not establish that the underlying table uses hash-cluster storage.

Why “One Read Instead of Two” Is Only a Simplification

An indexed access path first locates an index entry and then accesses the associated table data, unless the index itself supplies everything needed. Hash access can avoid the separate index search. When the desired data fits in the relevant block and that block must be fetched, this provides an attractive small-read access pattern.

It does not establish that every query performs exactly one physical read or that hashing cuts I/O in half. Indexes can involve several levels, requested blocks may already be cached, and rows may span multiple blocks. A query that returns hundreds of employees must process those employees even if it locates their starting storage efficiently. Finding data and returning all of it are different costs.

When comparing designs, distinguish logical block accesses from physical reads and elapsed time. A cached indexed lookup may already be inexpensive. A hash lookup with overflow may inspect more blocks than expected. Measure the actual application statements with representative data instead of converting the diagram into a universal performance formula.

When a Hash Cluster Is a Good Candidate

Begin with the application's frequent predicates. Hashing is most useful when those predicates provide equality values for the cluster key. A department lookup is a natural example if callers usually request all employees for one identified department. A composite key can also be appropriate when callers routinely supply all of its components.

SELECT employee_id, employee_name
FROM employees
WHERE company_id = 7
  AND department_id = 20;

If the proposed cluster key is (company_id, department_id), this request supplies both components. A request containing only company_id = 7 is a different access requirement. Do not assume that a partial key obtains the same direct hash lookup as the complete combination. Review the real query mix before selecting the key.

Next, examine stability of size. You should be able to estimate the number of distinct keys, the number of rows associated with each, and their approximate storage requirements. “Stable” does not mean that updates are forbidden. It means the planned organization remains a reasonable fit as normal business activity changes the data.

Finally, consider whether the benefit matters to the application. A frequently executed lookup on a predictable data set deserves investigation. A rarely used administrative query may not justify changing the storage organization. The decision should reflect total workload cost, including loading, maintenance, and queries that do not use the chosen key.

Collisions, Overflow, and Uneven Groups

A hash collision occurs when different cluster-key values map to the same hash value. It does not make the keys equal and does not permit Oracle to return incorrect rows. The query predicate still determines which rows qualify. Collisions can, however, increase the amount of candidate data examined during a lookup.

Overflow is a related but different concern: the data assigned to a location may need more space than the initial allocation accommodates. Additional blocks can then be needed. Even with a reasonable distribution of hash values, a single department containing many more employees than the others can have a much larger storage footprint.

Consider a hypothetical organization with many departments of ten employees and one department of several thousand. An average group size hides the exceptional department. Ask how often that department is queried and whether it dominates response times. Test realistic large groups as well as typical groups; a favorable average does not describe the slowest important request.

These examples are planning scenarios, not benchmark results. Their purpose is to show why key counts alone are insufficient. Row width, group size, distribution, and expected growth all contribute to the storage and access pattern.

Understanding HASHKEYS and SIZE Before Creating Anything

The HASHKEYS clause identifies a hash cluster and controls its configured hash-value capacity. It is not a declaration that a table may contain only that many rows. Many rows may share a cluster key, and different keys may collide. Treat this parameter as part of the physical design rather than as a row-count limit or a uniqueness constraint.

The SIZE setting describes the estimated space associated with a hash value. For planning, consider the combined data that may be assigned there, including multiple rows and, where applicable, multiple tables. Choosing a value from the size of one row alone can seriously understate the requirement.

Allocating generously may reduce pressure on individual locations but also consume space that remains unused. Allocating too little can make overflow more likely. This tradeoff is why predictable data size matters. A hash cluster should be designed against an expected operating range, including a reasonable growth scenario, rather than only today's smallest sample.

Before progressing to the creation lesson, write down your assumptions: distinct keys, typical and large groups, representative row widths, available space, and forecast growth. These assumptions become inputs for testing. If they are too uncertain to estimate, a more flexible storage arrangement may be the better starting point.

When Other Storage Choices Fit Better

Range queries are a major warning sign. Hashing does not preserve the ordering of original key values, so a request for a range cannot simply walk successive hash locations as though they were sorted keys:

SELECT employee_id, employee_name, department_id
FROM employees
WHERE department_id BETWEEN 20 AND 40;

This does not imply that every such statement must use a particular plan. Other indexes and optimizer choices can affect execution. The narrower point is that the direct equality-lookup advantage of hashing does not automatically extend to a range predicate. If ranges dominate the workload, investigate ordered index access instead.

Rapid, unpredictable growth is another poor fit. A design chosen for a small reference collection may become inappropriate when that collection turns into a continuously expanding transaction history. Evaluate the expected lifetime of the storage design, not merely whether the initial data can be loaded.

Frequent full scans and sparse allocation also deserve attention. If applications regularly inspect most rows, quick access to one key may offer little benefit. Reserving substantial storage for future keys can be undesirable when current scans must work through a sparsely populated structure. Space efficiency and scan behavior belong in the same comparison as point lookup speed.

Key Datatypes and Hash Expressions

The cluster key does not have to be numeric. Supported character keys and composite keys are possible. The fact that hashing produces a numeric hash value does not mean the original business identifier must be a number. Choose a key according to the application's equality predicates and the supported Oracle definitions.

Do not generalize that statement to every datatype. Oracle's CREATE CLUSTER reference restricts cluster-key types, including LONG, LONG RAW, REF, collection types, LOB types, Any* types, and user-defined object types. The clustered table's corresponding key columns must match the cluster definition's datatype and size.

Oracle uses its internal hash function unless an applicable HASH IS expression is specified. That clause has SQL restrictions; it is not an invitation to call arbitrary application or PL/SQL functions. For this introductory decision, prefer understanding the normal mechanism before considering custom expressions. A custom expression does not remove the need to plan for distribution and growth.

A Practical Evaluation for Department Lookups

Suppose a personnel application repeatedly requests the employees of one department. Start by collecting representative statements and identifying how often they supply the complete proposed key. Include exceptions: employee-name searches, cross-department reports, and requests that sort or aggregate large portions of the table.

Build comparable test data for the current indexed design and the proposed hash-cluster design. Keep the business data and requested results equivalent. Include large departments, realistic character lengths, and a plausible future volume. Record the assumptions so that later measurements can be interpreted rather than treated as unexplained timing numbers.

Compare execution plans, logical accesses, physical reads, and elapsed time under representative cache conditions and concurrency. Check both successful lookups and searches for absent keys. Include the cost of inserting and updating data, along with any secondary indexes the application requires. A faster isolated lookup does not automatically mean a faster overall system.

Retain the design only if the measured benefit justifies its space and maintenance implications. If results depend on an unrealistically small sample or exclude important reporting queries, improve the experiment before making a production decision. No Oracle statements or performance tests have been executed as part of this lesson; the SQL illustrates the questions such an evaluation should answer.

Separate Business Rules from Physical Placement

A useful final check is to ask which parts of the design express business rules and which parts only help find rows. A department identifier may participate in a foreign key, and an employee identifier may be unique. Those rules remain necessary regardless of whether employees are stored in a heap table, an indexed cluster, or a hash cluster. Hashing a value is not a substitute for declaring the appropriate constraints.

Similarly, storing several employees near one another does not define their display order. If an application needs employees alphabetically, express that requirement with ORDER BY. Do not infer an ordering from the diagram, from an observed test result, or from the numeric appearance of the key. Physical proximity and a guaranteed SQL result order are separate properties.

This separation helps keep the evaluation fair. Compare designs that enforce the same rules and return the same requested information. Otherwise, a faster test may simply have omitted constraint checks, sorting, or columns that the real application needs. Write down the complete request first, then evaluate the access path that serves it. The goal is to improve a correct application operation while keeping its meaning intact.

Decision Checklist and Next Lesson

A hash cluster is a candidate when these answers support a predictable equality-driven access pattern. It remains a tradeoff to test, not a default upgrade for every indexed table. Continue to creating an Oracle hash cluster to connect the workload decision with the required database definitions.

Oracle 26ai References


SEMrush Software 6 SEMrush Banner 6