天天看點

跟屌絲大哥學習DB2---DB2 runstats、reorgchk、reorg 指令

1、runstats

runsats可以搜集表的資訊,也可以搜集索引資訊。作為runstats本身沒有優化的功能,但是它更新了統計資訊以後,可以讓db2優化器使用最新的統計資訊來進行優化,這樣優化的效果更好。

runstats   on   table   <tbschema>.<tbname>   收集表   <tbname>   的統計資訊。表名必須是用   <dbschema>   全限定的。

  2、reorg

 a、 reorg   table   <tbschema>.<tablename>   通過重構行來消除“碎片”資料并壓縮資訊,對表進行重組。表名必須是用   <dbschema>   全限定的。

  b、

reorg還有一個功能就是可以将表中的資料按照某個索引關鍵字的順序排列,進而可以減少某些查詢i/o的數量。

  執行reorg可以考慮分為表上有索引和沒有索引兩種情況: 

     a.如表名為db2inst1.staff,索引名為db2inst1.istaff

      reorg table db2inst1.staff index db2inst1.istaff use tempspace1

    b.建議reorg時使用use參數指定資料重排時使用的臨時表空間,否則,reorg工作将會

       在表所在表空間中原地執行.如果表上有多個索引,index參數值請使用最為重要的索

       引名.

    c.表上沒有索引:

       reorg table db2inst1.staff use tempspace1

      reorg table sysibm.systables use tempspace1

3、

讓db2系統定時runstats、reorg

q:定期runstats、reorg

a:在db2 v8.2以上可以使用 call sysproc.admin_cmd來實作,

這裡主要講在v8.2以前的版本中利用shell或者批處理來實作同樣的功能

因為在以前的版本中存儲過程中是不能使用ddl操作語句的!(這點對于oracle剛轉過來的人來說很是郁悶的)

然後可以利用db2自帶的配置自動維護來做,但是java做的東西比較讓人感覺頭痛!尤其是速度和莫名的錯誤!

本代碼使用作業系統的腳本來實作這部分功能!

1.windows下

如下:

下一個cmd檔案s.cmd

内容如下:

db2 connect to ccp_dm

db2 -x "select 'runstats on table '||rtrim(tabschema)||'.'||tabname||' on all columns' from sysstat.tables where card=-1">tab.sql

db2 -f tab.sql

--其中where後的條件可以修改

然後就是定制任務:用windos的定制任務!每周或者每月運作,這個就不講了哈!

這部分經測試,通過!

不過能,這裡隻提到了runstats,對于reorg同理也可以實作!

4、reorgchk

c:\documents and settings\administrator>db2 reorgchk update statistics on table all

正在執行 runstats ....

  reorgchk   on   table   all   确定是否需要對表進行重組。這對于對所有表自動執行 runstats很有用。   

1) 針對系統表進行reorgchk

db2 reorgchk update statistics on table system

使用update statistics參數指定資料庫首先執行runstats指令。

2) 針對使用者表進行reorgchk

db2 reorgchk update statistics on table user

下面是執行的部分結果

執行 runstats ....

db2 reorgchk 指令是最重要的、也是經常被忽略的 db2 調整指令之一。 db2 reorgchk 指令被忽略是因為它不是一個一次性調整項。由于更新是在 db2 資料庫上執行的,是以關于表的統計資訊将不會是最新的。db2 reorgchk 指令更新 db2 優化器所使用的重要統計資訊。建議在大約每 10,000 次更新後重複 db2 reorgchk 指令。

在運作 db2 reorgchk 指令之前,您應該停止 ibm directory server 以防止在指令執行的同時發生任何 db2 查詢或更新。雖然這是可選的,但資料庫查詢和更新可能會非常緩慢并有可能逾時。

請注意,運作 db2 reorgchk 指令所帶來的性能益處是即時的。不必在 db2 reorgchk 指令之後重新啟動 db2。

除了提高性能之外,db2 reorgchk 指令還報告關于資料庫中所有表和索引的統計資訊。db2 reorgchk 指令還報告關于 db2 表的組織的統計資訊。

====================================分割線================================

最新内容請見作者的github頁:http://qaseven.github.io/