Database Engines
Database Engine = library ที่จัดการ on-disk storage และ CRUD operations — DBMS (เช่น MySQL) ใช้ engine + เพิ่ม features เหนือ engine (server, replication, isolation, stored procedures)
Engine ที่ควรรู้
InnoDB
- B+Tree — secondary indexes ชี้ไป PK, PK ชี้ row (clustered index)
- ACID compliant + transactions + foreign keys
- Row-level locking → concurrent writes ดี
- Default ของ MySQL & MariaDB ปัจจุบัน
- Owned by Oracle
MyISAM
- B-Tree indexes ชี้ row ตรง ๆ
- ไม่มี transactions — ไม่ ACID!
- Table-level locking → write throughput ต่ำ
- Crash → tables corrupted → ต้อง manual repair
- เคยเป็น default ของ MySQL — ห้ามใช้ใน production ใหม่
RocksDB
- Facebook fork จาก LevelDB (2012)
- ใช้ LSM Tree — write-optimized, multi-threaded compaction
- Transactional + features เพิ่มจาก LevelDB เยอะ
- MyRocks = RocksDB สำหรับ MySQL/MariaDB/Percona
- MongoRocks = สำหรับ MongoDB
- Insert เร็ว 10x + disk ใช้น้อย 50% เทียบกับ InnoDB ในบาง workload
LevelDB
- เขียนโดย Jeff Dean & Sanjay Ghemawat (Google) ปี 2011
- ใช้ LSM Tree — write + SSD เหมาะมาก
- ไม่มี transactions
- มีหลาย levels: Memtable → Level 0 → Level 1-6
- ใช้ใน Bitcoin Core, AutoCAD, Minecraft
Aria (MariaDB)
- คล้าย MyISAM แต่ crash-safe
- ไม่ owned by Oracle
SQLite
- Lightweight embedded — ทั้ง engine + DBMS รวมกัน
- ไฟล์เดียว ไม่มี server process
- ใช้ในมือถือทุกเครื่อง, browser embedded
B-Tree DBs vs LSM-Tree DBs
| B-Tree (read-optimized) | LSM-Tree (write-optimized) |
|---|---|
| PostgreSQL, MySQL, Oracle, SQL Server | Cassandra, HBase, Bigtable |
| MongoDB, Couchbase | Elasticsearch, InfluxDB |
| LevelDB, RocksDB, YugaByteDB |
กฎเลือก
- อ่านเยอะ + random read → B-Tree
- เขียนเยอะ + sequential write (logs, time-series, IoT) → LSM-Tree
เปลี่ยน Engine = เปลี่ยน Performance
-- MySQL: เปลี่ยน engine ของ table ได้
CREATE TABLE my_logs (...) ENGINE=RocksDB;
ALTER TABLE existing_table ENGINE=InnoDB;PostgreSQL เปลี่ยน engine ไม่ได้ — engine ฝังในตัว ถ้าต้องการ LSM-Tree ใน Postgres ต้องใช้ extension (TimescaleDB, Citus)
Key Points
- Database Engine = library จัดการ storage — DBMS เพิ่ม features เหนือ engine
- InnoDB = default MySQL, ACID, row-level lock, B+Tree clustered
- MyISAM = legacy, ไม่มี transaction, table-level lock — ห้ามใช้ใหม่
- RocksDB (LSM-Tree) = write-heavy champion, disk efficient
- B-Tree DBs เก่ง read, LSM-Tree DBs เก่ง write
- MySQL เปลี่ยน engine ได้ → Postgres ไม่ได้
Related
- MySQL — InnoDB, MyISAM, MyRocks
- PostgreSQL — engine ฝังตัว, heap-organized
- MongoDB — WiredTiger (B+Tree)
- Database Indexing — B-Tree vs B+Tree structure
- Row Store vs Column Store — อีกมิติของ engine classification