db2v9.7 以前的方法:


1、初始化表test1和test2并模拟高水位大于已使用表空间的状态:
create table test1 like emp_test
create table test2 like emp_test
分别向test1和test2插入数据
drop table test1
查看表空间状态如下:
 Tablespace ID                        = 9
 Name                                 = OFLINEDATA
 Type                                 = Database managed space
 Contents                             = All permanent data. Large table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 46368
 Useable pages                        = 46336
 Used pages                           = 992
 Free pages                           = 45344
 High water mark (pages)              = 1888
 Page size (bytes)                    = 8192
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1
 Minimum recovery time                = 2014-03-04-07.34.45.000000
2、使用db2dart命令查看高水位状态:
db2dart newdb /DHWM
然后在交互状态下输入表空间id:9
查看生成的RPT文件:
FYI: An active connection to the database has been detected.
     False errors may be reported.  
     Deactivate all connections and re-run to verify.
检测到有连接存在,去掉连接重新执行,重新查看RPT文件:
Dump high water mark processing - phase start.

Number of free extents below high water mark: 28
Number of used extents below high water mark: 31

Object holding high water mark:

 Object ID: 5
 Type:      Index Extent


Dump high water mark processing - phase end.

High water mark processing - phase end.

                     ______________________________________

         The requested DB2DART processing has completed successfully!
                     All operation completed without error;
                   no problems were detected in the database.
                     ______________________________________

                        Complete DB2DART report found in:
/home/db2inst3/sqllib/db2dump/DART0000/NEWDB.RPT

    _______    D A R T    P R O C E S S I N G    C O M P L E T E    _______
PRT文件中的“Type:      Index Extent”显示,是表test2的index占用了大量的高水位以上的块
3、获得降低高水位标记的建议:
db2dart newdb /LHWM
交互界面输入表空间ID和期望降低的高水位值,第二个参数0代表尽可能降低高水位:
9 0
生成的RPT文件如下:

Step #1:  Object ID = 5

 => EXPORT all data from table, drop the table, disconnect, reconnect,
    recreate the table, and LOAD the data back into the table.

    Table: DB2INST3.TEST2

     DAT object size:   19
     INX object size:   9
     XDA object size:   0
     LF object size:    0
     LOB object size:   0
     LOBA object size:  0
     BMP object size:   0

     Total size of object parts: 28
     Minimum number of extents that will move by this operation: 28

     Current high water mark:                    30
     Desired high water mark:                    0
     Number of used extents in tablespace:       31
     Number of free extents below original HWM:  28
     Number of free extents below desired HWM:   0
     Number of free extents below current HWM:   0


Final high water mark:  Extent #30 (31 extents, 992 pages).

** This cannot be lowered further as there are not enough free extents
   to move the object holding the high water mark.


** Note: The table space is enabled with reclaimable storage, as an
   alternative to the suggestions above (if any), the ALTER TABLESPACE
   LOWER HIGH WATER MARK command may be used to lower the high water
   mark.


Lower high water mark processing - phase end.

High water mark processing - phase end.

                     ______________________________________

         The requested DB2DART processing has completed successfully!
                     All operation completed without error;
                   no problems were detected in the database.
                     ______________________________________

                        Complete DB2DART report found in:
/home/db2inst3/sqllib/db2dump/DART0000/NEWDB.RPT

    _______    D A R T    P R O C E S S I N G    C O M P L E T E    _______
可以看到占用高水位的表为test2,需要对其离线重组。可以忽略后面提示的“This cannot be lowered further”
4、重组表并重新查看表空间使用率:
db2 reorg table db2inst3.test2
db2 runstats on table db2inst3.test2 with distribution and indexes all
db2 list tablespaces show detail
结果如下:
Tablespace ID                        = 9
 Name                                 = OFLINEDATA
 Type                                 = Database managed space
 Contents                             = All permanent data. Large table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 46368
 Useable pages                        = 46336
 Used pages                           = 960
 Free pages                           = 45376
 High water mark (pages)              = 1856
 Page size (bytes)                    = 8192
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1
 Minimum recovery time                = 2014-03-04-07.34.45.000000
