05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 10 ■ DATABASE TABLES 389<br />

Creating a sorted hash cluster is much the same as the other clusters. To set up a sorted<br />

hash cluster capable of storing the above data, we could use the following:<br />

ops$tkyte@ORA10G> CREATE CLUSTER shc<br />

2 (<br />

3 cust_id NUMBER,<br />

4 order_dt timestamp SORT<br />

5 )<br />

6 HASHKEYS 10000<br />

7 HASH IS cust_id<br />

8 SIZE 8192<br />

9 /<br />

Cluster created.<br />

We’ve introduced a new keyword here: SORT. When we created the cluster, we identified<br />

the HASH IS CUST_ID <strong>and</strong> we added an ORDER_DT of type timestamp with the keyword SORT.<br />

This means the data will be located by CUST_ID (where CUST_ID=:X) <strong>and</strong> physically retrieved<br />

<strong>and</strong> sorted by ORDER_DT. Technically, it really means we’ll store some data that will be retrieved<br />

via a NUMBER column <strong>and</strong> sorted by the TIMESTAMP. The column names here are not relevant, as<br />

they were not in the B*Tree or HASH clusters, but convention would have us name them after<br />

what they represent.<br />

The CREATE TABLE for our CUST_ORDERS would look like this:<br />

ops$tkyte@ORA10G> CREATE TABLE cust_orders<br />

2 ( cust_id number,<br />

3 order_dt timestamp SORT,<br />

4 order_number number,<br />

5 username varchar2(30),<br />

6 ship_addr number,<br />

7 bill_addr number,<br />

8 invoice_num number<br />

9 )<br />

10 CLUSTER shc ( cust_id, order_dt )<br />

11 /<br />

Table created.<br />

We’ve mapped the CUST_ID column of this table to the hash key for the sorted hash cluster<br />

<strong>and</strong> the ORDER_DT column to the SORT column. We can observe using AUTOTRACE in SQL*Plus<br />

that the normal sort operations we expect are missing when accessing the sorted hash cluster:<br />

ops$tkyte@ORA10G> set autotrace traceonly explain<br />

ops$tkyte@ORA10G> variable x number<br />

ops$tkyte@ORA10G> select cust_id, order_dt, order_number<br />

2 from cust_orders<br />

3 where cust_id = :x<br />

4 order by order_dt;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!