数据分区(分片)将数据分散到多个服务器/数据库,每个服务器保存一个子集——支持数据的水平扩展和超越单个服务器的负载扩展。选择如何分区(分区键和策略)至关重要。
什么是分区/分片
text
PARTITIONING / SHARDING → divide data into pieces (partitions/shards) across multiple
servers, each holding a SUBSET:
→ no single server holds (or is overwhelmed by) all the data
→ scales STORAGE and LOAD horizontally (each shard handles its portion)
→ enables handling data/throughput beyond one machine's capacity
分区策略
text
HASH-based → hash the partition key → assign to a shard:
✓ EVEN distribution (avoids hotspots) ✗ range queries hard; resharding is tricky
RANGE-based → partition by value ranges (e.g. A-M, N-Z; date ranges):
✓ efficient range queries ✗ risk of HOTSPOTS (uneven load if data/access is skewed)
DIRECTORY/lookup → a lookup table maps keys to shards (flexible, but the lookup is overhead)
GEOGRAPHIC → partition by region (data locality)
