DB2 DPF查詢分佈不均勻的表(轉)
阿新 • • 發佈:2021-06-30
DB2多分割槽資料庫(DPF)環境下,經常會遇到表空間節點分佈不均的現象。一般來說,主要原因有:
1、分佈鍵選擇不合理: 例如:誤選日期、地區程式碼、品牌編號等這類取值較為單一的欄位作為分佈鍵。 2、分佈鍵選擇合理,但欄位值本身分佈不均: 例如:以phone_no作為分佈鍵,但存在大量為空值的記錄。 以下是自己總結的方法,用於查詢分佈不均的表: 一、查詢存在分佈不均現象的表空間: 以4個數據節點、page size為32k的DB2資料庫(V9.7DPF)為例 --節點使用率差值排序,找出傾斜最大的表空間select tbspcname,(max(freeratio)-min(freeratio)) diff from ( select tbspcname,node,sum(total_GB) total_GB,sum(usable_GB) usable_GB,sum(used_GB) used_GB,sum(free_GB) free_GB ,freeratio from ( select substr(tablespace_name,1,30) as tbspcname,'NODE_00' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',0)) as aa where tablespace_type=0 union all select substr(tablespace_name,1,30) as tbspcname,'NODE_01' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',1)) as aa where tablespace_type=0 union all select substr(tablespace_name,1,30) as tbspcname,'NODE_02' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',2)) as aa where tablespace_type=0 union all select substr(tablespace_name,1,30) as tbspcname,'NODE_03' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',3)) as aa where tablespace_type=0 union all select substr(tablespace_name,1,30) as tbspcname,'NODE_04' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',4)) as aa where tablespace_type=0 ) ss Group by tbspcname,node,freeratio ) tt group by tbspcname order by diff desc ;
假設找出TBS_DATA表空間的分佈不均,節點間差值較大,下面進一步查詢分佈不均的表。
附錄: --查詢節點差值較大的節點號select tt.tbspcname,tt.node,tt.freeratio min_freeratio from ( select rank() over(partition by tbspcname order by freeratio) rank_id,ss.* from ( select tbspcname,node,freeratio from ( select substr(tablespace_name,1,30) as tbspcname,'NODE_00' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',0)) as aa where tablespace_type=0 union all select substr(tablespace_name,1,30) as tbspcname,'NODE_01' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',1)) as aa where tablespace_type=0 union all select substr(tablespace_name,1,30) as tbspcname,'NODE_02' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',2)) as aa where tablespace_type=0 union all select substr(tablespace_name,1,30) as tbspcname,'NODE_03' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',3)) as aa where tablespace_type=0 union all select substr(tablespace_name,1,30) as tbspcname,'NODE_04' node,total_pages*32/(1024*1024) total_GB,usable_pages*32/(1024*1024) usable_GB,used_pages*32/(1024*1024) used_GB,free_pages*32/(1024*1024) free_GB,free_pages*100.0/usable_pages as freeratio from table(snapshot_tbs_cfg('',4)) as aa where tablespace_type=0 ) rr where usable_GB <> 0 and tbspcname in ('TBS_DATA') group by tbspcname,node,freeratio order by tbspcname,node,freeratio ) ss ) tt where tt.rank_id=1 order by tt.tbspcname ;
二、查找出表空間內分佈不均的表:
生成統計SQL:按分佈鍵統計每個節點的資料量 --生成統計SQLselect 'select '''||lower(ltrim(rtrim(a.tabschema)))||'.'||lower(ltrim(rtrim(a.tabname)))||''' as tabname,dbpartitionnum('||b.colname||') node_id,count(*) count from '||lower(ltrim(rtrim(a.tabschema)))||'.'||lower(ltrim(rtrim(a.tabname)))||' group by dbpartitionnum('||b.colname||') order by count(*) with ur;' from syscat.tables a,syscat.columns b where a.tabschema=b.tabschema and a.tabname=b.tabname and a.type in ('T','S') and a.tbspace in ('TBS_DATA') and a.fpages > 0 and b.partkeyseq > 0 and b.colno=0 order by a.fpages asc with ur;
或者
db2 -x "select 'select '''||ltrim(rtrim(a.tabschema))||'.'||ltrim(rtrim(a.tabname))||''' as tabname,dbpartitionnum('||b.colname||'),count(*) from '||ltrim(rtrim(a.tabschema))||'.'||ltrim(rtrim(a.tabname))||' group by dbpartitionnum('||b.colname||') order by count(*) with ur;' from syscat.tables a,syscat.columns b where a.tabschema=b.tabschema and a.tabname=b.tabname and a.tbspace in ('TBS_DATA') and a.type in ('T','S') and a.fpages > 0 and b.partkeyseq > 0 and b.colno=0 order by a.fpages asc with ur" > dpf_tbspc_partition.sql
然後執行統計sql,生成統計結果dpf_tbspc_partition.log
db2 -tvf dpf_tbspc_partition.sql -z dpf_tbspc_partition.log
三、根據以上的統計結果(dpf_tbspc_partition.log),結合下面的分佈鍵資訊,綜合分析查詢分佈不均的表:
select a.tabschema,a.tabname,a.fpages*32/1024 size_mb,b.colno,b.colname,b.partkeyseq from syscat.tables a,syscat.columns b where a.tabschema=b.tabschema and a.tabname=b.tabname and a.type in ('T','S') and lower(ltrim(rtrim(a.tabschema)))||'.'lower(ltrim(rtrim(a.tabname)))='schema.tablename' order by b.partkeyseq desc,b.colno asc with ur ;