首页  :: 新随笔  :: 管理

DB2表空间管理

Posted on 2021-12-07 23:54  高&玉  阅读(1248)  评论(0编辑  收藏  举报

1 介绍

1.1 表空间类型

存储类型

表空间类型

注释

DMS

All permanent data. Regular table space.

系统表空间

SMS

System Temporary data

系统临时空间

DMS

All permanent data. Large table space

用户表空间

DMS

User Temporary data

用户临时空间

SMS

All permanent data. Regular table space.

用户表空间

1.2 存储类型

表空间存储类型:

  • 系统管理的空间(system managed space,SMS):由操作系统的文件系统管理器和管理空间。在DB2 V9之前,如果不带任何参数创建数据库或表空间,就会导致所有表空间作为SMS对象创建。
  • 数据库管理的表空间(database managed space,DMS):由DB2数据库管理程序控制存储空间。表空间容器可使用文件系统或裸设备。需要更多的维护
  • DMS的自动存储(automatic storage with DMS):自动存储实际上不是一种单独的表空间类型,而是一种处理DMS存储的不同方式。

1.3 表空间介绍

表空间介绍:

TBSPACE

作用

SYSCATSPACE

系统目录表空间,数据字典,系统信息不可更改和不可删除

TEMPSPACE

临时表空间,排序时需要额外空间的操作

USERSPACE

创建表时没指定表空间,默认的用户表空间

1.4 表空间存储空间

表空间存储空间上限:表空间存储上限64GB到64TB。

表空间类型 4KB 8KB 16KB 32KB
SMS表空间 64GB 128GB 256GB 512GB
临时SMS和临时自动存储器 8TB 16TB 32TB 64TB
DMS和非临时自动存储器表空间(常规) 64GB 128GB 256GB 512GB
DMS、临时DMS和非临时自动存储器表空间(大型) 8TB 16TB 32TB 64TB

参考官方文档:https://www.ibm.com/docs/zh/db2/10.5?topic=data-page-table-table-space-size

2 创建表空间

        DB2 V9版本以上创建的表空间默认是自动存储器表空间。

        在DB2 V9.7版本之前,对于在最初没有启用自动存储的数据库,后期不能启用自动存储的特性;但在DB2 V9.7及以后的版本中,开启未启用自动存储,后期可以转换为自动存储数据库,同样,现有的DMS表空间现在可转换为使用自动存储。使用alter database语句来为现有数据库启用自动存储。

 

创建表空间语法:

>>-CREATE--+-----------------------+---------------------------->
           +-LARGE-----------------+   
           +-REGULAR---------------+   
           | .-SYSTEM-.            |   
           '-+--------+--TEMPORARY-'   
             '-USER---'                

>--TABLESPACE--tablespace-name---------------------------------->

>--+-----------------------------------------------------------+-->
   |     .-DATABASE PARTITION GROUP-.                          |   
   '-IN--+--------------------------+--db-partition-group-name-'   

>--+--------------------------+--------------------------------->
   '-PAGESIZE--integer--+---+-'   
                        '-K-'     

   .-MANAGED BY--AUTOMATIC STORAGE--| storage-group |--| size-attributes |--.   
>--+------------------------------------------------------------------------+-->
   '-MANAGED BY--+-SYSTEM--| system-containers |--------------------------+-'   
                 '-DATABASE--| database-containers |--| size-attributes |-'     

>--+---------------------------------+-------------------------->
   '-EXTENTSIZE--+-number-of-pages-+-'   
                 '-integer--+-K-+--'     
                            '-M-'        

>--+-----------------------------------+------------------------>
   '-PREFETCHSIZE--+-AUTOMATIC-------+-'   
                   +-number-of-pages-+     
                   '-integer--+-K-+--'     
                              '-M-'        

>--+-----------------------------+------------------------------>
   '-BUFFERPOOL--bufferpool-name-'   

>--+--------------------------------------+--------------------->
   '-OVERHEAD--+-number-of-milliseconds-+-'   
               '-INHERIT----------------'     

>--+------------------------+----------------------------------->
   +-NO FILE SYSTEM CACHING-+   
   '-FILE SYSTEM CACHING----'   

>--+------------------------------------------+----------------->
   '-TRANSFERRATE--+-number-of-milliseconds-+-'   
                   '-INHERIT----------------'     

>--+--------------------------------+--------------------------->
   '-DATA TAG--+-integer-constant-+-'   
               +-INHERIT----------+     
               '-NONE-------------'     

>--+---------------------------------+-------------------------><
   '-DROPPED TABLE RECOVERY--+-ON--+-'   
                             '-OFF-'     

storage-group

|--+-----------------------------------+------------------------|
   '-USING STOGROUP--storagegroup-name-'   

size-attributes

|--+---------------------+--+-----------------------------+----->
   '-AUTORESIZE--+-NO--+-'  '-INITIALSIZE--integer--+-K-+-'   
                 '-YES-'                            +-M-+     
                                                    '-G-'     

>--+------------------------------------+----------------------->
   '-INCREASESIZE--integer--+-PERCENT-+-'   
                            '-+-K-+---'     
                              +-M-+         
                              '-G-'         

>--+-----------------------------+------------------------------|
   '-MAXSIZE--+-integer--+-K-+-+-'   
              |          +-M-+ |     
              |          '-G-' |     
              '-NONE-----------'     

system-containers

   .----------------------------------------------------------------------.   
   |           .-,------------------.                                     |   
   V           V                    |                                     |   
|----USING--(----'container-string'-+--)--+-----------------------------+-+--|
                                          '-| on-db-partitions-clause |-'     

database-containers

   .--------------------------------------------------------------.   
   V                                                              |   
|----USING--| container-clause |--+-----------------------------+-+--|
                                  '-| on-db-partitions-clause |-'     

container-clause

      .-,---------------------------------------------------.      
      V                                                     |      
|--(----+-FILE---+--'container-string'--+-number-of-pages-+-+--)--|
        '-DEVICE-'                      '-integer--+-K-+--'        
                                                   +-M-+           
                                                   '-G-'           

on-db-partitions-clause

|--ON--+-DBPARTITIONNUM--+-------------------------------------->
       '-DBPARTITIONNUMS-'   

      .-,--------------------------------------------------.      
      V                                                    |      
>--(----db-partition-number1--+--------------------------+-+--)--|
                              '-TO--db-partition-number2-'        

 

创建SMS表空间

db2 "create [large] tablespace TBS_NAME managed by system using('path','path')"

 

创建DMS类型表空间

db2 “create [large] tablespace TBS_NAME managed by database using (file/device 'container string' number of pages K|M|G,file/device 'container string' number of pages K|M|G)”

 

创建pagesize为8K的表空间

db2 "create buffer buffer8k pagesize 8192"
db2 "create tablespace NAME pagesize 8192 managed by system using ('path') bufferpool buffer8k"

PS:DB2表空间默认的pagesize=4096(4k),创建pagesize=8192(8k)的表空间需要指定一个pagesize=8192的bufferpool。

 

创建系统表空间(存储分组、排序、连接、重组、创建索引操作等中间结果)

db2 "create system temporary tablespace TBS_NAME managed by system using('path','path')"

 

创建用户临时表空间(用来批量插入、批量更新和批量删除以加快速度)

db2 "create user temporary tablespace NAME managed by database using(file 'file' 5000)"

3 查看表空间

3.1 查看表空间信息

db2 list tablespaces show detail

[inst105]# db2 list tablespaces show detail

           Tablespaces for Current Database

 Tablespace ID                        = 0
 Name                                 = SYSCATSPACE
 Type                                 = Database managed space
 Contents                             = All permanent data. Regular table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 10240
 Useable pages                        = 10236
 Used pages                           = 9612
 Free pages                           = 624
 High water mark (pages)              = 9612
 Page size (bytes)                    = 16384
 Extent size (pages)                  = 4
 Prefetch size (pages)                = 4
 Number of containers                 = 1

 Tablespace ID                        = 1
 Name                                 = TEMPSPACE1
 Type                                 = System managed space
 Contents                             = System Temporary data
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 1
 Useable pages                        = 1
 Used pages                           = 1
 Free pages                           = Not applicable
 High water mark (pages)              = Not applicable
 Page size (bytes)                    = 16384
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1

 Tablespace ID                        = 2
 Name                                 = USERSPACE1
 Type                                 = Database managed space
 Contents                             = All permanent data. Large table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 2048
 Useable pages                        = 2016
 Used pages                           = 224
 Free pages                           = 1792
 High water mark (pages)              = 288
 Page size (bytes)                    = 16384
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1

 Tablespace ID                        = 3
 Name                                 = SYSTOOLSPACE
 Type                                 = Database managed space
 Contents                             = All permanent data. Large table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 2048
 Useable pages                        = 2044
 Used pages                           = 88
 Free pages                           = 1956
 High water mark (pages)              = 88
 Page size (bytes)                    = 16384
 Extent size (pages)                  = 4
 Prefetch size (pages)                = 4
 Number of containers                 = 1

 

db2pd -db DBNAME -tablespaces

[inst105]# db2pd -db db01 -tablespaces

Database Member 0 -- Database DB01 -- Active -- Up 0 days 00:00:09 -- Date 2021-12-07-23.33.59.387305

Tablespace Configuration:
Address            Id    Type Content PageSz ExtentSz Auto Prefetch BufID BufIDDisk FSC NumCntrs MaxStripe  LastConsecPg RSE  Name
0x00002AC7BC78FD40 0     DMS  Regular 16384  4        Yes  4        1     1         Def 1        0          3            Yes  SYSCATSPACE
0x00002AC7BC79CEE0 1     SMS  SysTmp  16384  32       Yes  32       1     1         On  1        0          31           No   TEMPSPACE1
0x00002AC7BC7AA080 2     DMS  Large   16384  32       Yes  32       1     1         Def 1        0          31           Yes  USERSPACE1
0x00002AC7BC7B7220 3     DMS  Large   16384  4        Yes  4        1     1         Def 1        0          3            Yes  SYSTOOLSPACE

Tablespace Statistics:
Address            Id    TotalPgs   UsablePgs  UsedPgs    PndFreePgs FreePgs    HWM        Max HWM    State      MinRecTime NQuiescers PathsDropped TrackmodState    
0x00002AC7BC78FD40 0     10240      10236      9612       0          624        9612       9612       0x00000000 0          0          No           n/a              
0x00002AC7BC79CEE0 1     1          1          1          0          0          -          -          0x00000000 0          0          No           n/a              
0x00002AC7BC7AA080 2     2048       2016       224        0          1792       288        288        0x00000000 0          0          No           n/a              
0x00002AC7BC7B7220 3     2048       2044       88         0          1956       88         88         0x00000000 0          0          No           n/a              

Tablespace Autoresize Statistics:
Address            Id    AS  AR  InitSize             IncSize              IIP MaxSize              LastResize                 LRF
0x00002AC7BC78FD40 0     Yes Yes 33554432             -1                   No  None                 None                       No  
0x00002AC7BC79CEE0 1     Yes No  0                    0                    No  0                    None                       No  
0x00002AC7BC7AA080 2     Yes Yes 33554432             -1                   No  None                 None                       No  
0x00002AC7BC7B7220 3     Yes Yes 33554432             -1                   No  None                 None                       No  

Tablespace Storage Statistics:
Address            Id    DataTag  Rebalance SGID  SourceSGID
0x00002AC7BC78FD40 0     0        No        0     -                    
0x00002AC7BC79CEE0 1     0        No        0     -                    
0x00002AC7BC7AA080 2     -1       No        0     -                    
0x00002AC7BC7B7220 3     -1       No        0     -                    

Containers:
Address            TspId ContainNum Type    TotalPgs   UseablePgs PathID     StripeSet  Container 
0x00002AC7BC7898A0 0     0          File    10240      10236      0          0          /home/inst105/inst105/NODE0000/DB01/T0000000/C0000000.CAT
0x00002AC7BC7C5000 1     0          Path    1          1          0          0          /home/inst105/inst105/NODE0000/DB01/T0000001/C0000000.TMP
0x00002AC7B7134C00 2     0          File    2048       2016       0          0          /home/inst105/inst105/NODE0000/DB01/T0000002/C0000000.LRG
0x00002AC7BC76A8C0 3     0          File    2048       2044       0          0          /home/inst105/inst105/NODE0000/DB01/T0000003/C0000000.LRG

 

使用系统视图查看

[inst105]# db2 "
> select 
> substr(TBSP_NAME,1,18) as TBSP_NAME,
> TBSP_TYPE,
> substr(TBSP_STATE,1,10) as TBSP_STATE,
> substr(TBSP_TOTAL_SIZE_KB,1,15) as TOTAL_KB,
> substr(TBSP_USABLE_SIZE_KB,1,15) as USABLE_KB,
> substr(TBSP_USED_SIZE_KB,1,15) as USED_KB,
> TBSP_UTILIZATION_PERCENT||'%' as PERCENT,
> TBSP_AUTO_RESIZE_ENABLED 
> from SYSIBMADM.TBSP_UTILIZATION
> "

TBSP_NAME          TBSP_TYPE  TBSP_STATE TOTAL_KB        USABLE_KB       USED_KB         PERCENT  TBSP_AUTO_RESIZE_ENABLED
------------------ ---------- ---------- --------------- --------------- --------------- -------- ------------------------
SYSCATSPACE        DMS        NORMAL     163840          163776          153792          93.90%                          1
TEMPSPACE1         SMS        NORMAL     16              16              16              100.00%                         -
USERSPACE1         DMS        NORMAL     32768           32256           3584            11.11%                          1
SYSTOOLSPACE       DMS        NORMAL     32768           32704           1408            4.30%                           1

TBSP_AUTO_RESIZE_ENABLED值:

  • 1:表示表空间是DMS自动扩展表空间。
  • -:表示表空间为SMS类型表空间由文件系统管理。
  • 0:表示表空间是DMS非自动扩展表空间。

3.2 表空间状态

常见的表空间状态:

状态

解释

0x1

停顿:SHARE                          

0x2

停顿:UPDATE

0x4

停顿:EXCLUSIVE

0x8

装入暂挂

0x10

删除暂挂

0x20

备份暂挂

0x40

正在前滚

0x80

前滚暂挂

0x100

复原暂挂

0x100

恢复暂挂(未使用)

0x200

禁用暂挂

0x400

正在重组

0x800

正在备份

0x1000

必须定义存储器

0x2000

正在复原

0x4000

脱机并且不可访问

0x8000

删除暂挂

0x2000000

可以定义存储器

0x4000000

存储器定义处于“最终”状态

0x8000000

在前滚之前已更改存储器定义

0x10000000

DMS 重新平衡程序处于活动状态

0x20000000

正在进行 TBS 删除

0x40000000

正在进行 TBS 创建

 

表空间状态解析:

[db2inst1]# db2tbst 0x2000
State = Restore in Progress

4 表空间管理

注意:

       SMS、自动存储器表空间均不需要人工介入扩容,以下是基于DMS类型表空间扩容。

 

扩容某表空间所有容器

db2 “alter tablspace TBS_NAME extend (all,1000)”

 

缩容某表空间容器(不建议)

db2 “alter tablespace TBS_NAME reduce (file 'filename' size)”

提示:size是要缩容的值。

 

容器为LVM扩容

db2 “alter tablespace TBS_NAME resize (device '/dev/rLVM' pages)”

提示:pages为增长后的页数。

 

容器为file扩容

db2 “alter tablespace TBS_NAME extend (file 'path' size)”

 

删除表空间某个容器

db2 “alter tablespace TBS_NMAE drop (file 'path',device '/dev/rdisk1')”

 

切换offline表空间为online

db2 “alter tablespace TBS_NAME switch online”

 

转换常规表空间为大型表空间

db2 “alter tablespace TBS_NAME convert to large”

提示:

       将常规表空间转换为large表空间后,需要将表空间内表的索引重建或者对表进行离线reorg,要不然往表中插入数据可能会报错SQL1236N、SQL1237W。

 

重命名表空间

db2 “rename tablespace TBS_NAME to TBS_NAME_NEW”