前言:搞了10年的Oracle,最近开始学习DB2,从牛书《循序渐进DB2.DBA系统管理、运维与应用案例学习笔记》开始,将我学习过程中的测试命令及过程和大家共享,遇到的问题也如实写在上面,希望和大家一起学习!
大家也可以参考我的BLOG:
http://www.db2china.net/home/space.php?uid=45420&do=blog,欢迎大家指正,谢谢!
1.创建8K缓冲池
CREATE BUFFERPOOL bufferpool8k size 1000 automatic pagesize 8k
2.创建表空间
db2 "create tablespace sms8k pagesize 8k managed by system using('FSMS_8K_1') bufferpool bufferpool8k"
[db2inst1@localhost ~]$ db2 list tablespace containers for 6
Tablespace Containers for Tablespace 6
Container ID = 0
Name = /home/db2inst1/mydb/mydbpath1/db2inst1/NODE0000/SQL00001/FSMS_8K_1
Type = Path
3、创建对应缓冲快对应的临时表空间
create system temporary tablespace tmp_sms8k managed by system using('tmp_sms8k')
Q:系统临时表空间
IBM官方说法:至少应该有一个具有数据库中存在的用户表空间的各个页大小的系统临时表空间,否则某些查询可能会失败?为什么?
但是如何指定临时表空间的pagesize呢?
A:
(1)系统临时表空间用来存储各种数据操作(排序、重组表、创建索引、连接表)中所需的内部临时数据,虽
然可以创建任意多个系统临时表空间,但建议用户只使用大多数表所使用的页大小创建一个,默认系统临时表空间名为TEMPSPACE1。用户临时表空间用来存储已说明全局临时表(已说明全局临时表存储的是应用程序临时数据)。用户临时表空间不是在数据库创建时默认创建的。
(2)如何没有对应块大小的临时表空间,可能会遇到 SQL1585N A system temporary table space with sufficient page , size does not exist.
类似这样的错误,这个时候有可能就是因为默认的临时表空间不够造成的,如果这个排序发生在 32K 的表空间的话,一般推荐创建一个相应 pagesize 缓冲大小的临时表空间来排序。
3.表空间extend
[db2inst1@localhost ~]$ db2 "alter tablespace USERSPACE1 extend (all 20)"
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL20318N Table space "USERSPACE1" of type "AUTOMATIC STORAGE" cannot be
altered using the "EXTEND" operation. SQLSTATE=42858
--自动管理存储不能做EXTEND
4.表空间resize/reduce
[db2inst1@localhost ~]$ db2 list tablespace containers for 5
Tablespace Containers for Tablespace 5
Container ID = 0
Name = /home/db2inst1/mydb/dmspath1/dms1.dat
Type = File
Container ID = 1
Name = /home/db2inst1/mydb/dmspath1/dms2.dat
Type = File
[db2inst1@localhost ~]$ db2 "alter tablespace DMSSPACE1 reduce(FILE '/home/db2inst1/mydb/dmspath1/dms1.dat' 500)"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "alter tablespace DMSSPACE1 resize(FILE '/home/db2inst1/mydb/dmspath1/dms1.dat' 500)"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$
5.表空间drop FILE
[db2inst1@localhost ~]$ db2 "alter tablespace DMSSPACE1 DROP (FILE '/home/db2inst1/mydb/dmspath1/dms1.dat')"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "alter tablespace DMSSPACE1 DROP (FILE '/home/db2inst1/mydb/dmspath1/dms2.dat')"
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL20170N There is not enough space in the table space "DMSSPACE1" for the
specified action. Reason code = "1". SQLSTATE=57059
--不能删除表空间的最后一个FILE
[db2inst1@localhost ~]$ db2 list tablespace containers for 5
Tablespace Containers for Tablespace 5
Container ID = 0
Name = /home/db2inst1/mydb/dmspath1/dms2.dat
Type = File
[db2inst1@localhost ~]$
6、获取tablespace 预取设置
db2 get snapshot for tablespaces on mydb >mydb.snp
[db2inst1@localhost ~]$ grep "Prefetch size" mydb.snp
Automatic Prefetch size enabled = Yes
Tablespace Prefetch size (pages) = 8
Automatic Prefetch size enabled = Yes
Tablespace Prefetch size (pages) = 64
Automatic Prefetch size enabled = Yes
Tablespace Prefetch size (pages) = 64
Automatic Prefetch size enabled = Yes
Tablespace Prefetch size (pages) = 8
Automatic Prefetch size enabled = Yes
Tablespace Prefetch size (pages) = 32
Automatic Prefetch size enabled = Yes
Tablespace Prefetch size (pages) = 32
Automatic Prefetch size enabled = Yes
Tablespace Prefetch size (pages) = 32
Automatic Prefetch size enabled = Yes
Tablespace Prefetch size (pages) = 8
7、取消tablespace caching(可以加快读取性能?)
db2 alter tablespace dmsspace1 no file system caching
Tablespace name = DMSSPACE1
Tablespace ID = 5
Tablespace Type = Database managed space
Tablespace Content Type = All permanent data. Large table space.
Tablespace Page size (bytes) = 4096
Tablespace Extent size (pages) = 32
Automatic Prefetch size enabled = Yes
Buffer pool ID currently in use = 1
Buffer pool ID next startup = 1
Using automatic storage = No
Auto-resize enabled = No
File system caching = No
Tablespace State = 0x'00000000'
Detailed explanation:
Normal
--临时表空间的pagesize 应至少比数据表空间大1个页大小,同时应大于查询结果集的最好行记录大小
[db2inst1@localhost ~]$ db2 "CREATE BUFFERPOOL bufferpool16k size 1000 automatic pagesize 16k"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "create system temporary tablespace TEMPSPACE16K pagesize 16k managed by system using ('/home/db2inst1/mydb/mydbpath2/db2inst1/NODE0000/MYDB/TMP_TBSP') bufferpool bufferpool16k"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 list tablespace containers for 8
Tablespace Containers for Tablespace 8
Container ID = 0
Name = /home/db2inst1/mydb/mydbpath2/db2inst1/NODE0000/MYDB/TMP_TBSP
Type = Path
8、基于块的缓冲池,主要用于OLAP/DSS的频繁读取的表空间
db2 create bufferpool block_bp size 4096 numblockpages 2048 blocksize 128