ข้ามไปยังเนื้อหา

Lesson 24 — Loading Data with SQLLoader (ภาษาไทย)

🧠 ภาพจำก่อนอ่าน: 🗄️ Data → ⚙️ Process → 🩺 Health
🎨 Visual Guide ของวิชานี้ — ภาพช่วยจำ ไม่แทนคำสั่งและเนื้อหาจริง

Oracle Hub · Data Movement · Lab 11

Curriculum: Oracle University 19c Administration Workshop, Lesson 24. Original Thai teaching note; full guide fidelity PENDING. ตัวอย่าง control file ใช้เรียน syntax ไม่ใช่ production deployment. No live load executed.

อ่าน control file, อธิบาย conventional/direct/external-table load, จัดการ errors/rejects, ตรวจผลและ rollback migration, ป้องกัน encoding corruption

  • Conventional path: SQL*Loader แปลง input records ไปเป็น bind arrays และ insert ผ่าน database processing; เหมาะกับ workload ทั่วไป
  • Direct path: สร้าง data blocks ด้วย path ที่ bypass บางส่วนของ conventional processing; เร็วในบางกรณีแต่มี restrictions, locking/index/constraint/redo implications
  • External-table load: สร้าง external table metadata และใช้ SQL insert จาก external data; ช่วย transformation/parallelism เมื่อเหมาะสม

เลือก mode จาก ความถูกต้องของข้อมูล ก่อนความเร็ว. Direct path ไม่ได้ใช้ได้กับทุก constraint/trigger/data type scenario. https://docs.oracle.com/en/database/oracle/oracle-database/26/sutil/sql-loader-conventional-path-direct-path-external-table-loads.html

-- Sample SQL*Loader control-file structure; lab-only placeholders:
LOAD DATA
INFILE 'sample.csv'
BADFILE 'sample.bad'
DISCARDFILE 'sample.dsc'
APPEND
INTO TABLE LAB_STAGE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
id INTEGER EXTERNAL,
customer_name CHAR(100),
amount DECIMAL EXTERNAL
)

การอ่าน file จริงต้องออกแบบ handling ของ embedded comma, quotes, newlines, character sets, decimal/date masks, nulls, duplicate keys และ trailing spaces; example นี้ไม่ใช่ CSV parser ที่ robust สำหรับทุก input. Mode APPEND เพิ่มข้อมูลถ้ารันซ้ำ — เสี่ยง duplicate; ต้องวาง idempotency.

Output ความหมาย DBA ต้องตรวจ
Loader log counts, field mappings, operation summary logical/physical records, errors, elapsed time
Bad file records ที่ถูกปฏิเสธระหว่าง parsing/DB insertion invalid datatype, constraint, charset
Discard file records ที่ไม่ผ่าน selection WHEN clause ธุรกิจต้องการ discard จริงหรือไม่
Database target data rows ที่ insert แล้ว row counts, sums, keys, referential consistency

Import success ไม่เท่ากับ row-level correctness: เช่นชื่อภาษาไทยอาจกลายเป็น mojibake โดยไม่มี ORA error.

SHOW CON_NAME
SELECT owner,table_name,num_rows,last_analyzed
FROM dba_tables
WHERE owner='LAB_OWNER' ORDER BY table_name;
SELECT owner,table_name,constraint_name,constraint_type,status
FROM dba_constraints
WHERE owner='LAB_OWNER' ORDER BY table_name;

NUM_ROWS เป็น optimizer statistics อาจ stale/estimated — ไม่ใช่ COUNT(*) ที่เที่ยงตรงทุกเวลา. ใน sandbox สามารถทำ SELECT COUNT(*) บน authorized table เมื่อตรวจ impact ของ full scans แล้ว.

  1. ขนาดและ hash ของ source file ก่อน upload
  2. encoding/BOM, decimal separator, date time zone และ CSV quoting rules
  3. expected input records / rejected / discarded / inserted ตามธุรกิจ
  4. uniqueness, constraints, duplicates, null percentages, key ranges
  5. reconciled totals เช่น SUM(amount) ก่อนและหลัง load
  6. sample Thai characters แบบที่ source ควบคุมได้
  7. retry policy และ undo/rollback (staging table, snapshot/backup)

เจอ ORA-01722 Invalid Number เมื่อโหลด CSV: ตรวจ control field mapping, comma/decimal, whitespace, locale, bad-file record. อย่าปิด constraint หรือใช้ implicit conversion แก้ปัญหาโดยไม่พิสูจน์ payload.

  • Conventional vs direct path มี trade-offs อะไร?
  • Bad records ต่างจาก discarded records อย่างไร?
  • เหตุใดใช้ APPEND แล้ว rerun อาจผิด?
  • จะวัดความถูกต้องของข้อมูลไทยหลังการโหลดอย่างไร?
Mode จุดเด่น ต้องตรวจก่อนเลือก
Conventional SQL INSERT and bind arrays; เหมาะ concurrent OLTP / small batch Redo/Constraints/Concurrency Overhead
Direct Write Oracle data blocks efficiently for bulk loads Active Transactions, Locking, VPD/INSERT Policy, Clustered Table, Index/Constraint/Trigger Restrictions
External Table Query/transform source files via SQL then INSERT File permissions, access driver, parsing/reject controls

SQL*Loader ไม่สร้าง Table ให้อัตโนมัติ: Target Table และ INSERT Privilege ต้องมีอยู่ก่อน. การใช้ REPLACE หรือ TRUNCATE ต้องตรวจ DELETE Privilege ตามข้อกำหนด SQL*Loader. Direct Path บางกรณีต้องมี Exclusive Access และไม่เหมาะกับ workload ที่มี Concurrent Writes

APPEND ไม่ Idempotent: รัน File เดิมซ้ำอาจเกิด Row Duplication. ใช้ Staging/Business Key, Hash/Row Counts, Quoting, Timestamp/Decimal Masks และ Thai Text Round-trip เพื่อให้พิสูจน์ได้ว่าข้อมูลไม่เสียหลัง Migration

Official: Oracle 19c Conventional vs Direct Path · SQL*Loader Concepts.

Status: DOC_REVIEWED / LOADER NOT RUN.

✅ บทที่ 24/31 ในเส้นทางหลัก

เมื่ออ่านและฝึกทบทวนจบแล้ว ทำเครื่องหมายเพื่อบันทึกใน Browser นี้