首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> DB2 資料庫 >> DB2 教學

IBM DB2 數據複製與遷移實戰指南:Export/Import 高效實踐

     編輯:DB2 教學

前言

在 DB2 數據庫維護中,表空間變更或跨庫數據遷移是常見需求。本文基於實測環境(IBM x346、DB2 V8.2.4、Win2000、DMS 表空間),分享一套經驗證的「導出 DDL → 修改腳本 → 建庫建表 → 批量 Export/Import」遷移方案,實測載入速度可達 60–100 萬行/分鐘。

適用場景

  • 需變更現有表空間(如改為 GATHER 等自定義表空間)
  • 需將全庫數據遷移至新建數據庫
  • 跨平台、跨版本的異構遷移前置準備

標準化遷移 8 步驟

  1. 導出結構:使用 db2cc(控制中心)勾選源庫所有表,生成 DDL 腳本。
  2. 調整腳本:按需修改表空間子句、分區策略、壓縮屬性等。
  3. 建立目標庫:創建新數據庫並預先定義 DMS 表空間(如 GATHER)。
  4. 執行 DDL:在目標庫運行調整後的腳本,完成空表創建。
  5. 生成 Export 腳本:編寫程式查詢系統表,自動產生每張表的 db2 "export to ... of ixf select * from ..." 命令。
  6. 生成 Import/Load 腳本:同理產生 db2 "load from ... of ixf replace into ... copy no without prompting" 命令。
  7. 執行導出:連接源庫批量運行 Export 腳本,產生 IXF 文件。
  8. 執行導入:連接目標庫批量運行 Load 腳本,完成數據裝載。

核心代碼範例(Java/JDBC)

附錄 1:自動生成 Export 腳本

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);
}

附錄 2:自動生成 Load 腳本

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);
}

附錄 3/4:生成腳本實例片段

# 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

關鍵優化提示

  • 優先使用 LOAD 而非 IMPORT,性能提升 3–5 倍;
  • 大表可並行拆分多個 IXF 文件,配合多會話並發 Load;
  • 遷移前執行 runstats 並備份統計信息,遷移後立即更新統計;
  • 關閉目標表索引、外鍵、觸發器,載入完成後重建,極大縮短窗口。

總結

這套「DDL 先行、IXF 中轉、Load 裝載」流程結構清晰、腳本可複用、易於自動化,適合中大型 DB2 遷移項目。實戰中配合表空間規劃、並行度調優,可穩定在百萬行/分鐘級別吞吐。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved