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 563 BIG1 and BIG2 are both about 1.5GB in size and each have 344MB free. We’ll try to rebuild the first table, BIG_TABLE1: ops$tkyte@ORA10GR1> alter table big_table1 move; alter table big_table1 move * ERROR at line 1: ORA-01652: unable to extend temp segment by 1024 in tablespace BIG1 That fails—we needed sufficient free space in tablespace BIG1 to hold an entire copy of BIG_TABLE1 at the same time as the old copy was there—in short, we would need about two times the storage for a short period (maybe more, maybe less—it depends on the resulting size of the rebuilt table). We now attempt the same operation on BIG_TABLE2: ops$tkyte@ORA10GR1> alter table big_table2 move; alter table big_table2 move * ERROR at line 1: ORA-14511: cannot perform operation on a partitioned object That is Oracle telling us we cannot do the MOVE operation on the “table”; we must perform the operation on each partition of the table instead. We can move (hence rebuild and reorganize) each partition one by one: ops$tkyte@ORA10GR1> alter table big_table2 move partition part_1; Table altered. ops$tkyte@ORA10GR1> alter table big_table2 move partition part_2; Table altered. ops$tkyte@ORA10GR1> alter table big_table2 move partition part_3; Table altered. ops$tkyte@ORA10GR1> alter table big_table2 move partition part_4; Table altered. ops$tkyte@ORA10GR1> alter table big_table2 move partition part_5; Table altered. ops$tkyte@ORA10GR1> alter table big_table2 move partition part_6; Table altered. ops$tkyte@ORA10GR1> alter table big_table2 move partition part_7; Table altered. ops$tkyte@ORA10GR1> alter table big_table2 move partition part_8; Table altered. Each individual move only needed sufficient free space to hold a copy of one-eighth of the data! Therefore, these commands succeeded given the same amount of free space as we had before. We needed significantly less temporary resources and, further, if the system failed (e.g., due to a power outage) after we moved PART_4 but before PART_5 finished “moving,” we would not have lost all of the work performed as we would have with a single MOVE statement. The first four partitions would still be “moved” when the system recovered, and we may resume processing at partition PART_5.

564 CHAPTER 13 ■ PARTITIONING Some may look at that and say, “Wow, eight statements—that is a lot of typing,” and it’s true that this sort of thing would be unreasonable if you had hundreds of partitions (or more). Fortunately, it is very easy to script a solution, and the previous would become simply ops$tkyte@ORA10GR1> begin 2 for x in ( select partition_name 3 from user_tab_partitions 4 where table_name = 'BIG_TABLE2' ) 5 loop 6 execute immediate 7 'alter table big_table2 move partition ' || 8 x.partition_name; 9 end loop; 10 end; 11 / PL/SQL procedure successfully completed. All of the information you need is there in the Oracle data dictionary, and most sites that have implemented partitioning also have a series of stored procedures they use to make managing large numbers of partitions easy. Additionally, many GUI tools such as Enterprise Manager have the built-in capability to perform these operations as well, without your needing to type in the individual commands. Another factor to consider with regard to partitions and administration is the use of “sliding windows” of data in data warehousing and archiving. In many cases, you need to keep data online that spans the last N units of time. For example, say you need to keep the last 12 months or the last 5 years online. Without partitions, this is generally a massive INSERT followed by a massive DELETE. Lots of DML, and lots of redo and undo generated. Now with partitions, you can simply do the following: 1. Load a separate table with the new months’ (or years’, or whatever) data. 2. Index the table fully. (These steps could even be done in another instance and transported to this database.) 3. Attach this newly loaded and indexed table onto the end of the partitioned table using a fast DDL command: ALTER TABLE EXCHANGE PARTITION. 4. Detach the oldest partition off the other end of the partitioned table. So, you can now very easily support extremely large objects containing time-sensitive information. The old data can easily be removed from the partitioned table and simply dropped if you do not need it, or it can be archived off elsewhere. New data can be loaded into a separate table, so as to not affect the partitioned table until the loading, indexing, and so on is complete. We will take a look at a complete example of a sliding window later in this chapter. In short, partitioning can make what would otherwise be daunting, or in some cases unfeasible, operations as easy as they are in a small database.

564<br />

CHAPTER 13 ■ PARTITIONING<br />

Some may look at that <strong>and</strong> say, “Wow, eight statements—that is a lot of typing,” <strong>and</strong> it’s<br />

true that this sort of thing would be unreasonable if you had hundreds of partitions (or more).<br />

Fortunately, it is very easy to script a solution, <strong>and</strong> the previous would become simply<br />

ops$tkyte@ORA10GR1> begin<br />

2 for x in ( select partition_name<br />

3 from user_tab_partitions<br />

4 where table_name = 'BIG_TABLE2' )<br />

5 loop<br />

6 execute immediate<br />

7 'alter table big_table2 move partition ' ||<br />

8 x.partition_name;<br />

9 end loop;<br />

10 end;<br />

11 /<br />

PL/SQL procedure successfully completed.<br />

All of the information you need is there in the <strong>Oracle</strong> data dictionary, <strong>and</strong> most sites that<br />

have implemented partitioning also have a series of stored procedures they use to make<br />

managing large numbers of partitions easy. Additionally, many GUI tools such as Enterprise<br />

Manager have the built-in capability to perform these operations as well, without your needing<br />

to type in the individual comm<strong>and</strong>s.<br />

Another factor to consider with regard to partitions <strong>and</strong> administration is the use of<br />

“sliding windows” of data in data warehousing <strong>and</strong> archiving. In many cases, you need to<br />

keep data online that spans the last N units of time. For example, say you need to keep the<br />

last 12 months or the last 5 years online. Without partitions, this is generally a massive INSERT<br />

followed by a massive DELETE. Lots of DML, <strong>and</strong> lots of redo <strong>and</strong> undo generated. Now with<br />

partitions, you can simply do the following:<br />

1. Load a separate table with the new months’ (or years’, or whatever) data.<br />

2. Index the table fully. (These steps could even be done in another instance <strong>and</strong> transported<br />

to this database.)<br />

3. Attach this newly loaded <strong>and</strong> indexed table onto the end of the partitioned table using<br />

a fast DDL comm<strong>and</strong>: ALTER TABLE EXCHANGE PARTITION.<br />

4. Detach the oldest partition off the other end of the partitioned table.<br />

So, you can now very easily support extremely large objects containing time-sensitive<br />

information. The old data can easily be removed from the partitioned table <strong>and</strong> simply<br />

dropped if you do not need it, or it can be archived off elsewhere. New data can be loaded into<br />

a separate table, so as to not affect the partitioned table until the loading, indexing, <strong>and</strong> so on<br />

is complete. We will take a look at a complete example of a sliding window later in this chapter.<br />

In short, partitioning can make what would otherwise be daunting, or in some cases<br />

unfeasible, operations as easy as they are in a small database.

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

Saved successfully!

Ooh no, something went wrong!