<DB2> "DB2 Create Partition Table and Related Operations" (Essence)

1 Basic concepts

When the amount of data in the table continues to increase, the speed of querying data will slow down, and the performance of the application will decrease. At this time, the 考虑table should be partitioned. A partitioned table is called a partitioned table .

After the table is partitioned, the table is still logically one 完整的表, but the data in the table is physically stored in multiple "table spaces" ( 物理文件上), so that when querying data, it is not necessary to scan the entire table every time but only from the The current partition finds the required data, which greatly improves the speed of data query.

advantage:

A Improve query performance: The query for partitioned objects can only search the partitions you care about, improving the retrieval speed.
B Enhanced availability: If a partition of the table fails, the data in other partitions of the table is still available.
C If a partition of the table fails and the data needs to be repaired, only the partition can be repaired. For example: to clean up the partitions during data cleanup, you only need to clean up the partitions of the relevant dates.
D Balanced I/O: You can map different partitions to different disks to balance I/O and improve overall system performance.

2 operations

2.1 View the partition table existing in the database

select t.TABSCHEMA,t.TABNAME as Tablename,t.c as NumPartion from (select TABSCHEMA,TABNAME,count(*) as c from sysibm.SYSDATAPARTITIONS group by (TABSCHEMA,TABNAME)) as t where t.c>1

2.2 View partition table details

db2 describe data partitions for table DB2INST1.TBL_EXTERNALPAYMENTAFINFO
或
db2 describe data partitions for table DB2INST1.TBL_EXTERNALPAYMENTAFINFO show detail
//说明:这里的表要加模式名

insert image description here
1. English meaning

PartitionId Inclusive Low Value High Value
partition sequence Contains (judging the boundary here) partition start end of partition

2. N—does not include boundaries; Y—includes boundaries
3. The sequence is automatically assigned by the system
4. Split: split a partition into two new partitions, and the separated partitions will exist independently.
5. Adding a partition means: detach the last partition, add a new partition, and then import the data of the last partition.

2.3 Disconnect access to the data table

Note: Applying access to partition data may cause the split to fail; if data is written to a partition that has been split and does not exist, data may be lost.

1. Stop the application
2. Execute the command in the database: db2 force applications all

2.4 Backup data

1-Back up the data of the entire table; pay attention to the data volume of the table
2-db2 "export to ./path ..."
3-db2 "select count(1) from tblname with ur"

2.5 Detach partition

view partition table

db2 describe data partitions for table DB2INST1. TBL_EXTERNALPAYMENTAFINFO

Detach partition to new table

db2 ALTER TABLE DB2INST1.TBL_EXTERNALPAYMENTAFINFO DETACH PARTITION OTHERS2 INTO TBL_EXTERNALPAYMENTAFINFO_2017
说明:

1、ALTER 和 DETACH PARTITION 都为固定格式detach 拆离
2、TBL_EXTERNALPAYMENTAFINFO_2017是新建的表名,可以任意,一般为了方便与原表相似。
3、拆离后的分区数据放在表TBL_EXTERNALPAYMENTAFINFO_2017中
4、OTHERS2 是最后一个分区的分区ID,同表中的109
5、拆离分区会自动创建表,无需提前创建。
6、拆离分区需要等一会,几秒钟到1-3分钟。此时,sysibm.SYSDATAPARTITIONS中原表的DATAPARTITIONNAME分区显示为SQL210110102024000情况。

2.6 Add partition

Description: Add a new partition, the script is as follows.

CONNECT TO BIZ;
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO  ADD PART DATAPT108 STARTING(MINVALUE) ENDING('2017-01-01') EXCLUSIVE IN TBS_RPDATA1;
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO ADD PART DATAPT109 STARTING('2017-01-01') ENDING('2017-02-01') EXCLUSIVE IN TBS_RPDATA1;
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO ADD PART DATAPT110 STARTING('2017-02-01') ENDING('2017-03-01') EXCLUSIVE IN TBS_RPDATA1;
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO ADD PART DATAPT111 STARTING('2017-03-01') ENDING('2017-04-01') EXCLUSIVE IN TBS_RPDATA1;
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO ADD PART DATAPT112 STARTING('2017-04-01') ENDING('2017-05-01') EXCLUSIVE IN TBS_RPDATA1;
---
---
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO ADD PART DATAPT154 STARTING('2020-10-01') ENDING('2020-11-01') EXCLUSIVE IN TBS_RPDATA1;
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO ADD PART DATAPT155 STARTING('2020-11-01') ENDING('2020-12-01') EXCLUSIVE IN TBS_RPDATA1;
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO ADD PART DATAPT156 STARTING('2020-12-01') ENDING('2021-01-01') EXCLUSIVE IN TBS_RPDATA1;
ALTER TABLE DB2INST1.TBL_DOMESTICPAYMENTAFINFO ADD PART OTHERS2   STARTING('2021-01-01') ENDING(MAXVALUE) EXCLUSIVE IN TBS_RPDATA1;
COMMIT WORK;
CONNECT RESET;
TERMINATE;

2.7 Import data

Description: Import the data backed up by the detached partition.

2.8 Data before and after verification

Description: Verify that the amount of data after adding partitions is the same as that before splitting.

2.9 Delete temporary table data

db2 drop table DB2INST2.TBL_EXTERNALPAYMENTAFINFO_2017

Guess you like

Origin blog.csdn.net/tangcoolcole/article/details/130997608
db2
db2