# 分区（数据库）

在数据库系统中，分区是一张逻辑上单一的大表或索引被划分成的若干较小物理片段中的一个，使得各组行得以分开存储和管理，而对查询和更新而言整体仍是一个逻辑实体。分区是包括 SQL Server、[PostgreSQL](https://www.edgepedia.org/zh/postgresql) 和 Oracle 在内的主流关系型系统以及 BigQuery 等云数据仓库所提供的一种数据管理技术。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup><sup> • </sup><sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 表按水平方式分区：各组行被映射到各个分区中，查询或更新时该表被视为单一逻辑实体。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup> |
| 常用方法 | 范围（按值区间）、列表（按显式列出的键值）和哈希（按哈希键值除以模数后的余数）。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup> |
| 何时划算 | PostgreSQL 的经验法则是表应超过数据库服务器的物理内存；收益通常只适用于非常大的表。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup> |
| 主要性能机制 | 分区修剪：优化器排除不可能包含满足查询谓词的行的分区，减少从磁盘读取的数据量。<sup>[3](https://docs.oracle.com/en/database/oracle/oracle-database/18/vldbg/partition-pruning.html)</sup><sup> • </sup><sup>[4](https://www.postgresql.org/docs/15/ddl-partitioning.html)</sup> |
| SQL Server 限制 | 每张表或索引最多 15,000 个分区；在 SQL Server 2012 之前默认限制为 1,000 个。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup> |
| 维护优势 | 删除或分离一个分区几乎瞬间即可移除数百万行，避免逐行操作以及批量 DELETE 带来的 VACUUM 开销。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup> |
| 与分片的区别 | 分区在单个数据库服务器内划分数据；分片将数据分布到多台服务器或机器上。<sup>[5](https://www.itpro.com/technology/big-data/what-is-database-sharding-how-benefit-enterprise-it)</sup> |

## 分区的工作原理

分区表声明一个分区键，即一列或一组列，其值决定每一行的存储位置。在 SQL Server 中，分区函数是一个数据库对象，它定义行如何基于该分区列的值映射到分区，而分区方案则将生成的分区映射到一个或多个文件组。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup> 在 PostgreSQL 中，分区表本身是一张不拥有存储的“虚拟”表；存储属于各个分区，这些分区在其他方面就是普通表，插入的行会被自动路由到与键值匹配的分区。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup>

**分区方法。** PostgreSQL 内置的声明式分区支持三种形式。在范围分区中，表被划分为互不重叠的值范围，每个范围的下界包含、上界排除，因此等于共享边界的值属于较高的分区。在列表分区中，每个分区显式列出其持有的键值。在哈希分区中，每个分区持有这样的行：其分区键的哈希值除以指定模数后得到指定的余数。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup> Oracle 提供更多种类的分区方法，包括间隔分区（为未来数据自动创建等分范围）、自动列表分区（为新键值自动创建分区）和引用分区（让子表通过主键关系继承父表的分区策略）。<sup>[6](https://www.oracle.com/database/technologies/partitioning.html)</sup>

分区本身还可以再分区，这种设计称为子分区或复合分区。Oracle 在 Oracle8i 中引入了复合（范围/哈希）分区，将范围分区的可管理性优势与哈希分区的均匀数据分布相结合；范围分区本身可追溯到 Oracle8，即 8.0 版。<sup>[7](https://docs.oracle.com/cd/A87860_01/doc/server.817/a76962/ch2.htm)</sup>

## 数据库为何对表分区

收益分为三类：可管理性、查询性能和数据生命周期控制。

**可管理性。** 分区让管理员可以针对数据子集而非整张表进行操作。在 SQL Server 中，数据可以被压缩、索引分区可以被重建、单个分区可以被截断，各个分区还可以从表中切换出去转入归档表；从 OLTP 系统向 OLAP 系统加载数据只需几秒，而不分区则需要几分钟甚至几小时。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup>

**批量移除与滚动窗口。** 对于随时间累积数据的表，最有价值的收益是快速移除旧数据。在 PostgreSQL 中，用 `DROP TABLE` 删除一个分区或用 `ALTER TABLE ... DETACH PARTITION` 分离一个分区可以非常迅速地删除数百万条记录，因为无需逐行删除，并且完全避免了批量 `DELETE` 会引起的 `VACUUM` 开销。分离还允许在删除之前对数据进行备份、聚合或生成报表。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup> 不常用的数据同样可以迁移到更便宜、更慢的存储介质上。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup>

**查询性能。** 当大部分被频繁访问的行集中在一个或少数几个分区中时，分区可以提升性能，并且它有效地替代了索引树的上层，使索引的热点部分更有可能装入内存。当查询或更新涉及单个分区的很大比例时，对该分区进行顺序扫描可能优于需要在整张表上分散随机访问读取的索引。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup> SQL Server 还补充说，当分区列与连接列匹配时，优化器可以更快地处理分区表之间的等值连接查询，并且可以将锁升级设置为在分区级别而非表级别发生，从而减少锁争用。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup>

## 分区修剪

分区修剪（在 SQL Server 和 Oracle 中称为分区消除）是优化器跳过不可能包含满足查询条件的行的分区的能力。Oracle 将其描述为数据仓库的一项关键性能特性：优化器在构建分区访问列表时分析 `FROM` 和 `WHERE` 子句以消除不需要的分区，从而大幅减少从磁盘检索的数据量并缩短处理时间。<sup>[3](https://docs.oracle.com/en/database/oracle/oracle-database/18/vldbg/partition-pruning.html)</sup>

修剪取决于谓词形式。Oracle 在范围或列表分区列上使用范围、`LIKE`、等值和 `IN` 列表谓词进行修剪，在哈希分区列上使用等值和 `IN` 列表谓词进行修剪；在复合对象上可以在两个层级同时修剪。<sup>[3](https://docs.oracle.com/en/database/oracle/oracle-database/18/vldbg/partition-pruning.html)</sup> 在 PostgreSQL 中，修剪仅由分区键所隐含的约束驱动，而非由索引的存在驱动，并且它不仅可以在计划时发生，还可以在执行期间发生，从而允许使用仅在运行时才知道的值进行修剪，例如预处理语句的参数或子查询的值。<sup>[4](https://www.postgresql.org/docs/15/ddl-partitioning.html)</sup> 在 BigQuery 中，过滤条件必须组织得当，使服务无需读取表数据即可确定要扫描哪些分区；将 `AND` 改为 `OR` 会破坏修剪，因为此时无法排除不匹配的分区。<sup>[8](https://docs.cloud.google.com/bigquery/docs/querying-partitioned-tables?hl=en)</sup>

## 成本与限制

分区并非没有代价，过度分区是一种公认的失败模式。

**计划与内存开销。** PostgreSQL 的文档警告说，分区过多意味着更长的查询计划时间以及计划和执行期间更高的内存消耗；只有当典型查询能修剪到只剩少数几个分区时，计划器才能很好地处理多达几千个分区的层次结构。<sup>[4](https://www.postgresql.org/docs/15/ddl-partitioning.html)</sup> SQL Server 支持每张表或索引最多 15,000 个分区，但建议除非绝对必要，避免设计包含数百或数千个分区的方案，并建议在使用大量分区时至少配备 16 GB 内存，否则 DML、DDL 和其他操作可能因内存不足而失败。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup> 在分区数超过 1,000 的表上创建或重建非对齐索引是可能的，但不受支持，存在性能下降或内存消耗过度的风险。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup>

**何时无益。** 不使用分区消除的查询随着分区数增长可能耗时更长。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup> PostgreSQL 的最佳实践指导是按最常出现在 `WHERE` 子句中的列进行分区，以便修剪能够生效，并仔细选择分区数量：太少会使索引过大、数据局部性差，太多则会提高计划成本。<sup>[4](https://www.postgresql.org/docs/15/ddl-partitioning.html)</sup> 云数据仓库还有运营配额；例如 BigQuery 限制每张表每天的分区修改次数，并建议当分区数将超过其限制时改用聚簇。<sup>[9](https://docs.cloud.google.com/bigquery/quotas)</sup>

## 分区与分片

分区和分片都将数据划分为更小的片段，但范围不同。分区在单个数据库服务器内运行，而分片将数据分布到多台服务器或机器上；企业通常在读密集的数据库上采用分区，在写密集的扩展场景中采用分片，两者可以结合使用。<sup>[5](https://www.itpro.com/technology/big-data/what-is-database-sharding-how-benefit-enterprise-it)</sup> PostgreSQL 说明了这一界限：其声明式分区将所有分区保留在一个数据库中，而将分区分布到各节点上是外部扩展的职责，而非核心功能。<sup>[2](https://www.postgresql.org/docs/current/ddl-partitioning.html)</sup>

## 采用历史

分区已从附加组件转变为开源数据库的核心功能。表分区在 PostgreSQL 中存在多年，早于版本 10，但要求用户维护一套不简单的规则和触发器；2017 年 10 月发布的 PostgreSQL 10 引入了针对范围和列表分区表的声明式分区语法，作为计划中的分区框架的一部分。<sup>[10](https://www.postgresql.org/about/news/postgresql-10-released-1786/)</sup> Oracle 的分区方法可追溯到 Oracle8（范围，1997 年前后的 8.0 版）和 Oracle8i（哈希和复合）。<sup>[7](https://docs.oracle.com/cd/A87860_01/doc/server.817/a76962/ch2.htm)</sup> 在 SQL Server 中，分区表和索引在 SQL Server 2016 SP1 之前仅限于某些版本，现在已在各版本以及 [Azure SQL 数据库](https://www.edgepedia.org/zh/azure-sql-database)和 Azure SQL 托管实例的所有服务层级中可用。<sup>[1](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)</sup>

## References

1. [Partitioned Tables and Indexes, Microsoft Learn](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver17)
2. [PostgreSQL Documentation: Table Partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html)
3. [Oracle Database 18c VLDB and Partitioning Guide: Partition Pruning](https://docs.oracle.com/en/database/oracle/oracle-database/18/vldbg/partition-pruning.html)
4. [PostgreSQL 15 Documentation: Table Partitioning](https://www.postgresql.org/docs/15/ddl-partitioning.html)
5. [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)
6. [Oracle Partitioning Overview](https://www.oracle.com/database/technologies/partitioning.html)
7. [Oracle8i (8.1.5) New Features: Partitioning Enhancements, Oracle Documentation](https://docs.oracle.com/cd/A87860_01/doc/server.817/a76962/ch2.htm)
8. [Query partitioned tables, Google Cloud Documentation](https://docs.cloud.google.com/bigquery/docs/querying-partitioned-tables?hl=en)
9. [Quotas and limits, BigQuery, Google Cloud Documentation](https://docs.cloud.google.com/bigquery/quotas)
10. [PostgreSQL 10 Released, PostgreSQL Global Development Group](https://www.postgresql.org/about/news/postgresql-10-released-1786/)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database engines and systems › Database deployment, administration and operation

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/partition-database

English original: https://www.edgepedia.org/partition-database
