# 数据分区

**数据分区**（data partitioning）是指将逻辑上的一整份大型数据集划分为更小的物理片段（称为分区）的做法，使得每个片段可以被单独存储、查询和管理。在数据库中，分区将一张大表拆分为更小的物理单元；在分布式系统中，分区将数据项映射到集群的各个节点上。采用分区的主要目的是实现数据级并行，查询性能、可管理性和存储分层等方面的收益都源自同一种划分<sup>[1](https://www.postgresql.org/docs/17/ddl-partitioning.html)</sup><sup> • </sup><sup>[2](https://ideas.repec.org/h/spr/sprchp/978-3-642-36524-9_9.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 将逻辑上的一张大型表拆分为更小的物理片段<sup>[1](https://www.postgresql.org/docs/17/ddl-partitioning.html)</sup> |
| 主要策略 | 水平分区（分片）、垂直分区和功能分区，可组合使用<sup>[3](https://learn.microsoft.com/en-in/azure/architecture/best-practices/data-partitioning)</sup> |
| 关键方案 | 范围分区（保持键序，适合范围扫描）与哈希分区（均匀分布，适合点查询）<sup>[4](https://martinfowler.com/articles/patterns-of-distributed-systems/key-range-partitions.html)</sup><sup> • </sup><sup>[5](https://hld.handbook.academy/trade-offs/partitioning-schemes/)</sup> |
| 适用时机 | 经验法则：表的大小应超过数据库服务器的物理内存<sup>[1](https://www.postgresql.org/docs/17/ddl-partitioning.html)</sup> |
| 查询优化 | 分区剪枝使查询规划器只需扫描可能匹配查询的分区；MySQL 报告其执行速度可比未分区表快一个数量级<sup>[6](https://dev.mysql.com/doc/refman/8.4/en/partitioning-pruning.html)</sup> |
| 规模限制 | SQL Server 每张表或索引最多支持 15,000 个分区；PostgreSQL 能很好地处理多达数千个分区的层次结构<sup>[7](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?trk=article-ssr-frontend-pulse_little-text-block&view=sql-server-ver16)</sup><sup> • </sup><sup>[8](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup> |
| 分区与分片 | 分区在同一数据库服务器上运行；分片运行在不同的服务器和机器上<sup>[9](https://www.itpro.com/technology/big-data/what-is-database-sharding-how-benefit-enterprise-it)</sup> |

## 为什么要分区

[PostgreSQL](https://www.edgepedia.org/zh/postgresql) 的文档列出了四项好处。当访问最频繁的行集中在一个或少数几个分区中时，查询性能可以显著提升，因为分区替代了索引树的上层结构，使索引的热点部分更有可能驻留内存。当查询或更新涉及单个分区很大比例的数据时，对该分区进行顺序扫描可能优于需要在整个表中分散随机读取的索引。批量加载和删除可以通过添加或移除分区来完成，而删除一个分区或用 `ALTER TABLE DETACH PARTITION` 将其分离，远比批量 `DELETE` 快，同时避免了后者的 `VACUUM` 开销。最后，不常使用的数据可以迁移到更廉价、更慢的存储介质上<sup>[1](https://www.postgresql.org/docs/17/ddl-partitioning.html)</sup>。

这些好处通常只有在不分区时表会变得非常大的情况下才值得追求。确切的阈值取决于应用，但一个常见的经验法则是表应超过数据库服务器的物理内存<sup>[1](https://www.postgresql.org/docs/17/ddl-partitioning.html)</sup>。Oracle 对数据仓库给出了同样的收益描述——在数据仓库中，表包含数百万行和数 GB 的数据，即使面对 TB 级别的表，分析也必须在合理时间内返回结果<sup>[10](https://docs.oracle.com/en/database/oracle/oracle-database/18/vldbg/partition-warehouse.html)</sup>。

## 分区策略

Azure 架构中心区分了三种典型策略，它们可以组合使用。**水平分区**（常称为分片）为每个分区赋予相同的模式但不同的行子集，例如特定客户集合的所有订单。**垂直分区**按字段划分：将频繁访问的列与较少访问的列分开，当某些列远大于其他列或具有不同的访问模式时，这能提升 I/O 效率。**功能分区**按每个限界上下文使用数据的方式来聚合数据，例如将发票数据存储在一个分区，将产品库存数据存储在另一个分区<sup>[3](https://learn.microsoft.com/en-in/azure/architecture/best-practices/data-partitioning)</sup>。

在水平分区中，两种主导方案在有序性和均匀性之间权衡。**范围分区**按排序顺序将键分配给逻辑分区，然后映射到集群节点。要查询一个范围，客户端找到包含该范围内键的分区，只查询这些分区，而非所有分区<sup>[4](https://martinfowler.com/articles/patterns-of-distributed-systems/key-range-partitions.html)</sup>。HBase、Google 的 Bigtable 和 TiKV 都使用范围分区<sup>[11](https://rahulsuryawanshi.com/distributed-systems/scalability-performance/partitioning-sharding/)</sup>。其弱点是数据倾斜：范围不一定均匀分布，因此某些范围可能包含远多于其他范围的数据；当键是时间戳时，访问本身也可能偏向最近的值<sup>[12](https://haoailab.com/dsc204a-w24/assets/scribe_notes/Feb_16_scribe_note.pdf)</sup>。

**哈希分区**对键应用哈希函数，用结果来选择分片，形式为 `shard = hash(key) mod N`。它均匀分散负载，适合以点查询为主的工作负载，但破坏了键的顺序，因此诸如“最近一小时的所有事件”这样的范围扫描必须分发到每个分区<sup>[11](https://rahulsuryawanshi.com/distributed-systems/scalability-performance/partitioning-sharding/)</sup><sup> • </sup><sup>[5](https://hld.handbook.academy/trade-offs/partitioning-schemes/)</sup>。Fowler 的模式示例具体说明了代价：将作者名的哈希值对九取模、据此映射到九个分区时，请求从“a”到“f”的名字无法确定哪些分区包含它们，因此必须查询所有分区<sup>[4](https://martinfowler.com/articles/patterns-of-distributed-systems/key-range-partitions.html)</sup>。[一致性哈希](https://www.edgepedia.org/zh/consistent-hashing)缓解了普通哈希的再平衡成本：添加节点时，它接管环上紧邻其前的键，因此大约 1/N 的键会迁移，其中 N 是节点数<sup>[11](https://rahulsuryawanshi.com/distributed-systems/scalability-performance/partitioning-sharding/)</sup>。

## 分区剪枝

使分区在 SQL 引擎中有效的性能机制是分区剪枝。查询规划器检查每个分区的定义，证明该分区无需扫描，因为它不可能包含匹配查询 `WHERE` 子句的行<sup>[1](https://www.postgresql.org/docs/17/ddl-partitioning.html)</sup>。MySQL 的优化器同样对每个值评估分区表达式，构建匹配分区列表，只扫描这些分区；当剪枝生效时，执行速度可比对具有相同数据的未分区表执行相同查询快一个数量级<sup>[6](https://dev.mysql.com/doc/refman/8.4/en/partitioning-pruning.html)</sup>。Oracle 称剪枝是数据仓库的关键性能特性，并可以在复合分区对象的两个层级上进行剪枝<sup>[13](https://docs.oracle.com/cd/E24693_01/server.11203/e16541/part_avail.htm)</sup>。

剪枝仅在查询对分区列进行过滤时才有效。PostgreSQL 的最佳实践指南建议按最常出现在 `WHERE` 子句中的列进行分区，对列表分区使用简单的等值条件，对范围分区使用简单的范围测试，因为分区键只允许使用可 B-tree 索引的列<sup>[8](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup>。

## 设计权衡

选择分区数量是一种平衡。分区太少会导致索引过大、数据局部性差、缓存命中率低；分区太多则延长查询规划时间，并增加规划和执行期间的内存消耗，因为每个分区的元数据必须加载到每个访问它的会话的本地内存中。PostgreSQL 的规划器在典型查询能剪枝到只剩少数几个分区时，能相当好地处理多达数千个分区的层次结构；传统的基于继承的分区在约一百个子表以内表现良好<sup>[8](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup>。SQL Server 的引擎每张表或索引最多支持 15,000 个分区<sup>[7](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?trk=article-ssr-frontend-pulse_little-text-block&view=sql-server-ver16)</sup>。

决策难以逆转：对大量数据重新分区可能极其缓慢，因此 PostgreSQL 建议模拟预期的工作负载，并警告不要假设分区越多越好，也不要假设越少越好<sup>[8](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup>。由于整个分区可以快速分离，设计分区方案时使一次性删除的所有数据位于单个分区中，可以简化数据保留策略；Oracle 的指南同样建议对只追加的时间序列表使用 `INTERVAL` 分区以消除分区维护开销，对每月接收约一千万行的表使用月度分区<sup>[8](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup><sup> • </sup><sup>[14](https://github.com/oracle/skills/blob/main/db/design/partitioning-strategy.md)</sup>。

## 分区、分片与多租户

分区和分片这两个术语有重叠但范围不同。一种常见的表述是：分区在同一数据库服务器上将数据划分为可管理的片段，而分片运行在不同的服务器和机器上；企业倾向于对读密集型数据库使用分区，对写密集型数据库使用分片，两者可以协同工作<sup>[9](https://www.itpro.com/technology/big-data/what-is-database-sharding-how-benefit-enterprise-it)</sup>。BigQuery 在分区和表分片之间做了相关对比——表分片是将数据存储在多个以日期前缀命名的表中，如 `[PREFIX]_YYYYMMDD`；它建议使用分区，因为分片表迫使服务为每张表维护模式和元数据副本，并对每个被查询的表验证权限，增加了查询开销<sup>[15](https://docs.cloud.google.com/bigquery/docs/partitioned-tables)</sup>。

在多租户 SaaS 系统中，数据分区描述租户数据的存储方式，可以是每个租户拥有独立存储构造的独立模式，也可以是数据混合存储并以租户标识符为键的池化模式。分区不等同于租户隔离：隔离数据使一个租户无法访问另一个租户的资源，必须单独实施。在 [Amazon DynamoDB](https://www.edgepedia.org/zh/amazon-dynamodb) 中，池化通过将租户标识符存储在每张表的分区键中来实现<sup>[16](https://docs.aws.amazon.com/whitepapers/latest/saas-architecture-fundamentals/data-partitioning.html)</sup>。

## References

1. [PostgreSQL Documentation: 5.12. Table Partitioning](https://www.postgresql.org/docs/17/ddl-partitioning.html)
2. [Partitioning, Springer chapter (Ideas RePEc)](https://ideas.repec.org/h/spr/sprchp/978-3-642-36524-9_9.html)
3. [Data partitioning guidance, Azure Architecture Center](https://learn.microsoft.com/en-in/azure/architecture/best-practices/data-partitioning)
4. [Key-Range Partitions, Patterns of Distributed Systems, MartinFowler.com](https://martinfowler.com/articles/patterns-of-distributed-systems/key-range-partitions.html)
5. [Partitioning Schemes: Range, Hash, Consistent Hash, Directory, The HLD Handbook](https://hld.handbook.academy/trade-offs/partitioning-schemes/)
6. [MySQL 8.4 Reference Manual: Partition Pruning](https://dev.mysql.com/doc/refman/8.4/en/partitioning-pruning.html)
7. [Partitioned Tables and Indexes, SQL Server, Microsoft Learn](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?trk=article-ssr-frontend-pulse_little-text-block&view=sql-server-ver16)
8. [PostgreSQL Documentation (current): Table Partitioning, Best Practices](https://www.postgresql.org/docs/current/ddl-partitioning.html)
9. [What is database sharding and how does it benefit enterprise IT?, ITPro](https://www.itpro.com/technology/big-data/what-is-database-sharding-how-benefit-enterprise-it)
10. [Using Partitioning in a Data Warehouse Environment, Oracle Database 18](https://docs.oracle.com/en/database/oracle/oracle-database/18/vldbg/partition-warehouse.html)
11. [Partitioning and Sharding in Distributed Systems, Rahul Suryawanshi](https://rahulsuryawanshi.com/distributed-systems/scalability-performance/partitioning-sharding/)
12. [DSC-204A Scalable Data Systems lecture notes, partitioning by key range and hash](https://haoailab.com/dsc204a-w24/assets/scribe_notes/Feb_16_scribe_note.pdf)
13. [Partitioning for Availability, Manageability, and Performance, Oracle](https://docs.oracle.com/cd/E24693_01/server.11203/e16541/part_avail.htm)
14. [Partitioning Strategy, oracle/skills on GitHub](https://github.com/oracle/skills/blob/main/db/design/partitioning-strategy.md)
15. [Introduction to partitioned tables, BigQuery, Google Cloud](https://docs.cloud.google.com/bigquery/docs/partitioned-tables)
16. [Data partitioning, SaaS Architecture Fundamentals, AWS](https://docs.aws.amazon.com/whitepapers/latest/saas-architecture-fundamentals/data-partitioning.html)

---
*Initially written Oct 26, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database theory and data modeling › Indexing and physical data organization

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/data-partitioning

English original: https://www.edgepedia.org/data-partitioning
