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:
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;
結論:非唯一索引預設都是分割槽索引。