Fundamentals of Database Engineering

สรุปจาก Udemy course “Fundamentals of Database Engineering” โดย Hussein Nasser (~6 ชั่วโมง, ระดับ intermediate-advanced)

Overview

Course นี้สอน fundamentals และ first principles ของ database engineering — ไม่ใช่สอนใช้ Postgres/MySQL แต่สอนว่าทำไมเทคโนโลยีเหล่านี้ถึงมีอยู่ เพื่อให้เลือก database ได้ถูก, design schema ได้ดี, และเข้าใจว่าทำไม query ช้า/เร็ว

เนื้อหาทั้ง 18 Sections

Part 1 — Foundation Core (Section 1-7)

Part 2 — Production Concerns (Section 8-12)

  • Section 8: Concurrency Control — Shared/Exclusive locks, Deadlock, 2PL, Double Booking
  • Section 9: Database Replication — Master/Backup, Multi-Master, Sync/Async, Eventual Consistency
  • Section 10: Database System Design — ระบบจองที่นั่ง (booking system) จริง
  • Section 11: Database Engines — InnoDB, MyISAM, RocksDB, LevelDB, B-Tree vs LSM-Tree
  • Section 12: Database Cursors — Server/Client-side, Keyset Pagination

Part 3 — Advanced & Modern Topics (Section 13-18)

Key Takeaways 15 ข้อ (จาก course)

  1. Database อ่านทีละ page ไม่ใช่ทีละ row (Postgres 8KB, MySQL 16KB)
  2. ACID = หัวใจของ relational DB
  3. 4 Read Phenomena: Dirty Read, Non-repeatable Read, Phantom Read, Lost Update
  4. B+Tree เป็นมาตรฐาน — leaf nodes linked ทำให้ range query เร็ว
  5. EXPLAIN ANALYZE = เครื่องมือเทพสำหรับ tune query
  6. MySQL ใช้ clustered index, Postgres ใช้ heap-organized
  7. Partitioning = แบ่งใน DB เดียว / Sharding = แบ่งคนละ server
  8. Pessimistic Locking vs Optimistic — เลือกตาม contention
  9. Sync vs Async Replication — trade-off ระหว่างความปลอดภัยกับความเร็ว
  10. B-Tree DBs เก่ง read vs LSM-Tree DBs เก่ง write
  11. Row store (OLTP) vs Column store (OLAP) — ใช้ผิดช้าได้ 100x
  12. Connection Pooling สำคัญ — TCP+TLS handshake = 10-100ms
  13. NULL = unknown — ใช้ IS NULL ไม่ใช่ = NULL
  14. อย่า SELECT * — ระบุ column ที่ใช้เสมอ
  15. Default ปี 2024+ → Postgres ก่อน แล้วค่อยเพิ่ม specialized DB ตามต้องการ

Pitfalls สำคัญ

Pitfallทางแก้
WHERE LOWER(col) = 'x'Functional index หรือ store lowercase
LIKE '%text%'Trigram index หรือ full-text search
Partition ผิด columnวิเคราะห์ query patterns ก่อน
Shard ก่อนวัดVertical scale + replicas ก่อน
OFFSET 1000000Keyset pagination
String concatenation สร้าง SQLParameterized queries เสมอ
  • PostgreSQL — DB หลักที่ course แนะนำเป็น default
  • MySQL — เปรียบเทียบกับ Postgres ตลอด course
  • MongoDB — ตัวอย่าง NoSQL ที่วิเคราะห์ลึก
  • Redis — in-memory store ที่วิเคราะห์ architecture
  • Memcached — เปรียบเทียบกับ Redis