DB2分區表如何區分索引是分區索引還是非分區索引

問題描述

經常有人問,我分區表裏的索引到底是分區索引還是非分區索引?

因爲是否是分區索引涉及到detach分區的時候是否會耗費大量的時間做異步索引清理:如果是非分區索引,則異步索引清理需要大量時間。

總體結論

--對於唯一索引或者主健,如果包含了分區健,則默認是分區索引;如果不包含分區健,則默認是非分區索引。
--對於非唯一索引,默認都是分區索引。
 

測試過程

DB2版本爲10.5

$ db2 "create table p1 (col1 int not null, col2 int not null, col3 int not null) partition by range(col2)(partition part1 starting 1 ending 5, partition part2 starting 6 ending 10, partition part3 starting 11 ending 15)"

1. 唯一索引

$ db2 "create unique index u_idx1 on p1 (col1)"
$ db2 "create unique index u_idx1_2 on p1 (col1,col2)"
$ db2 "create unique index u_idx1_3 on p1 (col1,col3)"
$ db2look -d sample -a -e -t p1

CREATE UNIQUE INDEX "DB2TST  "."U_IDX1" ON "DB2TST  "."P1" 
                ("COL1" ASC)
                NOT PARTITIONED IN "TBS2"

                COMPRESS NO 
                INCLUDE NULL KEYS ALLOW REVERSE SCANS;

-- DDL Statements for Indexes on Table "DB2TST  "."P1"

SET SYSIBM.NLS_STRING_UNITS = 'SYSTEM';

CREATE UNIQUE INDEX "DB2TST  "."U_IDX1_2" ON "DB2TST  "."P1" 
                ("COL1" ASC,
                 "COL2" ASC)
                PARTITIONED

                COMPRESS NO 
                INCLUDE NULL KEYS ALLOW REVERSE SCANS;

-- DDL Statements for Indexes on Table "DB2TST  "."P1"

SET SYSIBM.NLS_STRING_UNITS = 'SYSTEM';

CREATE UNIQUE INDEX "DB2TST  "."U_IDX1_3" ON "DB2TST  "."P1" 
                ("COL1" ASC,
                 "COL3" ASC)
                NOT PARTITIONED IN "TBS2"

                COMPRESS NO 
                INCLUDE NULL KEYS ALLOW REVERSE SCANS;

結論:對於唯一索引,如果索引中包含了分區健,默認是分區索引。如果索引中不包含分區健,默認是非分區索引。
問題:如果是不包含分區健的唯一索引,想做成分區的怎麼破?答:沒有辦法,創建的時候會報錯 SQL20303N

$ db2 "create unique index u_idx3 on p1 (col3) partitioned"
DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL20303N  The partitioned unique index was not created because either the 
index definition did not include all of the partitioning columns, or the index 
was being created over XML data.  SQLSTATE=42990

2. 主健

刪表重建 
$ db2 "alter table p1 add primary key(col1)"
$ db2 "select varchar(INDNAME,40) as idxname from syscat.indexpartitions where TABNAME='P1'"

IDXNAME                                 
----------------------------------------

  0 record(s) selected.

$ db2 "alter table p1 drop primary key"
$ db2 "alter table p1 add primary key(col1,col2)"
$ db2 "select varchar(INDNAME,40) as idxname from syscat.indexpartitions where TABNAME='P1'"

IDXNAME                                 
----------------------------------------
SQL200122112144130                      
SQL200122112144130                      
SQL200122112144130                      

  3 record(s) selected.

結論:主健實際上是唯一索引,因此效果跟唯一索引類似,即:
如果主健中包含了分區健,則主健對應索引是分區索引。 如果主健中不包含分區健,則索引是非分區索引。

3. 非唯一索引

刪表重建

$ db2 "create index idx1 on p1 (col1)"
$ db2 "create index idx1_2 on p1 (col1,col2)"
$ db2 "create index idx1_3 on p1 (col1,col3)"
$ db2look -d sample -a -e -t p1

CREATE INDEX "DB2TST  "."IDX1" ON "DB2TST  "."P1" 
                ("COL1" ASC)
                PARTITIONED

                COMPRESS NO 
                INCLUDE NULL KEYS ALLOW REVERSE SCANS;

-- DDL Statements for Indexes on Table "DB2TST  "."P1"

SET SYSIBM.NLS_STRING_UNITS = 'SYSTEM';

CREATE INDEX "DB2TST  "."IDX1_2" ON "DB2TST  "."P1" 
                ("COL1" ASC,
                 "COL2" ASC)
                PARTITIONED

                COMPRESS NO 
                INCLUDE NULL KEYS ALLOW REVERSE SCANS;

-- DDL Statements for Indexes on Table "DB2TST  "."P1"

SET SYSIBM.NLS_STRING_UNITS = 'SYSTEM';

CREATE INDEX "DB2TST  "."IDX1_3" ON "DB2TST  "."P1" 
                ("COL1" ASC,
                 "COL3" ASC)
                PARTITIONED

                COMPRESS NO 
                INCLUDE NULL KEYS ALLOW REVERSE SCANS;

結論:非唯一索引默認都是分區索引。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章