05.11.2015 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

570<br />

CHAPTER 13 ■ PARTITIONING<br />

9 (to_date('01/01/2006','dd/mm/yyyy'))<br />

10 PARTITION part_3 VALUES LESS THAN<br />

11 (MAXVALUE)<br />

12 )<br />

13 /<br />

Table created.<br />

Now when you insert a row into that table, it will go into one of the three partitions—no<br />

row will be rejected, since partition PART_3 can take any value of RANGE_KEY_COLUMN that doesn’t<br />

go into PART_1 or PART_2 (even null values of the RANGE_KEY_COLUMN will be inserted into this<br />

new partition).<br />

Hash Partitioning<br />

When hash partitioning a table, <strong>Oracle</strong> will apply a hash function to the partition key to determine<br />

in which of the N partitions the data should be placed. <strong>Oracle</strong> recommends that N be a<br />

number that is a power of 2 (2, 4, 8, 16, <strong>and</strong> so on) to achieve the best overall distribution, <strong>and</strong><br />

we’ll see shortly that this is absolutely good advice.<br />

How Hash Partitioning Works<br />

Hash partitioning is designed to achieve a good spread of data across many different devices<br />

(disks), or just to segregate data out into more manageable chunks. The hash key chosen for a<br />

table should be a column or set of columns that are unique, or at least have as many distinct<br />

values as possible to provide for a good spread of the rows across partitions. If you choose a<br />

column that has only four values, <strong>and</strong> you use two partitions, then all the rows could quite<br />

easily end up hashing to the same partition, obviating the goal of partitioning in the first place!<br />

We will create a hash table with two partitions in this case. We will use a column named<br />

HASH_KEY_COLUMN as our partition key. <strong>Oracle</strong> will take the value in this column <strong>and</strong> determine<br />

the partition this row will be stored in by hashing that value:<br />

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

2 ( hash_key_column date,<br />

3 data varchar2(20)<br />

4 )<br />

5 PARTITION BY HASH (hash_key_column)<br />

6 ( partition part_1 tablespace p1,<br />

7 partition part_2 tablespace p2<br />

8 )<br />

9 /<br />

Table created.<br />

Figure 13-2 shows that <strong>Oracle</strong> will inspect the value in the HASH_KEY_COLUMN, hash it, <strong>and</strong><br />

determine which of the two partitions a given row will appear in:

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

Saved successfully!

Ooh no, something went wrong!