Database Performance Tradeoffs
Ý chính
Database performance không có một đòn tối ưu duy nhất. Mỗi cải tiến đổi chi phí sang chỗ khác: index tăng read speed nhưng làm write chậm; denormalization giảm join nhưng tăng sync complexity; cache giảm load nhưng tạo stale read; sharding scale write nhưng tăng vận hành.
Vòng chẩn đoán
slow path
-> đọc [[Query Execution Plan]]
-> xác định scan/index/join/sort/lock bottleneck
-> sửa query hoặc index
-> kiểm tra schema/partition/workload isolation
-> đo lại latency, throughput, CPU, memory, I/OBảng trade-off
| Kỹ thuật | Giúp | Giá phải trả |
|---|---|---|
| Database Indexing | Read/filter/join nhanh | Write chậm, storage, maintenance |
| Database Partitioning | Scan ít dữ liệu hơn | Key sai làm query xuyên partition chậm |
| Denormalization | Read path nhanh | Duplicate data, stale/inconsistency |
| Materialized View | Query phức tạp thành lookup | Refresh lag, pipeline vận hành |
| Read Replica | Scale read | Replication lag |
| Database Sharding | Scale write/storage ngang | Cross-shard query, rebalancing, ops complexity |
| MVCC | Reader/writer ít block nhau | Version cleanup, conflict semantics |
Chọn database
- SQL Database: ưu tiên schema, constraints, join và transaction correctness.
- NoSQL Database: ưu tiên scale, schema flexibility hoặc access pattern chuyên biệt.
- NewSQL: cần SQL/ACID nhưng phải scale phân tán, chấp nhận coordination latency.
- Document Store: đọc theo aggregate/document, tránh join read-time bằng materialized read model.