1. 日志查看

  1. # 查看DB2diag日志的路径(一般路径为:/home/db2inst1/sqllib/db2dump)
  2. # 方式1
  3. db2 get dbm cfg show detail|grep -i diag
  4. # 方式2:看最后的输出就知道在哪里了
  5. db2diag

2. 连接数设置

2.1. 数据库管理器配置

  1. # 数据库管理器配置
  2. # 查看最大连接数
  3. db2 get dbm cfg
  4. # Max number of coordinating agents (MAX_COORDAGENTS) = AUTOMATIC(200)
  5. # Max number of client connections (MAX_CONNECTIONS) = AUTOMATIC(MAX_COORDAGENTS)
  6. # 设置最大连接数
  7. db2 update dbm cfg using <parameter_name> <value>
  8. db2 update dbm cfg using max_coordagents 1000 automatic max_connections -1 automatic

2.2. 数据库配置

  1. # 数据库配置
  2. # 查看最大连接数
  3. db2 get db cfg for deskdb
  4. db2 get db cfg for deskdb | grep -i maxappls
  5. # Max number of active applications (MAXAPPLS) = AUTOMATIC(40)
  6. # Average number of active applications (AVG_APPLS) = AUTOMATIC(1)
  7. # 设置最大连接数
  8. db2 update db cfg for <db-name> using maxappls <new number>
  9. db2 update db cfg for deskdb using MAXAPPLS 200
  10. # 恢复默认设置命令
  11. db2 reset db cfg

2.3. 实例查看

  1. # 查看当前连接数
  2. db2 list applications for db deskdb
  3. db2 list applications for db deskdb | wc -l
  4. db2 list applications for db deskdb show detail

2.4. 活跃连接数查看

  1. -- 方式1
  2. SELECT COUNT(*) as num FROM SYSIBMADM.APPL_PERFORMANCE;
  3. -- 方式2
  4. SELECT COUNT(*) as num FROM TABLE(SYSPROC.SNAP_GET_APPL('BDAPDB',0));
  5. SELECT COUNT(*) as num FROM TABLE(SYSPROC.SNAP_GET_APPL(NULL,0));
  6. SELECT COUNT(*) as num FROM TABLE(SYSPROC.SNAP_GET_APPL_INFO('BDAPDB',-2));

3. 表空间操作

3.1. 表空间使用率

  1. db2 "select substr(a.tbsp_name,1,18) as name,substr(a.tbsp_type,1,10) as
  2. tbstype,a.TBSP_USING_AUTO_STORAGE as AUTO_STORAGE,substr(a.tbsp_state,1,8) as
  3. state,a.tbsp_total_size_kb/1024 as TotalMB ,a.TBSP_PAGE_TOP*a.TBSP_PAGE_SIZE/1024/1024 as
  4. top_size_mb,a.tbsp_used_size_kb/1024 as UsedMB, a.TBSP_UTILIZATION_PERCENT as
  5. UsedPer ,b.CONTAINER_NAME from sysibmadm.tbsp_utilization a, sysibmadm.SNAPCONTAINER
  6. b where tbsp_type='DMS' and a.TBSP_ID=b.TBSP_ID order by AUTO_STORAGE,UsedPer desc"

3.2. 创建表空间

  1. su - db2inst1
  2. db2 connect to deskdb
  3. # 创建表空间
  4. db2 create tablespace TBS_OTH_DAT managed by automatic storage
  5. db2 create tablespace TBS_OTH_IDX managed by automatic storage
  6. # 删除表空间
  7. db2 drop tablespace TBS_OTH_DAT
  8. db2 drop tablespace TBS_OTH_IDX
  9. # 创建缓冲池(size表示页数,pagesize表示页大小,size*pagesize就是缓冲池的内存大小)
  10. db2 create bufferpool bp16k size 1000 pagesize 16k
  11. # 先删除之前创建的表空间(注意:确保目录为空)
  12. rm -rf /data/db2/TBS_OTH_DAT
  13. rm -rf /data/db2/TBS_OTH_IDX
  14. rm -rf /data/db2/TBS_TEMP
  15. chown -R db2inst1:db2iadm1 /data/db2
  16. db2 "create tablespace TBS_OTH_DAT pagesize 16k managed by database using(file '/data/db2/TBS_OTH_DAT' 10G) bufferpool bp16k"
  17. db2 "create tablespace TBS_OTH_IDX pagesize 16k managed by database using(file '/data/db2/TBS_OTH_IDX' 1G) bufferpool bp16k"
  18. db2 "create TEMPORARY tablespace TBS_TEMP pagesize 16k managed by database using(file '/data/db2/TBS_TEMP' 1G) bufferpool bp16k"

