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

DB2 資料移動利器:LOAD 命令深度解析與實戰範例(含效能優化)

     編輯:DB2 教學

LOAD vs IMPORT:核心差異

特性LOADIMPORT
資料來源檔案、游標、管道、裝置僅檔案
寫入方式直接格式化頁寫入磁碟,不經緩衝池逐列 INSERT,經緩衝池
約束檢查延後至 Delete Phase 統一檢查逐列即時檢查
日誌記錄最小化(可設 NONRECOVERABLE)完整記錄每列
效能大量資料載入快數倍適合小量資料
可恢復性需搭配 COPY YES/TSM 或備份完全可恢復

LOAD 四大階段

  1. Load Phase:資料格式化為頁寫入表,收集索引鍵並排序;可設定一致點(Consistency Point)支援中斷續傳。
  2. Build Phase:依收集的鍵值建立索引;失敗可從此階段重啟。
  3. Delete Phase:刪除違反唯一/主鍵約束的列,寫入例外表(若指定);產生訊息記錄。
  4. Index Copy Phase:若指定系統暫存表空間且選 READ ACCESS,將索引從暫存空間複製回目標表空間。

四階段全部完成才算作業結束;任一階段失敗均進入「Load Pending」狀態,需 RESTART 或 TERMINATE 解除。

實戰範例專區

範例 12:從游標載入(IMPORT 不支援)

declare mycur cursor for select * from org;
create table org2 like org;
load from mycur of cursor insert into org2;

範例 13:例外表處理唯一鍵違規

-- 建立目標表與例外表
create table staff1 like staff;
insert into staff1 select * from staff where id <= 160;
create table staffexp like staff1;
alter table staffexp add column time timestamp;  -- 至少需 timestamp 欄位

-- 建立唯一索引製造衝突
create unique index idxstaff on staff1(id);

-- 匯出資料並載入(含違規資料)
export to d:staff.txt of del select * from staff;
load from d:staff.txt of del insert into staff1 for exception staffexp;

違規列寫入 staffexp,原表僅載入合規資料。若未指定例外表則直接捨棄。

範例 14:DUMPFILE 處理格式錯誤列

-- 故意破壞第 32 列格式
load from d:staff.txt of del modified by dumpfile=d:dump insert into staff1 for exception staffexp;

格式錯誤列導向 dump.000;約束違規列導向例外表。兩者互補,確保資料不遺失。

範例 15:限制載入筆數

load from d:staff.txt of del rowcount 3 insert into staff1;

範例 16:WARNINGCOUNT 控制容錯門檻

load from d:staff.txt of del warningcount 1 insert into staff1;
-- 超過 1 個警告即中止,表進入 Load Pending
-- 解除:load from d:staff.txt of del terminate into staff1;
-- 修正後續傳:load from d:staff.txt of del restart into staff1;

範例 17:NOROWWARNINGS 抑制列級警告提升效能

load from d:staff.txt of del modified by norowwarnings insert into staff1;

範例 18:載入同步產生統計資訊(僅相容 REPLACE)

-- 完整統計
load from d:staff.txt of del replace into staff1 statistics yes with distribution and detailed indexes all;
-- 簡略統計
load from d:staff.txt of del replace into staff1 statistics yes and indexes all;

範例 19:解除 Check Pending 狀態

create table staff1 like staff;
alter table staff1 add constraint chk check(dept < 100);
-- 載入違規資料導致 Check Pending
load from d:staff.txt of del insert into staff1;
-- 解除
set integrity for staff1 check immediate unchecked;

範例 20:效能調優參數總覽

參數/修飾符效用適用場景
NONRECOVERABLE標記不可恢復,前滾忽略,表空間不進入 Backup Pending可重建的暫存資料、測試環境
COPY NO(預設)歸檔日誌啟用時,載入後表空間進入 Backup Pending,需備份才可寫入生產環境標準模式
COPY YES載入變更同步寫入磁帶/目錄/TSM,表空間不進入 Backup Pending高可用性需求
FASTPARSE減少資料檢查,假設輸入資料乾淨DEL/ASC 格式、確定資料正確時
ANYORDER不保留輸入順序,SMP 系統上配合 CPU_PARALLELISM > 1 提升平行度大量載入、SMP 環境
DATA BUFFER指定 4K 頁面數作為內部緩衝區大量載入,適當加大有助效能
CPU_PARALLELISM指定解析/轉換/格式化的進程/執行緒數僅 SMP 系統
DISK_PARALLELISM指定寫入磁碟的平行度多磁碟陣列環境

操作建議

  • 大量載入前預估磁碟空間、排序暫存空間、日誌空間。
  • 生產環境務必搭配 COPY YES 或事後立即備份,確保可恢復。
  • 載入前先 runstats 或載入時開啟 STATISTICS,避免優化器使用過時統計。
  • 遇到 Load Pending 先 TERMINATE 釋放鎖定,再決定 RESTART 或重新載入。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved