DB2表空间管理(1)—基础篇

1.创建数据库

[db2inst2@xifenfei ~]$ db2 "create database xifenfei automatic storage yes 
on /home/db2inst2/dbauto dbpath on /home/db2inst2/xifenfei 
using codeset utf-8 territory cn collate using system"

DB20000I  The CREATE DATABASE command completed successfully.

DBPATH ON表示数据库目录
USING CONDESET codeset TERRITORY territory指定数据库编码集(Codeset)和区域(Territory)
automatic storage yes on /home/db2inst2/dbauto 表示启用自动存储管理,on指定路径

2.查看数据库

[db2inst2@xifenfei ~]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 1

Database 1 entry:

 Database alias                       = XIFENFEI
 Database name                        = XIFENFEI
 Local database directory             = /home/db2inst2/xifenfei
 Database release level               = c.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

3.连接数据库

[db2inst2@xifenfei ~]$ db2 connect to xifenfei

   Database Connection Information

 Database server        = DB2/LINUX 9.5.0
 SQL authorization ID   = DB2INST2
 Local database alias   = XIFENFEI

4.创建缓冲池

[db2inst2@xifenfei ~]$ db2 "create bufferpool bp32k size 100 pagesize 32k"
DB20000I  The SQL command completed successfully.

DB2默认创建库指创建4k的bufferpool,因为下面需要创建32k的表空间,所以需要先创建32k的bufferpool

5.创建数据库管理(DMS)的数据表空间

[db2inst2@xifenfei ~]$ db2 "create large tablespace tbs_data pagesize 32k managed by database 
using (file '/home/db2inst2/dms/tbs_data01.dbf' 100M) extentsize 32 prefetchsize automatic 
bufferpool bp32k no file system caching"

DB20000I  The SQL command completed successfully.

no file system caching关闭文件系统缓存,默认选项

6.创建系统管理(SMS)的临时表空间

[db2inst2@xifenfei ~]$ db2 "create temporary tablespace tbs_temp pagesize 32k 
managed by system using ('/home/db2inst2/xifenfei/temp')  bufferpool bp32k"

DB20000I  The SQL command completed successfully.

7.创建系统管理(SMS)的用户临时表空间

[db2inst2@xifenfei ~]$ db2 "create user temporary tablespace tbs_user_temp pagesize 32k 
managed by system using ('/home/db2inst2/xifenfei/user_temp')  bufferpool bp32k"

DB20000I  The SQL command completed successfully.

8.自动存储管理(Automatic Storage)的表空间

[db2inst2@xifenfei ~]$ db2 "create tablespace tbs_index pagesize 32k bufferpool bp32k"
DB20000I  The SQL command completed successfully.
[db2inst2@xifenfei ~]$ db2 "create tablespace tbs_data2 initialsize 10M increasesize 10M maxsize 10G"
DB20000I  The SQL command completed successfully.

数据表空间选择DMS,临时表空间选择SMS

9.查看表空间状态

[db2inst2@xifenfei ~]$ 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                          = 16384
 Useable pages                        = 16380
 Used pages                           = 10500
 Free pages                           = 5880
 High water mark (pages)              = 10500
 Page size (bytes)                    = 4096
 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)                    = 4096
 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                          = 8192
 Useable pages                        = 8160
 Used pages                           = 96
 Free pages                           = 8064
 High water mark (pages)              = 96
 Page size (bytes)                    = 4096
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1

 Tablespace ID                        = 3
 Name                                 = TBS_DATA
 Type                                 = Database managed space
 Contents                             = All permanent data. Large table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 3200
 Useable pages                        = 3168
 Used pages                           = 96
 Free pages                           = 3072
 High water mark (pages)              = 96
 Page size (bytes)                    = 32768
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1

 Tablespace ID                        = 4
 Name                                 = TBS_TEMP
 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)                    = 32768
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1

 Tablespace ID                        = 5
 Name                                 = TBS_USER_TEMP
 Type                                 = System managed space
 Contents                             = User 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)                    = 32768
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1

 Tablespace ID                        = 6
 Name                                 = TBS_INDEX
 Type                                 = Database managed space
 Contents                             = All permanent data. Large table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 1024
 Useable pages                        = 992
 Used pages                           = 96
 Free pages                           = 896
 High water mark (pages)              = 96
 Page size (bytes)                    = 32768
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1

 Tablespace ID                        = 7
 Name                                 = TBS_DATA2
 Type                                 = Database managed space
 Contents                             = All permanent data. Large table space.
 State                                = 0x0000
   Detailed explanation:
     Normal
 Total pages                          = 2560
 Useable pages                        = 2528
 Used pages                           = 96
 Free pages                           = 2432
 High water mark (pages)              = 96
 Page size (bytes)                    = 4096
 Extent size (pages)                  = 32
 Prefetch size (pages)                = 32
 Number of containers                 = 1

10.查看表空间和容器相关信息

[db2inst2@xifenfei ~]$ db2 list tablespace containers for 6 show detail

            Tablespace Containers for Tablespace 6

 Container ID                         = 0
 Name                                 = /home/db2inst2/dbauto/db2inst2/NODE0000/XIFENFEI/T0000006/C0000000.LRG
 Type                                 = File
 Total pages                          = 1024
 Useable pages                        = 992
 Accessible                           = Yes

11.显示表空间配置信息/使用信息/容器信息

[db2inst2@xifenfei ~]$ db2pd -d xifenfei -tablespaces

Database Partition 0 -- Database XIFENFEI -- Active -- Up 0 days 01:07:23

Tablespace Configuration:
Address    Id    Type Content PageSz ExtentSz Auto Prefetch BufID BufIDDisk FSC NumCntrs MaxStripe  LastConsecPg Name
0x9F43E060 0     DMS  Regular 4096   4        Yes  4        1     1         Off 1        0          3            SYSCATSPACE
0x9F43E6B0 1     SMS  SysTmp  4096   32       Yes  32       1     1         On  1        0          31           TEMPSPACE1
0x9F442EB0 2     DMS  Large   4096   32       Yes  32       1     1         Off 1        0          31           USERSPACE1
0x9F73B2E0 3     DMS  Large   32768  32       Yes  32       2     2         Off 1        0          31           TBS_DATA
0x9FAFE320 4     SMS  SysTmp  32768  32       Yes  32       2     2         On  1        0          31           TBS_TEMP
0x9FB029B0 5     SMS  UsrTmp  32768  32       Yes  32       2     2         On  1        0          31           TBS_USER_TEMP
0x9FB05420 6     DMS  Large   32768  32       Yes  32       2     2         Off 1        0          31           TBS_INDEX
0x9FB0BB20 7     DMS  Large   4096   32       Yes  32       1     1         Off 1        0          31           TBS_DATA2

Tablespace Statistics:
Address    Id    TotalPgs   UsablePgs  UsedPgs    PndFreePgs FreePgs    HWM        State      MinRecTime NQuiescers
0x9F43E060 0     16384      16380      10500      0          5880       10500      0x00000000 0          0          
0x9F43E6B0 1     1          1          1          0          0          0          0x00000000 0          0          
0x9F442EB0 2     8192       8160       96         0          8064       96         0x00000000 0          0          
0x9F73B2E0 3     3200       3168       96         0          3072       96         0x00000000 0          0          
0x9FAFE320 4     1          1          1          0          0          0          0x00000000 0          0          
0x9FB029B0 5     1          1          1          0          0          0          0x00000000 0          0          
0x9FB05420 6     1024       992        96         0          896        96         0x00000000 0          0          
0x9FB0BB20 7     2560       2528       96         0          2432       96         0x00000000 0          0          

Tablespace Autoresize Statistics:
Address    Id    AS  AR  InitSize    IncSize     IIP MaxSize     LastResize                 LRF
0x9F43E060 0     Yes Yes 33554432    -1          No  None        None                       No  
0x9F43E6B0 1     Yes No  0           0           No  0           None                       No  
0x9F442EB0 2     Yes Yes 33554432    -1          No  None        None                       No  
0x9F73B2E0 3     No  No  0           0           No  0           None                       No  
0x9FAFE320 4     No  No  0           0           No  0           None                       No  
0x9FB029B0 5     No  No  0           0           No  0           None                       No  
0x9FB05420 6     Yes Yes 33554432    -1          No  None        None                       No  
0x9FB0BB20 7     Yes Yes 10485760    10485760    No  -2147483648 None                       No  

