db_icon_text

INTERVAL partitioning has been introduced by Oracle as an extension of RANGE partitioning. There are few limitations like the fact that’s not supported at subpartition level and the partitioning key has to be a DATE or NUMBER but also some interesting advantages. When a table is partitioned by INTERVAL, in fact, partitions are created automatically as data is loaded into the table so we don’t have to bother creating anything in advance. We might, anyway, prefer to maintain our historical data in tables that are partitioned by RANGE. This because it’s common to keep inside the system a “rolling window” (e.g. last 90 days) of historical data meaning oldest partitions are dropped as soon as they become obsolete and with INTERVAL partitioning the last partition in the range section cannot be dropped. Moreover, in many processes it’s preferrable to have a full control of which partitions are created because data loaded into the system is not guaranteed to be “clean” and rows that don’t map to existing partitions have to be rejected.
After all these considerations, let’s have a look at what we can do to make the best out of both partitioning options (tested on 11g).
Let’s create a test table partitioned by RANGE and a couple of indexes

The partition created with the table holds data for 1st Jan 2017 so we can insert the following row

Obviously, being this table partitioned by RANGE, we can’t insert data for partitions which are not in place yet

Imagine now that we have a big amount of rows we want to load, data that has been cleansed so we are sure it contains only “RUN_DATE” values that we expect. How to make it fast without manually creating all needed partitions? The best option is to get advantage of INTERVAL partitioning…but wait, isn’t our table partitioned by RANGE? Yes, it is, but we can temporarily change that!

and now let’s insert our new rows without caring too much about the existing partitions

As we can see the rows have been inserted and Oracle took care of creating the proper partitions for them

Same happened to all the local indexes

Partition names have been generated by Oracle. Index partitions have the same name as the table partitions only if the indexes are on place when the new partition is created. If we add a new local index now, the names of index partitions for this one will be different.
With a small trick, by using “lock table partition”, we can actually manually create a new partition without even inserting data even if the partitioning is by INTERVAL

Before going on I want to show one very interesting and important thing. By querying the dictionary we can see that there is a flag showing which partitions have been created as interval

That’s correct because the table has been initially created with RANGE partitioning and one partition P20170101, Oracle knows that. This information is lost once we convert the table back to RANGE partitioning by setting the interval to null

Interestingly, if we convert again to INTERVAL

we can drop the oldest partition without any problem

This is a table partitioned by interval and we just dropped the last partition in the range section! Actually in this case we can drop any partition and this is possible because none of them is flagged as INTERVAL=YES. As soon as you create at least one new partition with INTERVAL=YES (meaning that Oracle creates it once you insert data) you will have again some restrictions on dropping partitions. To be more specific, you will be able to drop all old partitions till the one right before the newly created with INTERVAL=YES. In our example, after creating a new interval partition, you won’t be able to drop SYS_P91.
Let’s go back to our test and change partitioning to RANGE again

Now we have partitions and data but names of partitions have been generated by Oracle

Even if the partition names are not self-explanatory regarding the data they hold, we can always access the right partition by using the “partition for” syntax

Anyway, if we want to use a standard naming format for our partitions (in this example PYYYYMMDD) we can rename them with a simple “alter table mytab rename partition…”.
Here is a script that does it automatically and sets the correct partition name according to the HIGH_VALUE. This is done for index partitions as well

After executing it we finally have everything with the proper names

Hope you enjoyed this journey among RANGE-INTERVAL partitioning!

Share this post on Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *