Despite the title and content, this article is more about thinking and testing than it is about list partitions as such. It’s just based on a little thought experiment with list partitioning.
I have a list-partitioned table, with no indexes, defined as follows:
create table area_sales ( area varchar2(10) not null, dated date not null, quantity number(6), value number(8,2), padding char(100), constraint as_area_ck check (area in ('England','Ireland','Scotland','Wales')) ) partition by list (area) ( partition england values ('England'), partition ireland values ('Ireland'), partition scotland values ('Scotland'), partition wales values ('Wales') ) ;
You’ll notice that there is no default partition, the partitioning column is declared as not null and is limited by a check constraint to a very specific set of values, and that every partition is defined to hold exactly one of the legal partition key values. Given those restrictions you might like to think about which of the following queries will be able to do perfect partition elimination:
select count(*) from area_sales where area = 'England' ; select count(*) from area_sales where area in ( 'England', 'Ireland', 'Wales') ; select count(*) from area_sales where area != 'England' ; select count(*) from area_sales where area not in ( 'England', 'Ireland', 'Wales') ; select count(*) from area_sales where area between 'England' and 'Ireland' ;
Once you’ve thought about what’s likely to happen, you might want to create and populate the table with a few thousand rows and run some tests to see if your thoughts were correct. Here’s a simple statement to spread some data evenly through the tables:
insert into area_sales select decode(mod(rownum,4), 0,'England', 1,'Ireland', 2,'Scotland', 3,'Wales' ), sysdate + 0.01 * rownum, rownum, rownum, rownum from all_objects where rownum <= 30000 ;
Once you’ve done the tests, and satisfied yourself that you understand what’s going on, you might start thinking about a few variations that might make the behaviour change, such as:
- you add a default partition
you add a default partition and remove the check constraint
you add a default partition and remove the not null constraint
you define partitions that hold multiple values
you change the order of partitions so the key values don’t appear in alphabetical order
you use a different version of Oracle
I’m not going to tell you the answers to these questions. The point I want to make is that this is the type of model, and these are the types of question you should ask yourself when you start to investigate a feature to see whether it will behave as well as you hope, and what special benefits you might get from it.
In fact, these are the questions I’m asking myself right now as I look at a system which is using list-partitioned tables to hold (lots of) sales data. I think it might be a good idea to restructure the table to disallow nulls, include a check constraint, and hold just one key value per partition – and then modify some of the code to get rid of “not in” and “!=” predicates.
The trouble is it’s a massive table with lots of indexes, the number of partitions would jump from eight to 40, and the number of times that rows would migrate due to partition key updates would increase; so the cost of restructuring would be large and there would be some fairly undesirable impact on DML that would offset the benefits of more efficient queries.