只能降低1888-1856=32pages,远远低于预期。
离线重组索引:
db2 reorg index all for table test2
db2 list tablespaces show detail
结果如下:
 Tablespace ID                        = 9
 Name                                 = OFLINEDATA
 Type                                 = Database managed space
 Contents                             = All permanent data. Large table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 46368
 Useable pages                        = 46336
 Used pages                           = 960
 Free pages                           = 45376
 High water mark (pages)              = 1024
 Page size (bytes)                    = 8192
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1
 Minimum recovery time                = 2014-03-04-07.34.45.000000
降到了1024个页,非常接近已使用页,说明对index的重组使得表空间高水位降低,也印证了RPT文件中的描述“Type:      Index Extent”

对于db2dart工具的补充说明:
绝对不能在使用db2dart工具时有活动的数据库连接!会导致收集到的数据严重不准确!
在实验结束后使用db2dart工具分别在连接和非连接状态下执行检测高水位命令,得到了极其不同的结果。
非连接:
High water mark:  1024 pages, 32 extents (extents #0 - 31)


 [0000] 65534 0x0e   [0001] 65534 0x0e   [0002] 65535 0x00   [0003]     5 0x40*
 [0004]     5 0x00*  [0005]     5 0x00   [0006]     5 0x00   [0007]     5 0x41*
 [0008]     5 0x01*  [0009] == EMPTY ==  [0010] == EMPTY ==  [0011]     5 0x00  
 [0012]     5 0x00   [0013]     5 0x00   [0014]     5 0x00   [0015]     5 0x00  
 [0016]     5 0x00   [0017]     5 0x00   [0018]     5 0x00   [0019]     5 0x00  
 [0020]     5 0x00   [0021]     5 0x00   [0022]     5 0x00   [0023]     5 0x00  
 [0024]     5 0x00   [0025]     5 0x00   [0026]     5 0x01   [0027]     5 0x01  
 [0028]     5 0x01   [0029]     5 0x01   [0030]     5 0x01   [0031]     5 0x01  



Dump high water mark processing - phase start.

Number of free extents below high water mark: 2
Number of used extents below high water mark: 30

Object holding high water mark:

 Object ID: 5
 Type:      Index Extent
连接:
High water mark:  1888 pages, 59 extents (extents #0 - 58)


 [0000] 65534 0x0e   [0001] 65534 0x0e   [0002] 65535 0x00   [0003]     5 0x40*
 [0004]     5 0x00*  [0005]     5 0x00   [0006]     5 0x00   [0007]     5 0x41*
 [0008]     5 0x01*  [0009] == EMPTY ==  [0010] == EMPTY ==  [0011]     5 0x00  
 [0012]     5 0x00   [0013]     5 0x00   [0014]     5 0x00   [0015]     5 0x00  
 [0016]     5 0x00   [0017]     5 0x00   [0018]     5 0x00   [0019]     5 0x00  
 [0020]     5 0x00   [0021]     5 0x00   [0022]     5 0x00   [0023]     5 0x00  
 [0024]     5 0x00   [0025]     5 0x00   [0026]     5 0x01   [0027]     5 0x01  
 [0028]     5 0x01   [0029]     5 0x01   [0030]     5 0x01   [0031]     5 0x01  
 [0032] == EMPTY ==  [0033] == EMPTY ==  [0034] == EMPTY ==  [0035] == EMPTY ==
 [0036] == EMPTY ==  [0037] == EMPTY ==  [0038] == EMPTY ==  [0039] == EMPTY ==
 [0040] == EMPTY ==  [0041] == EMPTY ==  [0042] == EMPTY ==  [0043] == EMPTY ==
 [0044] == EMPTY ==  [0045] == EMPTY ==  [0046] == EMPTY ==  [0047] == EMPTY ==
 [0048] == EMPTY ==  [0049] == EMPTY ==  [0050] == EMPTY ==  [0051] == EMPTY ==
 [0052] == EMPTY ==  [0053] == EMPTY ==  [0054] == EMPTY ==  [0055] == EMPTY ==
 [0056] == EMPTY ==  [0057] == EMPTY ==  [0058] == EMPTY ==


Dump high water mark processing - phase start.

Number of free extents below high water mark: 29
Number of used extents below high water mark: 30

Object holding high water mark:

 * The high-water mark extent is not holding an extent for any objects
   in this tablespace. It may be holding an extent that was released by
   an object but is not completely free yet.  Connect to the database
   and run the LIST TABLESPACES SHOW DETAIL command to free the extent
   before running db2dart again.


Dump high water mark processing - phase end.

High water mark processing - phase end.
存在活动连接的数据库收集到的数据与实际数据差距极大,在以后的使用中一定要注意。





使用db2v9.7中的方法:
1、重新初始化数据:
create table test2 like emp_test
declare c1 cursor for select * from emp_test
load from c1 of cursor insert into test2 nonrecoverable
drop table test1
2、查看当前表空间情况:
db2 "SELECT varchar(tbsp_name, 16) as tbsp_name,RECLAIMABLE_SPACE_ENABLED,TBSP_USED_PAGES,TBSP_FREE_PAGES, TBSP_PAGE_TOP from TABLE (MON_GET_TABLESPACE('',-1)) AS t where t.TBSP_PAGE_TOP > t.TBSP_USED_PAGES"
结果如下:
TBSP_NAME        RECLAIMABLE_SPACE_ENABLED TBSP_USED_PAGES      TBSP_FREE_PAGES      TBSP_PAGE_TOP       
---------------- ------------------------- -------------------- -------------------- --------------------
OFLINEDATA                               1                  704                44768                 1568

  1 record(s) selected.
说明OFLINEDATA表空间的高水位在已使用页之上,而且RECLAIMABLE_SPACE_ENABLED(可回收存储器)的值为1,说明可以降低该表空间的高水位。
3、降低高水位:
3.1、ALTER TABLESPACE OFLINEDATA LOWER HIGH WATER MARK
在执行过程中,表空间的状态为0x00080000
执行结束后,查看执行结果如下:
TBSP_NAME        RECLAIMABLE_SPACE_ENABLED TBSP_USED_PAGES      TBSP_FREE_PAGES      TBSP_PAGE_TOP       
---------------- ------------------------- -------------------- -------------------- --------------------
OFLINEDATA                               1                  704                45632                  736

  1 record(s) selected.
高水位已经下降,并且此时表空间状态为0x00000000,一切正常。在迁移过程中可以使用如下sql来监控数据块的移动:
db2 "SELECT varchar(tbsp_name, 20) as tbsp_name, NUM_EXTENTS_MOVED,NUM_EXTENTS_LEFT, TOTAL_MOVE_TIME from TABLE(MON_GET_EXTENT_MOVEMENT_STATUS('TBS1',-2)) AS t"
也可以查看db2diag.log文件里对数据块迁移的描述:
2014-03-05-17.13.27.793665+480 E5653222E485        LEVEL: Info
PID     : 27103                TID  : 140707503269632PROC : db2sysc 0
INSTANCE: db2inst3             NODE : 000          DB   : NEWDB
APPHDL  : 0-254                APPID: *LOCAL.DB2.140305091327
AUTHID  : DB2INST3
EDUID   : 161                  EDUNAME: db2agent (NEWDB) 0
FUNCTION: DB2 UDB, buffer pool services, sqlbExtentMovementEntryPoint, probe:4896
DATA #1 :
Extent Movement started on table space 9

2014-03-05-17.13.28.831765+480 E5653708E568        LEVEL: Info
PID     : 27103                TID  : 140707503269632PROC : db2sysc 0
INSTANCE: db2inst3             NODE : 000          DB   : NEWDB
APPHDL  : 0-254                APPID: *LOCAL.DB2.140305091327
AUTHID  : DB2INST3
EDUID   : 161                  EDUNAME: db2agent (NEWDB) 0
FUNCTION: DB2 UDB, buffer pool services, sqlbLockAndMoveExtents, probe:4105
DATA #1 : String, 126 bytes
The extent movement operation has moved all extents it could. There are no more free extents lower in the tablespace. Exiting.

2014-03-05-17.13.28.835150+480 E5654277E530        LEVEL: Info
PID     : 27103                TID  : 140707503269632PROC : db2sysc 0
INSTANCE: db2inst3             NODE : 000          DB   : NEWDB
APPHDL  : 0-254                APPID: *LOCAL.DB2.140305091327
AUTHID  : DB2INST3
EDUID   : 161                  EDUNAME: db2agent (NEWDB) 0
FUNCTION: DB2 UDB, buffer pool services, sqlbLockAndMoveExtents, probe:4442
MESSAGE : ADM6008I  Extents within table space "OFLINEDATA"  (ID "9") have been
          moved. Reason code = "0".
3.2、缩小表空间(如有必要):
ALTER TABLESPACE OFLINEDATA REDUCE (ALL CONTAINERS 10 M)
查看执行结果如下:
TBSP_NAME        RECLAIMABLE_SPACE_ENABLED TBSP_USED_PAGES      TBSP_FREE_PAGES      TBSP_PAGE_TOP       
---------------- ------------------------- -------------------- -------------------- --------------------
OFLINEDATA                               1                  704                44352                  736
可以看到TBSP_FREE_PAGES的值降低,说明表空间在缩小,至此降低高水位命令测试成功。如果要降低高水位的表空间是自动存储表空间,则看可以直接使用命令:
ALTER TABLESPACE OFLINEDATA REDUCE MAX





测试数据移动期间表空间访问情况:
1、初始化数据
create table test2 like emp_test
多次执行以下sql:
declare c1 cursor for select * from emp_test
load from c1 of cursor insert into test2 nonrecoverable
load from c1 of cursor insert into test1 nonrecoverable
drop table test1
制造表空间高水位高于使用页的情况
2、进行数据移动:
ALTER TABLESPACE OFLINEDATA LOWER HIGH WATER MARK
2.1、在另一个终端查看表空间状态:
db2pd -db newdb -tab
结果如下:
0x00007FF8BC0B9C40 9     DMS  Large   8192   32       Yes  32       1     1         Off 1        0          31           OFLINEDATA
0x00007FF8BC0B9C40 9     23072      23040      9536       0          13504      12448      20096      0x00080000 1394070412 0          No           
状态为0x00080000
2.2、查看数据库锁:
db2pd -db newdb -locks showlocks
结果如下:
Database Partition 0 -- Database NEWDB -- Active -- Up 0 days 00:34:17 -- Date 03/06/2014 10:13:30

Locks:
Address            TranHdl    Lockname                   Type       Mode Sts Owner      Dur HoldCount  Att        ReleaseFlg rrIID
0x00007FF8BB21D280 12         0900000000000000000000006D default    ..X  G   12         1   0          0x00000000 0x40000000 0     ***invalid lockobj:109[db2inst3@test
蓝色中的描述很奇怪,IBM官方解释说是db2pd工具的错误,建议使用snapshot。重新使用snapshot做实验发现如下锁:
Application handle                         = 579
Application ID                             = *LOCAL.DB2.140306022422
Sequence number                            = 01415
Application name                           = db2ExtMov
CONNECT Authorization ID                   = DB2INST3
Application status                         = Connect Completed
Status change time                         = 03/06/2014 10:24:22.042560
Application code page                      = 1208
Locks held                                 = 1
Total wait time (ms)                       = 0

List Of Locks
 Lock Name                   = 0x0900000000000000000000006D
 Lock Attributes             = 0x00000000
 Release Flags               = 0x40000000
 Lock Count                  = 1
 Hold Count                  = 0
 Lock Object Name            = 9
 Object Type                 = Internal Extent Movement Lock
 Tablespace Name             = OFLINEDATA
 Mode                        = X
表空间级的X锁,锁类型为“Internal Extent Movement Lock”,此前从来没有碰到过这个锁,它有什么特性呢?网上描述很少,暂且搁置。此处特别好奇到底alter tablespace LOWER HIGH WATER MARK命令和reorg有什么不同,于是执行reorg重新查看锁发现如下内容:
Application handle                         = 486
Application ID                             = *LOCAL.db2inst3.140306013913
Sequence number                            = 01416
Application name                           = db2bp
CONNECT Authorization ID                   = DB2INST3
Application status                         = UOW Executing
Status change time                         = 03/06/2014 10:43:50.563868
Application code page                      = 1208
Locks held                                 = 6
Total wait time (ms)                       = 0

List Of Locks
 Lock Name                   = 0x0900000000000000000000006D
 Lock Attributes             = 0x00000000
 Release Flags               = 0x00000100
 Lock Count                  = 1
 Hold Count                  = 0
 Lock Object Name            = 9
 Object Type                 = Internal Extent Movement Lock
 Tablespace Name             = OFLINEDATA
 Mode                        = S

 Lock Name                   = 0x09000000000000000000000070
 Lock Attributes             = 0x00000000
 Release Flags               = 0x40000000
 Lock Count                  = 4
 Hold Count                  = 0
 Lock Object Name            = 9
 Object Type                 = Tablespace
 Tablespace Name             = OFLINEDATA
 Mode                        = IX

 Lock Name                   = 0x09000400000000000000000074
 Lock Attributes             = 0x00000000
 Release Flags               = 0x40000000
 Lock Count                  = 1
 Hold Count                  = 0
 Lock Object Name            = 4
 Object Type                 = Internal Table Alter Lock
 Tablespace Name             = OFLINEDATA
 Table Schema                = DB2INST3
 Table Name                  = TEST2
 Mode                        = X

 Lock Name                   = 0x000005000C0019000000000052
 Lock Attributes             = 0x00000000
 Release Flags               = 0x40000000
 Lock Count                  = 1
 Hold Count                  = 0
 Lock Object Name            = 1638412
 Object Type                 = Row
 Tablespace Name             = SYSCATSPACE
 Table Schema                = SYSIBM
 Table Name                  = SYSTABLES
 Mode                        = NS

 Lock Name                   = 0x09000400000000000000000054
 Lock Attributes             = 0x00000000
 Release Flags               = 0x40000000
 Lock Count                  = 1
 Hold Count                  = 0
 Lock Object Name            = 4
 Object Type                 = Table
 Tablespace Name             = OFLINEDATA
 Table Schema                = DB2INST3
 Table Name                  = TEST2
 Mode                        = U

 Lock Name                   = 0x00000500000000000000000054
 Lock Attributes             = 0x00000000
 Release Flags               = 0x40000000
 Lock Count                  = 1
 Hold Count                  = 0
 Lock Object Name            = 5
 Object Type                 = Table
 Tablespace Name             = SYSCATSPACE
 Table Schema                = SYSIBM
 Table Name                  = SYSTABLES
 Mode                        = IS
出现了和alter tablespace命令相同的Internal Extent Movement Lock,但是此处为表空间的S锁而非X锁;除此之外还有表空间的IX锁、表的X锁和表的U锁,怪不得reorg一出,所有操作都是浮云。猜测表空间的Internal Extent Movement Lock锁不会影响用户对表空间中任何表的dml操作,在reorg操作中真正起到隔离作用的应该是表的X锁和U锁。


在使用alter tablespace命令降低高水位的过程中对表进行dml操作:
删除emp_test的一条数据:
delete from emp_test where empno=1
成功。
删除test2的一条数据:
delete from test2 where empno=2
成功。
可以在降低高水位的过程中对表进行dml操作。但是降低高水位的过程应该对表空间的I/O影响很大,所以降低高水位命令还是应该充分测试后使用。