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

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 13 ■ PARTITIONING 567 Table Partitioning Schemes There are currently four methods by which you can partition tables in Oracle: • Range partitioning: You may specify ranges of data that should be stored together. For example, everything that has a timestamp within the month of Jan-2005 will be stored in partition 1, everything with a timestamp within Feb-2005 will be stored in partition 2, and so on. This is probably the most commonly used partitioning mechanism in Oracle. • Hash partitioning: You saw this in the first example in this chapter. A column (or columns) has a hash function applied to it, and the row will be placed into a partition according to the value of this hash. • List partitioning: You specify a discrete set of values, which determines the data that should be stored together. For example, you could specify that rows with a STATUS column value in ( 'A', 'M', 'Z' ) go into partition 1, those with a STATUS value in ( 'D', 'P', 'Q' ) go into partition 2, and so on. • Composite partitioning: This is a combination of range and hash or range and list. It allows you to first apply range partitioning to some data, and then within that range have the final partition be chosen by hash or list. In the following sections, we’ll look at the benefits of each type of partitioning and at the differences between them. We’ll also look at when to apply which schemes to different application types. This section is not intended to present a comprehensive demonstration of the syntax of partitioning and all of the available options. Rather, the examples are simple and illustrative, and designed to give you an overview of how partitioning works and how the different types of partitioning are designed to function. ■Note For full details on partitioning syntax, I refer you to either the Oracle SQL Reference Guide or to the Oracle Administrator’s Guide. Additionally, the Oracle Data Warehousing Guide is an excellent source of information on the partitioning options and is a must-read for anyone planning to implement partitioning. Range Partitioning The first type we will look at is a range partitioned table. The following CREATE TABLE statement creates a range partitioned table using the column RANGE_KEY_COLUMN. All data with a RANGE_ KEY_COLUMN strictly less than 01-JAN-2005 will be placed into the partition PART_1, and all data with a value strictly less than 01-JAN-2006 will go into partition PART_2. Any data not satisfying either of those conditions (e.g., a row with a RANGE_KEY_COLUMN value of 01-JAN-2007) will fail upon insertion, as it cannot be mapped to a partition: ops$tkyte@ORA10GR1> CREATE TABLE range_example 2 ( range_key_column date , 3 data varchar2(20) 4 ) 5 PARTITION BY RANGE (range_key_column)

568 CHAPTER 13 ■ PARTITIONING 6 ( PARTITION part_1 VALUES LESS THAN 7 (to_date('01/01/2005','dd/mm/yyyy')), 8 PARTITION part_2 VALUES LESS THAN 9 (to_date('01/01/2006','dd/mm/yyyy')) 10 ) 11 / Table created. ■Note We are using the date format DD/MM/YYYY in the CREATE TABLE to make this “international.” If we used a format of DD-MON-YYYY, then the CREATE TABLE would fail with ORA-01843: not a valid month if the abbreviation of January was not Jan on your system. The NLS_LANGUAGE setting would affect this. I have used the three-character month abbreviation in the text and inserts, however, to avoid any ambiguity as to which component is the day and which is the month. Figure 13-1 shows that Oracle will inspect the value of the RANGE_KEY_COLUMN and, based on that value, insert it into one of the two partitions. Figure 13-1. Range partition insert example The rows inserted were specifically chosen with the goal of demonstrating that the partition range is strictly less than and not less than or equal to. We first insert the value 15-DEC-2004, which will definitely go into partition PART_1. We also insert a row with a date/time that is one second before 01-JAN-2005—that row will also will go into partition PART_1 since

568<br />

CHAPTER 13 ■ PARTITIONING<br />

6 ( PARTITION part_1 VALUES LESS THAN<br />

7 (to_date('01/01/2005','dd/mm/yyyy')),<br />

8 PARTITION part_2 VALUES LESS THAN<br />

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

10 )<br />

11 /<br />

Table created.<br />

■Note We are using the date format DD/MM/YYYY in the CREATE TABLE to make this “international.” If we<br />

used a format of DD-MON-YYYY, then the CREATE TABLE would fail with ORA-01843: not a valid month<br />

if the abbreviation of January was not Jan on your system. The NLS_LANGUAGE setting would affect this.<br />

I have used the three-character month abbreviation in the text <strong>and</strong> inserts, however, to avoid any ambiguity<br />

as to which component is the day <strong>and</strong> which is the month.<br />

Figure 13-1 shows that <strong>Oracle</strong> will inspect the value of the RANGE_KEY_COLUMN <strong>and</strong>, based<br />

on that value, insert it into one of the two partitions.<br />

Figure 13-1. Range partition insert example<br />

The rows inserted were specifically chosen with the goal of demonstrating that the<br />

partition range is strictly less than <strong>and</strong> not less than or equal to. We first insert the value<br />

15-DEC-2004, which will definitely go into partition PART_1. We also insert a row with a date/time<br />

that is one second before 01-JAN-2005—that row will also will go into partition PART_1 since

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

Saved successfully!

Ooh no, something went wrong!