Creating a virtually hashed table

To create a virtually hashed table, specify the maximum value for the hash region. This is the partial syntax for create table; the parameters for virtually hashed tables are shown in bold:

create table [database.[owner].]table_name 
. . .
             | {unique | primary key}
             using clustered
             (column_name [asc | desc] [{, column_name [asc | desc]}...])=
             (hash_factor [{, hash_factor}...])
                            with max num_hash_values key

where:

Determining values for hash_factor

You can keep the hash factor for the first key as 1. The hash factor for all the remaining key columns is greater than the maximum value of the previous key allowed in the hash region multiplied by its hash factor.

Adaptive Server allows tables with hash factors greater than 1 for the first key column to have fewer rows on a page. For example, if a table has a hash factor of 5 for the first key column, after every row in a page, space for the next four rows is kept empty. To support this, Adaptive Server requires five times the amount of table space.

If the value of a key column is greater than or equal to the hash factor of the next key column, the current row is inserted in the overflow clustered region to avoid collisions in the hash region.

For example, t is a virtually hashed table with key columns id and age, and corresponding hash factors of (10,1). Because the hash value for rows (5, 5) and (2, 35) is 55, this may result in a hash collision.

However, because the value 35 is greater than or equal to 10 (the hash factor for the next key column, id), Adaptive Server stores the second row in the overflow clustered region, avoiding collisions in the hash region.

In another example, if u is a virtually hashed table with a primary index and hash factors of (id1, id2, id3) = (125, 25, 5) and a max hash_value of 200:

This example illustrates how the number of rows in the hash region, row length, and the number of rows per page affect the page layout of hash and overflow regions. It creates a virtually hashed table named orders on the pubs2 database on the order_seg segment:

create table orders(
id int,
age int,
primary key using clustered (id,age) = (10,1) with max 1000 key)
on order_seg

The layout for the data is:

In this example:

Figure 11-2: Page layout for data

This example creates a virtually-hashed table named orders on the pubs2 database on the order_seg segment. The lay out for the data is that the order_seg segment starts on page ID 51200, the ID for the first data object allocation map page is 51201, the maximum rows per page is 168, the row size is 10, and the root index page of the overflow cllustered region is 51217.

For this page layout, the number of rows per page is 168. Since the hash factors for id and age are 10 and 1, respectively, the maximum value for column age that qualifies for the hash region is 9. The range of key values of (id and age) combination that qualify for the the hash region (1000 keys in total) is:

From these keys, the first 168 keys—(0, 0) to (16, 7)—are mapped to the first data page, 51208. The next range of 168 keys—(16, 8) to (33, 5)—are mapped to the second data page, 51209, and so on.