DB2 Maintenance
DB2维护任务:
1. 收集统计信息
表:runstats on table <schema.tablename>
表和索引:runstats on table <schema.tablename> and indexes all
索引:runstats on table <schema.tablename> for indexes all
查看表是否有收集信息:select tabname,card from syscat.tables where tabname=<table_name>
当card为-1说明需要收集
查看没有统计信息的表:select tabname from syscat.tables where stats_time is null or stats_time in ('-1')
查看没有统计信息的索引:select indname from syscat.indexes where stats_time is null or stats_time in ('-1')
查看30天没有更新统计信息的表和索引:select tabname from syscat.tables where stats_time < current_timestamp - 30 days
select indname from syscat.indexes where stats_time < current_timestamp - 30 days
2. 更新统计信息
收集单个表:reorgchk update statictics on table <schema.tablename>
收集所有表:reorgchk update statistics on table all
收集单个模式:reorgchk update statistics on schema <schemaname>
3. 重组:回收浪费的空间并重新组织数据
a. 更新统计数据:reorgchk update statistics on table all
b. 查看哪些表需要重组:select tabschema, tabname, overflow from syscat.tables order by overflow desc fetch first 10 rows only
当overflow很大说明需要reorg
c. 重组表:reorg table <schemaname.tablename>
d. 重组索引:reorg indexes all for table <schemaname.tablename>
e. 重新收集统计信息:runstats on table <schemaname.tablename> and indexes all
4. 重绑定:是DB2使用新统计的信息
命令:db2 rebind database_alias -l <logfile>
维护方式:
1. 手动维护
2. 创建维护脚本
3. 自动维护

制作定时执行脚本:1. 产生语句:
db2 connect to <dbname>
db2 "select 'reorg table '||rtrim(tabschema)||'.'||tabname||';' from syscat.tables where type='T'" > reorg.sql
db2 "select 'reorg indexes all for table '||rtrim(tabschema)||'.'||tabname||';' from syscat.tables where type='T'" > reorg_index.sql
db2 "select 'runstats on table '||rtrim(tabschema)||'.'||tabname||' and indexes all;' from syscat.tables where type='T'" > runstats.sql
db2 "select 'rebind package '||rtrim(pkgschema)||'.'||pkgname||';' from syscat.packages where pkgschema not in('NULLID')" > rebind.sql
2. 处理语句产生的文件:
去掉开头的多余符号
3. 编写sh文件:
db2 connect to <dbname>
db2 -tvf reorg.sql -z reorg_error.log
db2 -tvf reorg_index.sql -z reorg_index_error.log
db2 -tvf runstats.sql -z runstats_error.log
db2 -tvf rebind.sql -z rebind_error.log
1. 收集统计信息
表:runstats on table <schema.tablename>
表和索引:runstats on table <schema.tablename> and indexes all
索引:runstats on table <schema.tablename> for indexes all
查看表是否有收集信息:select tabname,card from syscat.tables where tabname=<table_name>
当card为-1说明需要收集
查看没有统计信息的表:select tabname from syscat.tables where stats_time is null or stats_time in ('-1')
查看没有统计信息的索引:select indname from syscat.indexes where stats_time is null or stats_time in ('-1')
查看30天没有更新统计信息的表和索引:select tabname from syscat.tables where stats_time < current_timestamp - 30 days
select indname from syscat.indexes where stats_time < current_timestamp - 30 days
2. 更新统计信息
收集单个表:reorgchk update statictics on table <schema.tablename>
收集所有表:reorgchk update statistics on table all
收集单个模式:reorgchk update statistics on schema <schemaname>
3. 重组:回收浪费的空间并重新组织数据
a. 更新统计数据:reorgchk update statistics on table all
b. 查看哪些表需要重组:select tabschema, tabname, overflow from syscat.tables order by overflow desc fetch first 10 rows only
当overflow很大说明需要reorg
c. 重组表:reorg table <schemaname.tablename>
d. 重组索引:reorg indexes all for table <schemaname.tablename>
e. 重新收集统计信息:runstats on table <schemaname.tablename> and indexes all
4. 重绑定:是DB2使用新统计的信息
命令:db2 rebind database_alias -l <logfile>
维护方式:
1. 手动维护
2. 创建维护脚本
3. 自动维护

制作定时执行脚本:1. 产生语句:
db2 connect to <dbname>
db2 "select 'reorg table '||rtrim(tabschema)||'.'||tabname||';' from syscat.tables where type='T'" > reorg.sql
db2 "select 'reorg indexes all for table '||rtrim(tabschema)||'.'||tabname||';' from syscat.tables where type='T'" > reorg_index.sql
db2 "select 'runstats on table '||rtrim(tabschema)||'.'||tabname||' and indexes all;' from syscat.tables where type='T'" > runstats.sql
db2 "select 'rebind package '||rtrim(pkgschema)||'.'||pkgname||';' from syscat.packages where pkgschema not in('NULLID')" > rebind.sql
2. 处理语句产生的文件:
去掉开头的多余符号
3. 编写sh文件:
db2 connect to <dbname>
db2 -tvf reorg.sql -z reorg_error.log
db2 -tvf reorg_index.sql -z reorg_index_error.log
db2 -tvf runstats.sql -z runstats_error.log
db2 -tvf rebind.sql -z rebind_error.log
Comments
Post a Comment