NoSQL
ฐานข้อมูลที่ใช้ API/data model อื่นนอกจาก SQL — แบ่งเป็น 4 ประเภทหลัก แต่ storage engine ข้างในอาจเหมือน SQL DB ก็ได้
มุมมองของ Hussein Nasser
Database แบ่งเป็น 2 ส่วน:
- Frontend/API — SQL, document API, command-style
- Storage Engine/Backend — B-Tree, LSM-Tree, pages, bytes
Storage engine ไม่สนใจ data format — เห็นเป็น bytes ใน pages → “SQL vs NoSQL” จริง ๆ เป็นคำถามเกี่ยวกับ API + data model ที่ frontend ใช้
4 ประเภทของ NoSQL
(1) Key-Value Stores
- ตัวอย่าง: Redis, DynamoDB, Memcached, etcd
- โครงสร้าง:
key → value(value อะไรก็ได้) - Use case: cache, session store, leaderboard, counter
(2) Document Stores
- ตัวอย่าง: MongoDB, CouchDB, Couchbase, Elasticsearch
- โครงสร้าง: collection ของ JSON/BSON documents
- Schema-less หรือ flexible schema
- Use case: content management, product catalog, user profile
(3) Wide-Column Stores
- ตัวอย่าง: Cassandra, HBase, ScyllaDB, Bigtable
- โครงสร้าง: row + dynamic columns (แต่ละ row ต่างกันได้)
- ใช้ LSM-Tree → write-heavy excellent
- Use case: time-series, IoT, logs
(4) Graph Databases
- ตัวอย่าง: Neo4j, ArangoDB, Amazon Neptune
- โครงสร้าง: nodes + edges with properties
- Use case: social network, fraud detection, recommendation
ทำไมคนหนีไป NoSQL?
- Flexible schema — ไม่ต้อง migration ทุกครั้ง
- Horizontal scaling built-in (sharding)
- High write throughput — โดยเฉพาะ LSM-Tree DBs
- Eventual consistency รับได้ — สำหรับ social media, analytics
ทำไมหลายคนกลับมา SQL?
- JOIN ซับซ้อน — NoSQL ต้อง denormalize
- ACID transactions ข้าม collection
- PostgreSQL + JSONB — ได้ทั้ง SQL power + flexible schema
- เครื่องมือ + community ของ SQL ครบกว่า
คำแนะนำของ Hussein Nasser (2024+)
| Workload | แนะนำ |
|---|---|
| Default | PostgreSQL ก่อน |
| Time-series | TimescaleDB (Postgres extension) |
| Flexible schema | MongoDB หรือ Postgres + JSONB |
| Global distribution + high write | CockroachDB / Cassandra |
| Analytics | ClickHouse + replicate จาก Postgres |
Page Size ของ NoSQL Engines
| Database | Page Size | Engine |
|---|---|---|
| PostgreSQL | 8 KB | Built-in heap |
| MySQL InnoDB | 16 KB | InnoDB |
| MongoDB | 32 KB | WiredTiger |
Key Points
- SQL vs NoSQL จริง ๆ คือเรื่อง API/data model ไม่ใช่ storage engine
- 4 ประเภท: Key-Value, Document, Wide-Column, Graph
- เส้นแบ่ง SQL/NoSQL เลือนลาง — MongoDB เพิ่ม transactions, Postgres มี JSONB
- Default → Postgres แล้วค่อยเพิ่ม specialized DB ตามต้องการ
- ย้ายไป NoSQL แล้ว develop แบบ relational = ปวดหัว
Related
- MongoDB — document store ที่วิเคราะห์ลึกใน course
- Redis — key-value store
- Memcached — key-value pure cache
- Database Engines — B-Tree vs LSM-Tree ที่ NoSQL ใช้
- CAP Theorem — NoSQL มักเป็น AP (availability + partition tolerance)
- PostgreSQL — ทางเลือก default ก่อน NoSQL