Containers:
Address    TspId ContainNum Type    TotalPgs   UseablePgs StripeSet  Container 
0x989BDE10 0     0          File    16384      16380      0          /home/db2inst2/dbauto/db2inst2/NODE0000/XIFENFEI/T0000000/C0000000.CAT
0x9F43ED00 1     0          Path    1          1          0          /home/db2inst2/dbauto/db2inst2/NODE0000/XIFENFEI/T0000001/C0000000.TMP
0x9F443500 2     0          File    8192       8160       0          /home/db2inst2/dbauto/db2inst2/NODE0000/XIFENFEI/T0000002/C0000000.LRG
0x9F73B930 3     0          File    3200       3168       0          /home/db2inst2/dms/tbs_data01.dbf
0x9F73DE90 4     0          Path    1          1          0          /home/db2inst2/xifenfei/temp
0x9FB05020 5     0          Path    1          1          0          /home/db2inst2/xifenfei/user_temp
0x9FB0B4A0 6     0          File    1024       992        0          /home/db2inst2/dbauto/db2inst2/NODE0000/XIFENFEI/T0000006/C0000000.LRG
0x9FB0C170 7     0          File    2560       2528       0          /home/db2inst2/dbauto/db2inst2/NODE0000/XIFENFEI/T0000007/C0000000.LRG
发表在 DB2 | 评论关闭

DB2实例管理

1.创建新实例

[root@xifenfei ~]# cd /opt/db2/V9.5/instance/
[root@xifenfei instance]# ./db2icrt -h
DBI1001I  Usage:

 db2icrt [-h|-?]
         [-d]
         [-a AuthType]
         [-p PortName]
         [-s InstType]
         -u FencedID InstName

[root@xifenfei instance]# useradd Fence2 
[root@xifenfei instance]# passwd Fence2 
Changing password for user Fence2.
New UNIX password: 
Retype new UNIX password: 
passwd: all authentication tokens updated successfully.
[root@xifenfei instance]# useradd db2inst2
[root@xifenfei instance]# passwd db2inst2
Changing password for user db2inst2.
New UNIX password: 
Retype new UNIX password: 
passwd: all authentication tokens updated successfully.
[root@xifenfei instance]# ./db2icrt -a server -p 55555 -s ese -u Fence2 db2inst2
DBI1070I  Program db2icrt completed successfully.




[root@xifenfei instance]# su - db2inst2
[db2inst2@xifenfei ~]$ db2 get instance

 The current database manager instance is:  db2inst2

[db2inst2@xifenfei ~]$ db2ilist
db2inst1
db2inst2

[db2inst2@xifenfei ~]$ db2greg -dump
S,DB2,9.5.0.0,/opt/db2/V9.5,,,0,0,,1332659153,0
S,DAS,9.5.0.0,/opt/db2/V9.5/das,lib/libdb2dasgcf.so,,,, ,,
I,DAS,9.5.0.0,dasusr1,/home/dasusr1/das,,1,,/opt/db2/V9.5/das,,
I,DB2,9.5.0.0,db2inst1,/home/db2inst1/sqllib,,1,0,/opt/db2/V9.5,,
I,DB2,9.5.0.0,db2inst2,/home/db2inst2/sqllib,,1,0,/opt/db2/V9.5,,

2.启动关闭实例

[db2inst2@xifenfei ~]$ db2stop
03/31/2012 11:57:48     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
[db2inst2@xifenfei ~]$ db2start
03/31/2012 11:57:52     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.

3.删除实例

--先需要关闭实例
[root@xifenfei instance]# ./db2idrop db2inst2
DBI1070I  Program db2idrop completed successfully.
发表在 DB2 | 一条评论

hint指定index的深入理解

模拟环境
创建一个表,含有位图index和b-tree index

SQL> create table t_xifenfei as 
  2  select object_id,object_name from dba_objects;

Table created.

SQL> create index b_tree_t_xifenfei on t_xifenfei(object_id);

