本文介紹一套經生產驗證(IBM x346, DB2 V8.2.4, Win2000, DMS 表空間)的 DB2 資料遷移標準化流程,核心思路:導出 DDL → 修改表空間 → 建立目標庫 → 導出/導入資料,並提供 Java 程式碼自動生成 EXPORT/LOAD 腳本,實現百萬級資料每分鐘 60-100 萬行的高效遷移。
db2cc) 選中所有表 → 產生 DDL 腳本。GATHER)、調整緩衝池、分區鍵等。db2 create database targetdb。GATHER。SYSCAT.TABLES 產生每表一條 EXPORT TO ... IXF 命令。LOAD FROM ... IXF REPLACE INTO ... COPY NO WITHOUT PROMPTING。public void createExportFile(Connection conn, String creator, String filePath) throws Exception {
DBBase dbBase = new DBBase(conn);
String sql = "select name from sysibm.systables where creator = '" + creator + "' and type='T'";
dbBase.executeQuery(sql);
DBResult result = dbBase.getSelectDBResult();
List list = new ArrayList<>();
while (result.next()) list.add(result.getString(1));
dbBase.close();
StringBuffer sb = new StringBuffer();
String enterFlag = "\r\n";
for (int i = 0; i < list.size(); i++) {
String tableName = list.get(i);
sb.append("db2 \"export to aa" + (i+1) + ".ixf of ixf select * from " + tableName + "\"" + enterFlag);
}
FileUtility.saveStringToFile(filePath, sb.toString(), false);
}
public void createLoadFile(Connection conn, String creator, String filePath) throws Exception {
// ... 查詢邏輯相同 ...
for (int i = 0; i < list.size(); i++) {
String tableName = list.get(i);
sb.append("db2 \"load from aa" + (i+1) + ".ixf of ixf replace into " + tableName + " COPY NO without prompting\"" + enterFlag);
}
FileUtility.saveStringToFile(filePath, sb.toString(), false);
}
db2 connect to testdb user test password test
db2 "export to aa1.ixf of ixf select * from table1"
db2 "export to aa2.ixf of ixf select * from table2"
db2 connect reset
db2 connect to testdb user test password test
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
LOAD 不記日誌、不觸發 Trigger、繞過約束檢查(需事後 SET INTEGRITY),速度比 IMPORT 快 10-100 倍。COPY YES USE TSM。SET INTEGRITY PENDING,全量載入完成後統一執行 SET INTEGRITY FOR table IMMEDIATE CHECKED。此方案結合 db2look (導出完整 DDL 含索引/觸發器/權限) 與自動化資料腳本,構成 DB2 異構/同構遷移的工業級標準作法。