在 DB2 數據庫維護中,表空間變更或跨庫數據遷移是常見需求。本文基於實測環境(IBM x346、DB2 V8.2.4、Win2000、DMS 表空間),分享一套經驗證的「導出 DDL → 修改腳本 → 建庫建表 → 批量 Export/Import」遷移方案,實測載入速度可達 60–100 萬行/分鐘。
db2 "export to ... of ixf select * from ..." 命令。db2 "load from ... of 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 tables = new ArrayList<>();
while (result.next()) tables.add(result.getString(1));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tables.size(); i++) {
sb.append("db2 \"export to aa").append(i+1).append(".ixf of ixf select * from ").append(tables.get(i)).append("\"\r\n");
}
FileUtility.saveStringToFile(filePath, sb.toString(), false);
}
public void createLoadFile(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 tables = new ArrayList<>();
while (result.next()) tables.add(result.getString(1));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tables.size(); i++) {
sb.append("db2 \"load from aa").append(i+1).append(".ixf of ixf replace into ")
.append(tables.get(i)).append(" copy no without prompting\"\r\n");
}
FileUtility.saveStringToFile(filePath, sb.toString(), false);
}
# export 腳本片段
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
# load 腳本片段
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
runstats 並備份統計信息,遷移後立即更新統計;這套「DDL 先行、IXF 中轉、Load 裝載」流程結構清晰、腳本可複用、易於自動化,適合中大型 DB2 遷移項目。實戰中配合表空間規劃、並行度調優,可穩定在百萬行/分鐘級別吞吐。