適用場景
- 變更表空間類型 (如 SMS → DMS)
- 跨資料庫/跨實例整庫遷移
- 測試環境從生產環境同步資料
標準化遷移流程 (8 步驟)
- DB2 Control Center 導出全庫 DDL 腳本
- 修改 DDL:調整表空間、緩衝池、分區策略等
- 建立目標資料庫 + 目標 DMS 表空間 (如
GATHER)
- 執行修改後 DDL 建立空表結構
- **自動生成 Export 腳本** (查詢
SYSCAT.TABLES)
- **自動生成 Load 腳本** (同源表清單)
- 源庫執行 Export (IXF 格式)
- 目標庫執行 Load (高效批次)
核心自動化代碼 (Java/JDBC)
Export 腳本生成器
public void createExportFile(Connection conn, String creator, String filePath) {
String sql = "select tabname from syscat.tables where tabschema = '" + creator + "' and type='T'";
List<String> tables = queryForList(conn, sql);
StringBuilder sb = new StringBuilder();
for (int i=0; i
Load 腳本生成器
public void createLoadFile(Connection conn, String creator, String filePath) {
String sql = "select tabname from syscat.tables where tabschema = '" + creator + "' and type='T'";
List<String> tables = queryForList(conn, sql);
StringBuilder sb = new StringBuilder();
for (int i=0; i
執行範例腳本
Export 階段
db2 connect to srcdb user srcusr using srcpwd
db2 "export to aa1.ixf of ixf select * from table1"
db2 "export to aa2.ixf of ixf select * from table2"
...
db2 connect reset
Load 階段
db2 connect to tgtdb user tgtusr using tgtpwd
db2 "load from aa1.ixf of ixf replace into table1 COPY NO without prompting"
db2 "load from aa2.ixf of ixf replace into table2 COPY NO without prompting"
...
db2 connect reset
效能基準與關鍵參數
- 環境:IBM x346, 3.2GHz×2, 4GB, RAID 1, DB2 V8.2.4, Win2000 Adv, **DMS 表空間**
- Load 速度:**60-100 萬行/分**
- 關鍵加速參數:
COPY NO:不建立複製映像 (需歸檔日誌環境下事後備份解除 Backup Pending)WITHOUT PROMPTING:無人值守- 預調
UTIL_HEAP_SZ、LOGBUFSZ、LOGFILSIZ - 目標表預建索引?**Load 重建索引更快**,建議載入前 drop 索引,載入後 recreate
遷移後驗收
runstats on table <schema>.<table> and indexes all
reorg table <schema>.<table> (如需)
- 應用端連線測試、資料筆數核對、檢查約束/觸發程序狀態