数据库
首页 > 数据库> > DB2检查数据在各节点分布情况

DB2检查数据在各节点分布情况

作者:互联网

情景:总所周知,DB2的表空间(数据)在节点中存储是根据每张表的分区键来分布的,如果分区键建的不好,会直接导致表空间在各节点的占用大小不均匀,久而久之,其中一个或几个节点的大小已所剩无几,其他的却依旧充足,这样就不得不扩展空间不足的节点大小,对性能也会有所影响。

 

检查:

1、查看表空间使用情况,YOUR_SPACENAME为查看的表空间名

SELECT * FROM SYSIBMADM.TBSP_UTILIZATION WHERE TBSP_NAME='YOUR_SPACENAME';

每个节点各位一条数据,'DBPARTITIONNUM'为节点序号,'TBSP_UTILIZATION_PERCENT'为各节点使用比例,'TBSP_ID'为表空间序号

2、选取其中使用率较高与较低的两组节点进行比较每张表的数据分布情况,$1、$3为节点序号,$2为表空间序号,$4为schema名称

1 db2 -x "select 'select '''||trim(a.tabschema)||'.'||trim(a.tabname)||''' as tabname, count(*) from '||trim(a.tabschema)||'.'||trim(a.tabname)||' where dbpartitionnum('||b.colname||')=$1 with ur;' from syscat.tables a ,syscat.columns b where a.tbspaceid=$2  and a.tabschema=b.tabschema  and a.tabschema=$4 and a.tabname=b.tabname and b.COLNo=0 " > cnt1.sql
2 db2 -txf cnt1.sql > cnt1.out
3 db2 -x "select 'select '''||trim(a.tabschema)||'.'||trim(a.tabname)||''' as tabname, count(*) from '||trim(a.tabschema)||'.'||trim(a.tabname)||' where dbpartitionnum('||b.colname||')=$3 with ur;' from syscat.tables a ,syscat.columns b where a.tbspaceid=$2  and a.tabschema=b.tabschema  and a.tabschema=$4 and a.tabname=b.tabname and b.COLNo=0 " > cnt2.sql
4 db2 -txf cnt2.sql > cnt2.out
5 diff cnt1.out cnt2.out>diff.out

输出结果diff.out即此schema下所有表在两个节点数据分布差异情况

3、找出记录数相差很大的表,查看表的SQL定义

db2look -d [dbname] -i [userName] -w [password] -z [schemaName] -t [tableName] -e -nofed -o tableddl.sql

或者使用连接工具生成DDL查看

4、根据实际情况进行数据清理或者重新建表

 

补充:也可直接查看某一具体表在个节点的数据分布

select 'aaa.bbb' as tabname,count(*) as cnt ,dbpartitionnum(hash字段) from aaa.bbb group by dbpartitionnum(hash字段) with ur;

 

标签:trim,tabname,检查数据,tabschema,DB2,节点,select,out
来源: https://www.cnblogs.com/hagendaz/p/11550953.html