Overview of Table Clusters
A table cluster is a group of tables that share common columns and store related data in the same blocks. When tables are clustered, a single data block can contain rows from multiple tables. For example, a block can store rows from both the employees
and departments
tables rather than from only a single table.
The cluster key is the column or columns that the clustered tables have in common. For example, the employees
and departments
tables share the department_id
column. You specify the cluster key when creating the table cluster and when creating every table added to the table cluster.
The cluster key value is the value of the cluster key columns for a particular set of rows. All data that contains the same cluster key value, such as department_id=20
, is physically stored together. Each cluster key value is stored only once in the cluster and the cluster index, no matter how many rows of different tables contain the value.
For an analogy, suppose an HR manager has two book cases: one with boxes of employees folders and the other with boxes of departments folders. Users often ask for the folders for all employees in a particular department. To make retrieval easier, the manager rearranges all the boxes in a single book case. She divides the boxes by department ID. Thus, all folders for employees in department 20 and the folder for department 20 itself are in one box; the folders for employees in department 100 and the folder for department 100 are in a different box, and so on.
You can consider clustering tables when they are primarily queried (but not modified) and records from the tables are frequently queried together or joined. Because table clusters store related rows of different tables in the same data blocks, properly used table clusters offer the following benefits over nonclustered tables:
-
Disk I/O is reduced for joins of clustered tables.
-
Access time improves for joins of clustered tables.
-
Less storage is required to store related table and index data because the cluster key value is not stored repeatedly for each row.
Typically, clustering tables is not appropriate in the following situations:
-
The tables are frequently updated.
-
The tables frequently require a full table scan.
-
The tables require truncating.
Overview of Indexed Clusters
An indexed cluster is a table cluster that uses an index to locate data. The cluster index is a B-tree index on the cluster key. A cluster index must be created before any rows can be inserted into clustered tables.
Assume that you create the cluster employees_departments_cluster
with the cluster key department_id
, as shown in Example 2-8. Because the HASHKEYS
clause is not specified, this cluster is an indexed cluster. Afterward, you create an index named idx_emp_dept_cluster
on this cluster key.
CREATE CLUSTER employees_departments_cluster (department_id NUMBER(4)) SIZE 512; CREATE INDEX idx_emp_dept_cluster ON CLUSTER employees_departments_cluster;
You then create the employees
and departments
tables in the cluster, specifying the department_id
column as the cluster key, as follows (the ellipses mark the place where the column specification goes):
CREATE TABLE employees ( ... ) CLUSTER employees_departments_cluster (department_id); CREATE TABLE departments ( ... ) CLUSTER employees_departments_cluster (department_id);
Finally, you add rows to the employees
and departments
tables. The database physically stores all rows for each department from the employees
and departments
tables in the same data blocks. The database stores the rows in a heap and locates them with the index.
Figure 2-6 shows the employees_departments_cluster
table cluster, which contains employees
and departments
. The database stores rows for employees in department 20 together, department 110 together, and so on. If the tables are not clustered, then the database does not ensure that the related rows are stored together.
The B-tree cluster index associates the cluster key value with the database block address (DBA) of the block containing the data. For example, the index entry for key 20 shows the address of the block that contains data for employees in department 20:
20,AADAAAA9d
The cluster index is separately managed, just like an index on a nonclustered table, and can exist in a separate tablespace from the table cluster.
Overview of Hash Clusters
A hash cluster is like an indexed cluster, except the index key is replaced with a hash function. No separate cluster index exists. In a hash cluster, the data is the index.
With an indexed table or indexed cluster, Oracle Database locates table rows using key values stored in a separate index. To find or store a row in an indexed table or table cluster, the database must perform at least two I/Os:
-
One or more I/Os to find or store the key value in the index
-
Another I/O to read or write the row in the table or table cluster
To find or store a row in a hash cluster, Oracle Database applies the hash function to the cluster key value of the row. The resulting hash value corresponds to a data block in the cluster, which the database reads or writes on behalf of the issued statement.
Hashing is an optional way of storing table data to improve the performance of data retrieval. Hash clusters may be beneficial when the following conditions are met:
-
A table is queried much more often than modified.
-
The hash key column is queried frequently with equality conditions, for example,
WHERE department_id=20
. For such queries, the cluster key value is hashed. The hash key value points directly to the disk area that stores the rows. -
You can reasonably guess the number of hash keys and the size of the data stored with each key value.
Hash Cluster Creation
The cluster key, like the key of an indexed cluster, is a single column or composite key shared by the tables in the cluster. The hash key values are actual or possible values inserted into the cluster key column. For example, if the cluster key is department_id
, then hash key values could be 10, 20, 30, and so on.
Oracle Database uses a hash function that accepts an infinite number of hash key values as input and sorts them into a finite number of buckets. Each bucket has a unique numeric ID known as a hash value. Each hash value maps to the database block address for the block that stores the rows corresponding to the hash key value (department 10, 20, 30, and so on).
To create a hash cluster, you use the same CREATE CLUSTER
statement as for an indexed cluster, with the addition of a hash key. The number of hash values for the cluster depends on the hash key. In Example 2-9, the number of departments that are likely to exist is 100, so HASHKEYS
is set to 100
.
Example 2-9 Hash Cluster
CREATE CLUSTER employees_departments_cluster (department_id NUMBER(4)) SIZE 8192 HASHKEYS 100;
After you create employees_departments_cluster
, you can create the employees
and departments
tables in the cluster. You can then load data into the hash cluster just as in the indexed cluster described in Example 2-8.
Hash Cluster Queries
The database, not the user, determines how to hash the key values input by the user. For example, assume that users frequently execute queries such as the following, entering different department ID numbers for p_id
:
SELECT * FROM employees WHERE department_id = :p_id; SELECT * FROM departments WHERE department_id = :p_id; SELECT * FROM employees e, departments d WHERE e.department_id = d.department_id AND d.department_id = :p_id;
If a user queries employees in department_id
=20
, then the database might hash this value to bucket 77. If a user queries employees in department_id
=10
, then the database might hash this value to bucket 15. The database uses the internally generated hash value to locate the block that contains the employee rows for the requested department.
Figure 2-7 depicts a hash cluster segment as a horizontal row of blocks. As shown in the graphic, a query can retrieve data in a single I/O.
Figure 2-7 Retrieving Data from a Hash Cluster
Description of "Figure 2-7 Retrieving Data from a Hash Cluster"
A limitation of hash clusters is the unavailability of range scans on nonindexed cluster keys (see "Index Range Scan"). Assume that no separate index exists for the hash cluster created in Example 2-9. A query for departments with IDs between 20 and 100 cannot use the hashing algorithm because it cannot hash every possible value between 20 and 100. Because no index exists, the database must perform a full scan.
Hash Cluster Variations
single-table hash cluster is an optimized version of a hash cluster that supports only one table at a time. A one-to-one mapping exists between hash keys and rows. A single-table hash cluster can be beneficial when users require rapid access to a table by primary key. For example, users often look up an employee record in theemployees
table by
employee_id
.
A sorted hash cluster stores the rows corresponding to each value of the hash function in such a way that the database can efficiently return them in sorted order. The database performs the optimized sort internally. For applications that always consume data in sorted order, this technique can mean faster retrieval of data. For example, an application might always sort on the order_date
column of the orders
table.
Hash Cluster Storage
Oracle Database allocates space for a hash cluster differently from an indexed cluster. In Example 2-9, HASHKEYS
specifies the number of departments likely to exist, whereas SIZE
specifies the size of the data associated with each department. The database computes a storage space value based on the following formula:
HASHKEYS * SIZE / database_block_size
Thus, if the block size is 4096 bytes in Example 2-9, then the database allocates at least 200 blocks to the hash cluster.
Oracle Database does not limit the number of hash key values that you can insert into the cluster. For example, even though HASHKEYS
is 100
, nothing prevents you from inserting 200 unique departments in the departments
table. However, the efficiency of the hash cluster retrieval diminishes when the number of hash values exceeds the number of hash keys.
To illustrate the retrieval issues, assume that block 100 in Figure 2-7 is completely full with rows for department 20. A user inserts a new department with department_id
43 into the departments
table. The number of departments exceeds the HASHKEYS
value, so the database hashes department_id
43 to hash value 77, which is the same hash value used for department_id
20. Hashing multiple input values to the same output value is called hash collision.
When users insert rows into the cluster for department 43, the database cannot store these rows in block 100, which is full. The database links block 100 to a new overflow block, say block 200, and stores the inserted rows in the new block. Both block 100 and 200 are now eligible to store data for either department. As shown in Figure 2-8, a query of either department 20 or 43 now requires two I/Os to retrieve the data: block 100 and its associated block 200. You can solve this problem by re-creating the cluster with a different HASHKEYS
value.
Figure 2-8 Retrieving Data from a Hash Cluster When a Hash Collision Occurs
Description of "Figure 2-8 Retrieving Data from a Hash Cluster When a Hash Collision Occurs"
Specify the Space Required by an Average Cluster Key and Its Associated Rows
The CREATE CLUSTER
statement has an optional clause, SIZE
, which is the estimated number of bytes required by an average cluster key and its associated rows. The database uses the SIZE
parameter when performing the following tasks:
-
Estimating the number of cluster keys (and associated rows) that can fit in a clustered data block
-
Limiting the number of cluster keys placed in a clustered data block. This maximizes the storage efficiency of keys within a cluster.
SIZE
does not limit the space that can be used by a given cluster key. For example, if SIZE
is set such that two cluster keys can fit in one data block, any amount of the available data block space can still be used by either of the cluster keys.
By default, the database stores only one cluster key and its associated rows in each data block of the cluster data segment. Although block size can vary from one operating system to the next, the rule of one key for each block is maintained as clustered tables are imported to other databases on other systems.
If all the rows for a given cluster key value cannot fit in one block, the blocks are chained together to speed access to all the values with the given key. The cluster index points to the beginning of the chain of blocks, each of which contains the cluster key value and associated rows. If the cluster SIZE
is such that multiple keys fit in a block, then blocks can belong to multiple chains.
Specify the Space Required by an Average Cluster Key and Its Associated Rows
The CREATE CLUSTER
statement has an optional clause, SIZE
, which is the estimated number of bytes required by an average cluster key and its associated rows. The database uses the SIZE
parameter when performing the following tasks:
-
Estimating the number of cluster keys (and associated rows) that can fit in a clustered data block
-
Limiting the number of cluster keys placed in a clustered data block. This maximizes the storage efficiency of keys within a cluster.
SIZE
does not limit the space that can be used by a given cluster key. For example, if SIZE
is set such that two cluster keys can fit in one data block, any amount of the available data block space can still be used by either of the cluster keys.
By default, the database stores only one cluster key and its associated rows in each data block of the cluster data segment. Although block size can vary from one operating system to the next, the rule of one key for each block is maintained as clustered tables are imported to other databases on other systems.
If all the rows for a given cluster key value cannot fit in one block, the blocks are chained together to speed access to all the values with the given key. The cluster index points to the beginning of the chain of blocks, each of which contains the cluster key value and associated rows. If the cluster SIZE
is such that multiple keys fit in a block, then blocks can belong to multiple chains.
Estimate Cluster Size and Set Storage Parameters
The following are benefits of estimating cluster size before creating the cluster:
-
You can use the combined estimated size of clusters, along with estimates for indexes and redo log files, to determine the amount of disk space that is required to hold an intended database. From these estimates, you can make correct hardware purchases and other decisions.
-
You can use the estimated size of an individual cluster to better manage the disk space that the cluster will use. When a cluster is created, you can set appropriate storage parameters and improve I/O performance of applications that use the cluster.
Set the storage parameters for the data segments of a cluster using the STORAGE
clause of the CREATE
CLUSTER
or ALTER
CLUSTER
statement, rather than the individual CREATE
or ALTER
statements that put tables into the cluster. Storage parameters specified when creating or altering a clustered table are ignored. The storage parameters set for the cluster override the table storage parameters.
Altering Clustered Tables
You can alter clustered tables using theALTER TABLE
statement. However, any data block space parameters, transaction entry parameters, or storage parameters you set in an
ALTER TABLE
statement for a clustered table generate an error message (
ORA-01771, illegal option for a clustered table
). The database uses the parameters of the cluster for all clustered tables. Therefore, you can use the
ALTER TABLE
statement only to add or modify columns, drop non-cluster-key columns, or add, drop, enable, or disable integrity constraints or triggers for a clustered table.
Creating Hash Clusters
You create a hash cluster using a CREATE CLUSTER
statement, but you specify a HASHKEYS
clause. The following statement creates a cluster named trial_cluster
, clustered by the trialno
column (the cluster key):
CREATE CLUSTER trial_cluster ( trialno NUMBER(5,0) ) TABLESPACE users STORAGE ( INITIAL 250K NEXT 50K MINEXTENTS 1 MAXEXTENTS 3 PCTINCREASE 0 ) HASH IS trialno HASHKEYS 150;
The following statement creates the trial
table in the trial_cluster
hash cluster:
CREATE TABLE trial ( trialno NUMBER(5,0) PRIMARY KEY, ... ) CLUSTER trial_cluster (trialno);
As with index clusters, the key of a hash cluster can be a single column or a composite key (multiple column key). In the preceding example, the key is the trialno
column.
The HASHKEYS
value, in this case 150
, specifies and limits the number of unique hash values that the hash function can generate. The database rounds the number specified to the nearest prime number.
If no HASH IS
clause is specified, then the database uses an internal hash function. If the cluster key is already a unique identifier that is uniformly distributed over its range, then you can bypass the internal hash function and specify the cluster key as the hash value, as in the preceding example. You can also use the HASH IS
clause to specify a user-defined hash function.
You cannot create a cluster index on a hash cluster, and you need not create an index on a hash cluster key.
Controlling Space Use Within a Hash Cluster
When creating a hash cluster, it is important to choose the cluster key correctly and set the HASH IS
, SIZE
, and HASHKEYS
parameters so that performance and space use are optimal. The following guidelines describe how to set these parameters.
Choosing the Key
Choosing the correct cluster key is dependent on the most common types of queries issued against the clustered tables. For example, consider the emp
table in a hash cluster. If queries often select rows by employee number, the empno
column should be the cluster key. If queries often select rows by department number, the deptno
column should be the cluster key. For hash clusters that contain a single table, the cluster key is typically the entire primary key of the contained table.
The key of a hash cluster, like that of an index cluster, can be a single column or a composite key (multiple column key). A hash cluster with a composite key must use the internal hash function of the database.
Setting HASH IS
Specify the HASH IS
parameter only if the cluster key is a single column of the NUMBER
data type, and contains uniformly distributed integers. If these conditions apply, you can distribute rows in the cluster so that each unique cluster key value hashes, with no collisions (two cluster key values having the same hash value), to a unique hash value. If these conditions do not apply, omit this clause so that you use the internal hash function.
Setting SIZE
SIZE
should be set to the average amount of space required to hold all rows for any given hash key. Therefore, to properly determine SIZE
, you must be aware of the characteristics of your data:
-
If the hash cluster is to contain only a single table and the hash key values of the rows in that table are unique (one row for each value),
SIZE
can be set to the average row size in the cluster. -
If the hash cluster is to contain multiple tables,
SIZE
can be set to the average amount of space required to hold all rows associated with a representative hash value.
Further, once you have determined a (preliminary) value for SIZE
, consider the following. If the SIZE
value is small (more than four hash keys can be assigned for each data block) you can use this value for SIZE
in the CREATE CLUSTER
statement. However, if the value of SIZE
is large (four or fewer hash keys can be assigned for each data block), then you should also consider the expected frequency of collisions and whether performance of data retrieval or efficiency of space usage is more important to you.
-
If the hash cluster does not use the internal hash function (if you specified
HASH IS
) and you expect few or no collisions, you can use your preliminary value ofSIZE
. No collisions occur and space is used as efficiently as possible. -
If you expect frequent collisions on inserts, the likelihood of overflow blocks being allocated to store rows is high. To reduce the possibility of overflow blocks and maximize performance when collisions are frequent, you should adjust
SIZE
as shown in the following chart.Available Space for each Block / Calculated SIZE Setting for SIZE 1 SIZE
2 SIZE
+ 15%3 SIZE
+ 12%4 SIZE
+ 8%>4 SIZE
Overestimating the value of
SIZE
increases the amount of unused space in the cluster. If space efficiency is more important than the performance of data retrieval, disregard the adjustments shown in the preceding table and use the original value forSIZE
.
Using Table Clusters for Performance
Follow these guidelines when deciding whether to cluster tables:
-
Cluster tables that are accessed frequently by the application in join statements.
-
Do not cluster tables if the application joins them only occasionally or modifies their common column values frequently. Modifying a row's cluster key value takes longer than modifying the value in an unclustered table, because Oracle Database might need to migrate the modified row to another block to maintain the cluster.
-
Do not cluster tables if the application often performs full table scans of only one of the tables. A full table scan of a clustered table can take longer than a full table scan of an unclustered table. Oracle Database is likely to read more blocks because the tables are stored together.
-
Cluster master-detail tables if you often select a master record and then the corresponding detail records. Detail records are stored in the same data block(s) as the master record, so they are likely still to be in memory when you select them, requiring Oracle Database to perform less I/O.
-
Store a detail table alone in a cluster if you often select many detail records of the same master. This measure improves the performance of queries that select detail records of the same master, but does not decrease the performance of a full table scan on the master table. An alternative is to use an index organized table.
-
Do not cluster tables if the data from all tables with the same cluster key value exceeds more than one or two data blocks. To access a row in a clustered table, Oracle Database reads all blocks containing rows with that value. If these rows take up multiple blocks, then accessing a single row could require more reads than accessing the same row in an unclustered table.
-
Do not cluster tables when the number of rows for each cluster key value varies significantly. This causes waste of space for the low cardinality key value; it causes collisions for the high cardinality key values. Collisions degrade performance.
Consider the benefits and drawbacks of clusters for the application. For example, you might decide that the performance gain for join statements outweighs the performance loss for statements that modify cluster key values. You might want to experiment and compare processing times with the tables both clustered and stored separately.
Using Hash Clusters for Performance
Follow these guidelines for choosing when to use hash clusters:-
Use hash clusters to store tables accessed frequently by SQL statements with
WHERE
clauses, if theWHERE
clauses contain equality conditions that use the same column or combination of columns. Designate this column or combination of columns as the cluster key. -
Store a table in a hash cluster if you can determine how much space is required to hold all rows with a given cluster key value, including rows to be inserted immediately and rows to be inserted in the future.
-
Use sorted hash clusters, where rows corresponding to each value of the hash function are sorted on a specific columns in ascending order, when the database can improve response time on operations with this sorted clustered data.
-
Do not store a table in a hash cluster if the application often performs full table scans and if you must allocate a great deal of space to the hash cluster in anticipation of the table growing. Such full table scans must read all blocks allocated to the hash cluster, even though some blocks might contain few rows. Storing the table alone reduces the number of blocks read by full table scans.
-
Do not store a table in a hash cluster if the application frequently modifies the cluster key values. Modifying a row's cluster key value can take longer than modifying the value in an unclustered table, because Oracle Database might need to migrate the modified row to another block to maintain the cluster.
Storing a single table in a hash cluster can be useful, regardless of whether the table is joined frequently with other tables, as long as hashing is appropriate for the table based on the considerations in this list.
Examples
Creating a Cluster: Example The following statement creates a cluster named personnel
with the cluster key column department
, a cluster size of 512 bytes, and storage parameter values:
CREATE CLUSTER personnel (department NUMBER(4)) SIZE 512 STORAGE (initial 100K next 50K);
Cluster Keys: Example The following statement creates the cluster index on the cluster key of personnel
:
CREATE INDEX idx_personnel ON CLUSTER personnel;
After creating the cluster index, you can add tables to the index and perform DML operations on those tables.
Adding Tables to a Cluster: Example The following statements create some departmental tables from the sample hr.employees
table and add them to the personnel cluster created in the earlier example:
CREATE TABLE dept_10 CLUSTER personnel (department_id) AS SELECT * FROM employees WHERE department_id = 10; CREATE TABLE dept_20 CLUSTER personnel (department_id) AS SELECT * FROM employees WHERE department_id = 20;
Hash Clusters: Examples The following statement creates a hash cluster named language
with the cluster key column cust_language
, a maximum of 10 hash key values, each of which is allocated 512 bytes, and storage parameter values:
CREATE CLUSTER language (cust_language VARCHAR2(3)) SIZE 512 HASHKEYS 10 STORAGE (INITIAL 100k next 50k);
Because the preceding statement omits the HASH
IS
clause, Oracle Database uses the internal hash function for the cluster.
The following statement creates a hash cluster named address
with the cluster key made up of the columns postal_code
and country_id
, and uses a SQL expression containing these columns for the hash function:
CREATE CLUSTER address (postal_code NUMBER, country_id CHAR(2)) HASHKEYS 20 HASH IS MOD(postal_code + country_id, 101);
Single-Table Hash Clusters: Example The following statement creates a single-table hash cluster named cust_orders
with the cluster key customer_id
and a maximum of 100 hash key values, each of which is allocated 512 bytes:
CREATE CLUSTER cust_orders (customer_id NUMBER(6)) SIZE 512 SINGLE TABLE HASHKEYS 100;
To drop a cluster that contains no tables, and its cluster index, use the DROP CLUSTER
statement. For example, the following statement drops the empty cluster named emp_dept
:
DROP CLUSTER emp_dept;
If the cluster contains one or more clustered tables and you intend to drop the tables as well, add the INCLUDING TABLES
clause of the DROP CLUSTER
statement, as follows:
DROP CLUSTER emp_dept INCLUDING TABLES;
If the INCLUDING TABLES
clause is not included and the cluster contains tables, an error is returned.
If one or more tables in a cluster contain primary or unique keys that are referenced by FOREIGN KEY
constraints of tables outside the cluster, the cluster cannot be dropped unless the dependent FOREIGN KEY
constraints are also dropped. This can be easily done using the CASCADE CONSTRAINTS
clause of the DROP CLUSTER
statement, as shown in the following example:
DROP CLUSTER emp_dept INCLUDING TABLES CASCADE CONSTRAINTS;
The database returns an error if you do not use the CASCADE CONSTRAINTS
clause and constraints exist.
Clusters Data Dictionary Views
The following views display information about hash clusters:
View | Description |
---|---|
DBA_CLUSTERS
| DBA view describes all clusters (including hash clusters) in the database. ALL view describes all clusters accessible to the user. USER view is restricted to clusters owned by the user. Some columns in these views contain statistics that are generated by the DBMS_STATS package or ANALYZE statement. |
DBA_CLU_COLUMNS
| These views map table columns to cluster columns. |
DBA_CLUSTER_HASH_EXPRESSIONS
| These views list hash functions for hash clusters. |
参考至:http://docs.oracle.com/cd/E11882_01/server.112/e25494/hash.htm#ADMIN11773
http://docs.oracle.com/cd/E11882_01/server.112/e25494/clustrs.htm#ADMIN11755
http://docs.oracle.com/cd/E11882_01/server.112/e40540/tablecls.htm#CNCPT89179
http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_5001.htm#SQLRF53808
本文原创,转载请注明出处、作者
如有错误,欢迎指正
邮箱:czmcj@163.com