Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
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.
- Page 558 and 559: CHAPTER 12 ■ DATATYPES 513 ■Not
- Page 560 and 561: CHAPTER 12 ■ DATATYPES 515 Coping
- Page 562 and 563: CHAPTER 12 ■ DATATYPES 517 Note t
- Page 564 and 565: CHAPTER 12 ■ DATATYPES 519 We are
- Page 566 and 567: CHAPTER 12 ■ DATATYPES 521 Format
- Page 568 and 569: CHAPTER 12 ■ DATATYPES 523 ops$tk
- Page 570 and 571: CHAPTER 12 ■ DATATYPES 525 You ca
- Page 572 and 573: CHAPTER 12 ■ DATATYPES 527 month
- Page 574 and 575: CHAPTER 12 ■ DATATYPES 529 DT2-DT
- Page 576 and 577: CHAPTER 12 ■ DATATYPES 531 DT TS
- Page 578 and 579: CHAPTER 12 ■ DATATYPES 533 ops$tk
- Page 580 and 581: CHAPTER 12 ■ DATATYPES 535 Since
- Page 582 and 583: CHAPTER 12 ■ DATATYPES 537 ops$tk
- Page 584 and 585: CHAPTER 12 ■ DATATYPES 539 ops$tk
- Page 586 and 587: CHAPTER 12 ■ DATATYPES 541 suppor
- Page 588 and 589: CHAPTER 12 ■ DATATYPES 543 Concep
- Page 590 and 591: CHAPTER 12 ■ DATATYPES 545 We can
- Page 592 and 593: CHAPTER 12 ■ DATATYPES 547 buffer
- Page 594 and 595: CHAPTER 12 ■ DATATYPES 549 Note t
- Page 596 and 597: CHAPTER 12 ■ DATATYPES 551 13 dbm
- Page 598 and 599: CHAPTER 12 ■ DATATYPES 553 equall
- Page 600 and 601: CHAPTER 12 ■ DATATYPES 555 ROWID/
- Page 602 and 603: CHAPTER 13 ■ ■ ■ Partitioning
- Page 604 and 605: CHAPTER 13 ■ PARTITIONING 559 6 (
- Page 606 and 607: CHAPTER 13 ■ PARTITIONING 561 els
- Page 610 and 611: CHAPTER 13 ■ PARTITIONING 565 Enh
- Page 612 and 613: CHAPTER 13 ■ PARTITIONING 567 Tab
- Page 614 and 615: CHAPTER 13 ■ PARTITIONING 569 tha
- Page 616 and 617: CHAPTER 13 ■ PARTITIONING 571 PAR
- Page 618 and 619: CHAPTER 13 ■ PARTITIONING 573 35
- Page 620 and 621: CHAPTER 13 ■ PARTITIONING 575 If
- Page 622 and 623: CHAPTER 13 ■ PARTITIONING 577 We
- Page 624 and 625: CHAPTER 13 ■ PARTITIONING 579 14
- Page 626 and 627: CHAPTER 13 ■ PARTITIONING 581 ops
- Page 628 and 629: CHAPTER 13 ■ PARTITIONING 583 In
- Page 630 and 631: CHAPTER 13 ■ PARTITIONING 585 ops
- Page 632 and 633: CHAPTER 13 ■ PARTITIONING 587 | S
- Page 634 and 635: CHAPTER 13 ■ PARTITIONING 589 12
- Page 636 and 637: CHAPTER 13 ■ PARTITIONING 591 ops
- Page 638 and 639: CHAPTER 13 ■ PARTITIONING 593 •
- Page 640 and 641: CHAPTER 13 ■ PARTITIONING 595 Now
- Page 642 and 643: CHAPTER 13 ■ PARTITIONING 597 the
- Page 644 and 645: CHAPTER 13 ■ PARTITIONING 599 imp
- Page 646 and 647: CHAPTER 13 ■ PARTITIONING 601 OLT
- Page 648 and 649: CHAPTER 13 ■ PARTITIONING 603 5 s
- Page 650 and 651: CHAPTER 13 ■ PARTITIONING 605 Sur
- Page 652 and 653: CHAPTER 13 ■ PARTITIONING 607 On
- Page 654 and 655: CHAPTER 13 ■ PARTITIONING 609 Row
- Page 656 and 657: CHAPTER 13 ■ PARTITIONING 611 So,
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.