Index created.

SQL> CREATE BITMAP INDEX  bitmap_t_xifenfei on t_xifenfei(object_name);

Index created.

SQL> BEGIN
  2  dbms_stats.gather_table_stats(USER,'T_XIFENFEI',cascade => true);
  3  END;
  4  / 

PL/SQL procedure successfully completed.

无index hint

SQL> SET AUTOT TRACE EXPL STAT 
SQL> SELECT OBJECT_ID FROM t_xifenfei;

845708 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 548923532

--------------------------------------------------------------------------------
| Id  | Operation         | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |            |   841K|  4109K|   886   (3)| 00:00:11 |
|   1 |  TABLE ACCESS FULL| T_XIFENFEI |   841K|  4109K|   886   (3)| 00:00:11 |
--------------------------------------------------------------------------------


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
      60525  consistent gets
          0  physical reads
          0  redo size
   15543305  bytes sent via SQL*Net to client
     620649  bytes received via SQL*Net from client
      56382  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
     845708  rows processed

这里因为object_id列可能有null值,所以不会使用b_tree_t_xifenfei索引,预料之中事件

index hint b_tree_t_xifenfei

SQL> SET  LINESIZE 150
SQL> SELECT /*+ INDEX(T b_tree_t_xifenfei) */object_id from t_xifenfei t;

845708 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1935372603

--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |   841K|  4109K| 18940   (1)| 00:03:48 |
|   1 |  TABLE ACCESS BY INDEX ROWID | T_XIFENFEI        |   841K|  4109K| 18940   (1)| 00:03:48 |
|   2 |   BITMAP CONVERSION TO ROWIDS|                   |       |       |            |          |
|   3 |    BITMAP INDEX FULL SCAN    | BITMAP_T_XIFENFEI |       |       |            |          |
--------------------------------------------------------------------------------------------------


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      91537  consistent gets
          0  physical reads
          0  redo size
   42362633  bytes sent via SQL*Net to client
     620649  bytes received via SQL*Net from client
      56382  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
     845708  rows processed

这里因为object_id列可能有null值,所以不会使用b_tree_t_xifenfei索引,这里的疑惑是:
就算不会使用b_tree_t_xifenfei index也不应该会使用BITMAP_T_XIFENFEI index,因为使用这个的cost会大于全表扫描

index hint 一个无效index

SQL> SELECT /*+ INDEX(T abc) */object_id from t_xifenfei t;

845708 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1935372603

--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |   841K|  4109K| 18940   (1)| 00:03:48 |
|   1 |  TABLE ACCESS BY INDEX ROWID | T_XIFENFEI        |   841K|  4109K| 18940   (1)| 00:03:48 |
|   2 |   BITMAP CONVERSION TO ROWIDS|                   |       |       |            |          |
|   3 |    BITMAP INDEX FULL SCAN    | BITMAP_T_XIFENFEI |       |       |            |          |
--------------------------------------------------------------------------------------------------


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
      91537  consistent gets
          0  physical reads
          0  redo size
   42362633  bytes sent via SQL*Net to client
     620649  bytes received via SQL*Net from client
      56382  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
     845708  rows processed

这里使用了一个无效的index,也使用了BITMAP_T_XIFENFEI,让人更加的感觉奇怪

原因分析
If the INDEX hint specifies no indexes, then the optimizer considers the cost of a scan on each available index on the table and then performs the index scan with the lowest cost. The database can also choose to scan multiple indexes and merge the results, if such an access path has the lowest cost. The optimizer does not consider a full table scan.
如果我们使用hint指定了一个无效的index,优化器会扫描表中所有可以使用的index,然后选择cost最小的index或者index组合,而不会选择全表扫描。
因为我们hint指定b_tree_t_xifenfei index的时候,因为object_id可能有值为空(列没定义为not null),所以不能使用该index,从而也就是相当于一个无效的index,从而扫描该表的其他可以使用的index,导致使用了位图索引(该类型index不排除null),而不是全表扫描.
温馨提示:使用hint指定index的时候需要慎重,如果不合适或者无效,可能导致程序效率更低

发表在 Oracle | 一条评论