DB2 10.5 for Linux, UNIX, and Windows

Partitioned indexes on partitioned tables

A partitioned index is made up of a set of index partitions, each of which contains the index entries for a single data partition. Each index partition contains references only to data in its corresponding data partition. Both system- and user-generated indexes can be partitioned.

A partitioned index becomes beneficial if:

There are some types of indexes that cannot be partitioned: You must always create these indexes as nonpartitioned. In addition, the index key for partitioned unique indexes must include all columns from the table-partitioning key, whether they are user- or system-generated. The latter would be the case for indexes created by the system for enforcing unique or primary constraints on data.

Figure 1 shows an example of partitioned indexes.

Figure 1. Partitioned indexes that share a table space with data partitions of a table
Illustration of partitioned indexes that share a table space with the data partitions of a table.
In this example, all of the data partitions for table A and all of the index partitions for table A are in a single table space. The index partitions reference only the rows in the data partition with which they are associated. (Contrast a partitioned index with a nonpartitioned index, where the index references all rows across all data partitions). Also, index partitions for a data partition are in the same index object. This particular arrangement of indexes and index partitions would be established with statements like the following statements:
CREATE TABLE A (columns) in ts1 
      PARTITION BY RANGE (column expression)
      (PARTITION PART0 STARTING FROM constant ENDING constant,
       PARTITION PART1 STARTING FROM constant ENDING constant, 
       PARTITION PART2 STARTING FROM constant ENDING constant,
     
   CREATE INDEX x1 ON A (...) PARTITIONED; 
   CREATE INDEX x2 ON A (...) PARTITIONED;
         
Figure 2 shows another example of a partitioned index.
Figure 2. Partitioned indexes with data partitions and index partitions in different table spaces.
Illustration of partitioned indexes with data partitions and index partitions in different table spaces.
In this example, the data partitions for table A are distributed across two table spaces, TS1, and TS3. The index partitions are also in different table spaces. The index partitions reference only the rows in the data partition with which they are associated. This particular arrangement of indexes and index partitions would be established with statements like the following statements:
CREATE TABLE A (columns) 
      PARTITION BY RANGE (column expression)
      (PARTITION PART0 STARTING FROM constant ENDING constant IN ts1 INDEX IN ts2,
       PARTITION PART1 STARTING FROM constant ENDING constant IN ts3 INDEX IN ts4, 
       PARTITION PART2 STARTING FROM constant ENDING constant IN ts3,INDEX IN ts5)
     
   CREATE INDEX x1 ON A (...); 
   CREATE INDEX x2 ON A (...);
         
In this case, the PARTITIONED clause was omitted from the CREATE INDEX statement; the indexes are still created as partitioned indexes, as this setting is the default for partitioned tables.
The Figure 3 diagram shows an example of a partitioned table with both nonpartitioned and partitioned indexes.
Figure 3. Combination of nonpartitioned and partitioned indexes for a partitioned table
Illustration of index partitions in various table spaces
In this diagram, index X1 is a nonpartitioned index that references all of the partitions of table T1. Indexes X2 and X3 are partitioned indexes that reside in various table spaces. This particular arrangement of indexes and index partitions would be established with statements like the following statements:
   CREATE TABLE t1 (columns) in ts1 INDEX IN ts2  1 
      PARTITION BY RANGE (column expression)
      (PARTITION PART0 STARTING FROM constant ENDING constant  IN ts3,  2 
       PARTITION PART1 STARTING FROM constant ENDING constant  INDEX IN ts5, 
       PARTITION PART2 STARTING FROM constant ENDING constant  INDEX IN ts4,
       PARTITION PART3 STARTING FROM constant ENDING constant  INDEX IN ts4,
       PARTITION PART4 STARTING FROM constant ENDING constant)

   CREATE INDEX x1 ON t1 (...) NOT PARTITIONED; 
   CREATE INDEX x2 ON t1 (...) PARTITIONED;
   CREATE INDEX x3 ON t1 (...) PARTITIONED;         
Note that:
Important: Unlike nonpartitioned indexes, with partitioned indexes you cannot use the INDEX IN clause of the CREATE INDEX statement to specify the table space in which to store index partitions. The only way to override the default storage location for index partitions is to specify the location at the time you create the table by using the partition-level INDEX IN clause of the CREATE TABLE statement. The table-level INDEX IN clause has no effect on index partition placement.

You create partitioned indexes for a partitioned table by including the PARTITIONED option in a CREATE INDEX statement. For example, for a table named SALES partitioned with sales_date as the table-partitioning key, to create a partitioned index, you could use a statement like this statement:

   CREATE INDEX partIDbydate on SALES (sales_date, partID) PARTITIONED
If you are creating a partitioned unique index, then the table partitioning columns must be included in the index key columns. So, using the previous example, if you tried to create a partitioned index with the following statement:
   CREATE UNIQUE INDEX uPartID on SALES (partID) PARTITIONED
the statement would fail because the column sales_date, which forms the table-partitioning key is not included in the index key.
If you omit the PARTITIONED keyword when you create an index on a partitioned table, the database manager creates a partitioned index by default unless the following conditions apply: In either of these cases, the index is created as a nonpartitioned index.

Although creating a nonpartitioned index with a definition that matches that of an existing nonpartitioned index returns SQL0605W, a partitioned index can coexist with a nonpartitioned index with a similar definition. This coexistence is intended to allow for easier adoption of partitioned indexes.