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.

CHAPTER 13 ■ PARTITIONING 575<br />

If you continue this experiment up to 16 partitions, you would see the same effects for the<br />

ninth through the fifteenth partitions—a skewing of the data to the interior partitions, away<br />

from the edges, <strong>and</strong> then upon hitting the sixteenth partition you would see a flattening-out<br />

again. The same would be true again up to 32 partitions, <strong>and</strong> then 64, <strong>and</strong> so on. This example<br />

just points out the importance of using a power of two as the number of hash partitions.<br />

List Partitioning<br />

List partitioning was a new feature of <strong>Oracle</strong>9i Release 1. It provides the ability to specify in<br />

which partition a row will reside, based on discrete lists of values. It is often useful to be able<br />

to partition by some code, such as a state or region code. For example, you might want to pull<br />

together in a single partition all records for people in the states of Maine (ME), New Hampshire<br />

(NH), Vermont (VT), <strong>and</strong> Massachusetts (MA), since those states are located next to or<br />

near each other, <strong>and</strong> your application queries data by geographic region. Similarly, you might<br />

want to group together Connecticut (CT), Rhode Isl<strong>and</strong> (RI), <strong>and</strong> New York (NY).<br />

You cannot use a range partition, since the range for the first partition would be ME<br />

through VT, <strong>and</strong> the second range would be CT through RI. Those ranges overlap. You cannot<br />

use hash partitioning since you cannot control which partition any given row goes into; the<br />

built-in hash function provided by <strong>Oracle</strong> does that.<br />

With list partitioning, we can accomplish this custom partitioning scheme easily:<br />

ops$tkyte@ORA10G> create table list_example<br />

2 ( state_cd varchar2(2),<br />

3 data varchar2(20)<br />

4 )<br />

5 partition by list(state_cd)<br />

6 ( partition part_1 values ( 'ME', 'NH', 'VT', 'MA' ),<br />

7 partition part_2 values ( 'CT', 'RI', 'NY' )<br />

8 )<br />

9 /<br />

Table created.<br />

Figure 13-3 shows that <strong>Oracle</strong> will inspect the STATE_CD column <strong>and</strong>, based on its value,<br />

place the row into the correct partition.<br />

As we saw for range partitioning, if we try to insert a value that isn’t specified in the list<br />

partition, <strong>Oracle</strong> will raise an appropriate error back to the client application. In other words,<br />

a list partitioned table without a DEFAULT partition will implicitly impose a constraint much<br />

like a check constraint on the table:<br />

ops$tkyte@ORA10G> insert into list_example values ( 'VA', 'data' );<br />

insert into list_example values ( 'VA', 'data' )<br />

*<br />

ERROR at line 1:<br />

ORA-14400: inserted partition key does not map to any partition

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

Saved successfully!

Ooh no, something went wrong!