3.3. 扩容

  1. db2 "alter tablespace <tablespace_name> extend (all <page_numbers>)"
  2. db2 alter "tablespace IBMDB2SAMPLEREL extend (all 1000)"
  3. db2 "alter tablespace <tablespace_name> add (FILE '<new_container_file>' <size>)"
  4. alter tablespace nnc_index01 add (file '\db2\nnc_index01_2' 204800)

4. 导入导出操作

4.1. CSV导出

  1. # 1. 生成列标题
  2. db2 "EXPORT TO /tmp/header.csv of del
  3. SELECT
  4. SUBSTR(REPLACE(REPLACE(XMLSERIALIZE(CONTENT XMLAGG(XMLELEMENT(NAME c,colname)
  5. ORDER BY colno) AS VARCHAR(1500)),'<C>',', '),'</C>',''),3)
  6. FROM syscat.columns WHERE tabschema=${SCHEMA} and tabname=${TABLE}"
  7. # 2. 查询主体
  8. db2 "EXPORT TO /tmp/body.csv of del SELECT * FROM ${SCHEMA}.${TABLE}"
  9. # 3. 合并
  10. cat /tmp/header.csv /tmp/body.csv > ${TABLE}.csv

4.2. 管道方式导入导出

  • 示例1(导出,已验证) ```sql mkfifo aa.fifo

db2 “export to aa.fifo of del select * from xxx”

db2 “import from aa.fifo of del insert into xxx”

  1. - **示例2(导出,未测试)**
  2. ```bash
  3. mkfifo ${target_file_tmp}
  4. (( $? != 0 )) && print "\nERROR: failed to create named pipe ${target_file_tmp}" && exit 1
  5. db2 -v "EXPORT TO ${target_file_header} of del SELECT 'COL1', 'COL2', 'COL3' FROM sysibm.sysdummy1 "
  6. cat ${target_file_header} ${target_file_tmp} >> ${target_file} &
  7. (( $? > 0 )) && print "Failed to append ${target_file} . Check permissions and free space" && exit 1
  8. db2 -v "EXPORT TO ${target_file_tmp} of del SELECT COL1, COL2, COL3 FROM myTable ORDER BY 1 "
  9. rc=$?
  10. (( rc == 1 )) && print "Export found no rows matching the query" && exit 1
  11. (( rc == 2 )) && print "Export completed with warnings, your data might not be what you expect" && exit 1
  12. (( rc > 2 )) && print "Export failed. Check the messages from export" && exit 1
  • 示例3(导入,未测试) ```sql — 创建表并启动 LOAD 的示例 DB2 脚本 CONNECT TO SAMPLE;

CREATE TABLE PIPE_TABLE ( “ID” NUMBER(10) NOT NULL , “NAME” VARCHAR2(35) , “LOC_ID” NUMBER(4)
);

LOAD FROM “\.\pipe\mynamedpipe” OF DEL MODIFIED BY CODEPAGE=1208 COLDEL~ ANYORDER
USEDEFAULTS CHARDEL”” DELPRIORITYCHAR NOROWWARNINGS METHOD P (1,2,3) MESSAGES “db2tabledata.txt” REPLACE INTO PIPE_TABLE ( “ID”, “NAME”, “LOC_ID” ) NONRECOVERABLE INDEXING MODE AUTOSELECT ;

TERMINATE;

  1. <a name="JtakJ"></a>
  2. ## 4.3. 游标方式导入
  3. 1. 远程数据库导入。
  4. ```bash
  5. -- 1. 编目
  6. CATALOG TCPIP NODE TargetND REMOTE xxx.xxx.xxx.xxx SERVER 50001;
  7. CATALOG DATABASE targetdb AT NODE TargetND;
  8. db2 connect to targetdb
  9. db2 "declare mycursor cursor database localdb user db2page using D4rkh4wk for select * from $SCHEMA.$TABLE"
  10. db2 "load from mycursor of cursor insert into $SCHEMA.$TABLE nonrecoverable"
  11. db2 connect reset
  1. 本地数据库导入。
    1. db2 connect to localdb
    2. db2 "declare mycursor cursor for select * from tabschema.tabname"
    3. db2 "load from mycursor of cursor insert into tabschema.tabname nonrecoverable"
    4. db2 connect reset

    5. 重建操作

    5.1. 重建表空间

    1. select 'reorg table ' || tabschema||'.'||tabname|| ' use tempspace1;' from syscat.tables where tbspaceid=3

    6. 表查询

    6.1. 行列转置

    6.1.1. 行转列

  • 样例数据 ```sql YEAR QUARTER RESULTS

2004 1 20 2004 2 30 2004 3 15 2004 4 10 2005 1 18 2005 2 40 2005 3 12 2005 4 27

  1. - **建表语句**
  2. ```sql
  3. CREATE TABLE Sales (Year INT, Quarter INT, Results INT);
  • SQL语句

    1. SELECT
    2. Year,
    3. MAX(CASE WHEN Quarter = 1 THEN Results END) AS Q1,
    4. MAX(CASE WHEN Quarter = 2 THEN Results END) AS Q2,
    5. MAX(CASE WHEN Quarter = 3 THEN Results END) AS Q3,
    6. MAX(CASE WHEN Quarter = 4 THEN Results END) AS Q4
    7. FROM Sales
    8. GROUP BY Year
  • 效果

    • 正常SQL效果如下: ```sql YEAR Q1 Q2 Q3 Q4

  1. 2004 20 30 15 10
  2. 2005 18 40 12 27
  1. - **不加`MAX()`效果如下:**
  2. ```sql
  3. YEAR Q1 Q2 Q3 Q4
  4. ----------- ----------- ----------- ----------- -----------
  5. 2004 20 - - -
  6. 2004 - 30 - -
  7. 2004 - - 15 -
  8. 2004 - - - 10
  9. 2005 18 - - -
  10. 2005 - 40 - -
  11. 2005 - - 12 -
  12. 2005 - - - 27

