日期:2014-05-16  浏览次数:20404 次

windows下db2维护的脚本

最近受命帮其他项目组做一个windows版本的db2维护脚步,其实人家有Linux上的shell文件,只是让我们翻译成windows版本的。借此机会熟悉了下windows下的批处理文件的书写。维护的代码如下

下面这段是对约束的检查,而且是有必要的,在数据刚load后其数据相当于是锁定的,并不能执行太多的操作

 set database=tbmprd

set schema=tbmprd

::此脚步的目的是执行后边的select 'set integrity for '语句对数据表进行约束的检查
echo 对于pending的一些表进行处理

echo 连接到数据库 db2 connect to %database%
db2 connect to %database%

echo 找到所有的表,然后执行set integrity for %schema%.'||TABNAME||' immediate checked
db2 "select 'set integrity for %schema%.'||TABNAME||' immediate checked;' from syscat.tables where upper(TABSCHEMA)=upper('%schema%') and STATUS='C' order by TABNAME" > temp.sql
findstr  "set integrity" temp.sql > unpending.sql


echo db2 -tvf unpending.sql > tbmprd_unpending.log
db2 -tvf unpending.sql >> tbmprd_unpending.log
pause
echo 找到所有有关联的表,这些表不能按照上面的单表pending的解决办法
echo db2 "select '%schema%.'||TABNAME||',' from syscat.tables where upper(TABSCHEMA)=upper('%schema%') and STATUS='C' order by TABNAME"
db2 "select '%schema%.'||TABNAME||',' from syscat.tables where upper(TABSCHEMA)=upper('%schema%') and STATUS='C' order by TABNAME"> temp.sql
findstr? "%schema%" temp.sql > unpending.sql
echo set integrity for? > temp.sql
type unpending.sql >> temp.sql
::此段的目的在于将temp.sql文件的最后一行的,号替换成 immediate checked。
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (temp.sql) do set /a line+=1
for /f "delims=" %%i in (temp.sql) do (
? set /a n+=1
? if not {!N!}=={!line!} >> unpendingtemp.sql echo %%i
? if {!N!}=={!line!} for /f "delims=," %%j in ("%%i") do >>unpendingtemp.sql echo %%j immediate checked;&goto :final
)
:final
SETLOCAL DISABLEDELAYEDEXPANSION
@echo on

::sed '$s/,/ immediate checked;/g' temp.sql>unpendingtemp.sql
::echo set integrity for  > unpending.sql
::findstr  "%schema%" unpendingtemp.sql >> unpending.sql

echo db2 -tvf unpending.sql >> tbmprd_unpending.log
db2 -tvf unpending.sql >> tbmprd_unpending.log

del temp.sql
del unpending.sql

db2 "select 'set integrity for %schema%.'||TABNAME||' immediate checked;' from syscat.tables where upper(TABSCHEMA)=upper('%schema}') and STATUS='C' order by TABNAME" >> tbmprd_unpending.log

echo -----------pending表已经处理完毕,参阅tbmprd_unpending.log------------

?下面的是对表的重组(reorg),运行统计(runstats),绑定(bind)

::变量 创建的数据库名
set database=tbmprd
::变量 创建的schema
set schema=tbmprd

echo --------------------------对于表进行reorg处理-------------------------------------

echo 连接到数据库 db2 connect to %database%
db2 connect to %database%


echo 对于目前所有的表进行reorg处理
echo -----------正在进行reorg相关处理,请耐心等待------------

db2 "select 'reorg table '||rtrim(tabschema)||'.'||tabname||';' from syscat.tables where type='T' and upper(TABSCHEMA)=upper('%schema%')"  > temp.sql
findstr  reorg  temp.sql > reorg.sql

db2 "select 'reorg indexes all for table '||rtrim(tabschema)||'.'||tabname||';' from syscat.tables where type='T' and upper(TABSCHEMA)=upper('%schema%')"  > temp.sql
findstr  reorg  temp.sql > reorg_index.sql

db2 "select 'runstats on table '||rtrim(tabschema)||'.'||tabname||' and indexes all;' from syscat.tables where type='T' and upper(TABSCHEMA)=upper('%schema%') "  > temp.sql
findstr  runstats  temp.sql > runstats.sql

db2 "select 'rebind package '||rtrim(pkgschema)||'.'||pkgname||';' from syscat.packages where pkgschema not in ('NULLID')"  > temp.sql
findstr  rebind  temp.sql > rebind.sql

db2 -tvf reorg.sql > tbmprd_runstats.log
db2 -tvf reorg_index.sql >> tbmprd_runstats.log
db2 -tvf runstats.sql >> tbmprd_runstats.log
db2 -tvf rebind.sql >> tbmprd_runstats.log
del temp.sql
del reorg.sql
del reorg_index.sql
del runstats.sql
del rebind.sql

echo -----------reorg表已经处理完毕,参阅tbmprd_runstats.log------------

?