6.1.2. 列转行

  • 样例数据 ```sql YEAR Q1 Q2 Q3 Q4

2004 20 30 15 10 2005 18 40 12 27

  1. - **建表语句**
  2. ```sql
  3. CREATE TABLE SalesAgg(
  4. year INTEGER,
  5. q1 INTEGER,
  6. q2 INTEGER,
  7. q3 INTEGER,
  8. q4 INTEGER
  9. );
  • SQL语句

    1. -- table函数创建虚表,跟from子句中的表产生了关系,取出表中的一列作为数据
    2. SELECT
    3. S.Year,
    4. Q.Quarter,
    5. Q.Results
    6. FROM SalesAgg AS S,
    7. TABLE(VALUES(1, S.q1),(2, S.q2),(3, S.q3),(4, S.q4)) AS Q(Quarter, Results);
  • 效果 ```sql

    1. YEAR QUARTER RESULTS

  1. 2004 1 20
  2. 2004 2 30
  3. 2004 3 15
  4. 2004 4 10
  5. 2005 1 18
  6. 2005 2 40
  7. 2005 3 12
  8. 2005 4 27
  1. - **补充**
  2. ```sql
  3. -- 示例:用values实现多行表(虚表)
  4. select * from (values (1,2),(2,3)) as t1(col1,col2);
  5. -- 执行结果如下:
  6. COL1 COL2
  7. ----------- -----------
  8. 1 2
  9. 2 3

参考

ChinaUnix:DB2数据仓库高效迁移数据的5个方法
http://blog.chinaunix.net/uid-25723371-id-5046698.html