# 增量处理

**增量处理**是一种数据工程模式，管道只处理自上次运行以来发生变化的数据，而不是重新加载或重新计算整个数据集。在提取、转换、加载（ETL）实践中，它表现为增量加载，即工具按固定间隔加载源系统与目标系统之间的增量或差异，并存储上次提取日期，以便只加载该日期之后新增的记录<sup>[1](https://aws.amazon.com/what-is/etl/)</sup>。在分析引擎中，同样的思路表现为增量模型构建和物化视图的增量刷新，每次运行只触及源值发生变化的行<sup>[2](https://docs.getdbt.com/docs/build/incremental-models)</sup>。与之相对的替代方案是全量加载或全量刷新，每次运行都会移动或重新计算全部内容<sup>[1](https://aws.amazon.com/what-is/etl/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 核心机制 | 每次运行只处理自上次成功运行以来新增、更新或删除的记录，使用存储的边界（如上次提取日期）<sup>[1](https://aws.amazon.com/what-is/etl/)</sup><sup> • </sup><sup>[3](https://www.ltimindtree.com/wp-content/uploads/2025/06/ETL-Testing.pdf)</sup> |
| 数据量效应 | 全量同步与增量同步之间的数据量差异可达一到两个数量级，仓库成本也相应不同<sup>[4](https://images.g2crowd.com/uploads/attachment/file/1304467/How-to-choose-the-most-cost-effective-data-pipeline-for-your-business.pdf)</sup> |
| 成本交叉点 | 当待更新记录占总量的15%至25%之间时，全量刷新与增量更新的单条记录成本趋于相同<sup>[5](https://business.baylor.edu/gina_green/teaching/dw/spr16/Ponniah_data-warehousing-fundamentals-for-it-professionalsSecondEdition.pdf)</sup> |
| 变更检测 | 时间戳或递增ID（拉取）以及来自日志或触发器的变更数据捕获（推送）是识别变更的主要方式<sup>[6](https://get.fivetran.com/rs/353-UTB-444/images/The-ultimate-guide-to-modern-data-movement-on-the-Lakehouse.pdf)</sup><sup> • </sup><sup>[7](https://datatoolbox.net/the-fundamentals/1-2-data-engineering-architecture/data-pipelines-guide-data-engineers/)</sup> |
| 正确性条件 | 源端的插入、更新和删除不一定对应目标端的插入、更新和删除；管道必须通过其查询逻辑来转换变更<sup>[8](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)</sup> |
| 流式类比 | 触发的增量管道处理自上次运行以来到达的所有数据后停止；连续管道不间断运行，数据到达即处理<sup>[9](https://docs.databricks.com/aws/en/compute/serverless/streaming)</sup> |

## 工作原理

增量管道在多次运行之间维护状态。通常的边界是一个刷新书签，记录目标表上次成功处理的边界；每次加载开始时，管道检索该书签，只读取其后的源窗口<sup>[8](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)</sup>。在dbt中，增量模型的首次运行通过转换源数据的全部行来构建目标表；后续运行时，模型只转换按指定条件筛选出的行，通常是自上次运行以来创建或更新的行，并将其插入已有的目标表<sup>[2](https://docs.getdbt.com/docs/build/incremental-models)</sup>。当现有`unique_key`的新信息到达时，该信息可以替换当前行而不是追加<sup>[2](https://docs.getdbt.com/docs/build/incremental-models)</sup>。

由于涉及状态，正确的增量加载建立在一个正确性标准之上：它应当产生与全量查询相同的最终目标，同时做更少的工作。首先编写表达业务逻辑的全量查询，然后将其限制在可能因上游源行变化而需要操作的主键集合上，即驱动集<sup>[8](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)</sup>。这保持了统一性，因为相同的查询逻辑同时支撑全量加载和增量加载；也保持了幂等性，因为重试运行会通过书签重新读取相同的源变更窗口<sup>[8](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)</sup>。

## 检测变更

增量处理需要识别对源上一状态所做的变更，这一实践称为变更数据捕获（CDC）<sup>[4](https://images.g2crowd.com/uploads/attachment/file/1304467/How-to-choose-the-most-cost-effective-data-pipeline-for-your-business.pdf)</sup>。CDC将数据库视为一组变更而非一个完整的静态数据库；CDC馈送的每一行通常携带元数据，如操作类型和一个用于确定性排序的列，以便处理乱序更新<sup>[10](https://docs.databricks.com/gcp/en/data-engineering/what-is-cdc)</sup>。基于日志的CDC最常用于从数据库进行增量更新<sup>[6](https://get.fivetran.com/rs/353-UTB-444/images/The-ultimate-guide-to-modern-data-movement-on-the-Lakehouse.pdf)</sup>。

当源不提供变更馈送时，管道退回到依赖源属性。对于不可变的、仅追加的数据，如交易、事件日志或传感器日志，变更就是新记录，检测很简单：按创建时间戳大于上次摄取时间、标识符大于上次处理值进行过滤，或从日志中上次消费的偏移量恢复<sup>[7](https://datatoolbox.net/the-fundamentals/1-2-data-engineering-architecture/data-pipelines-guide-data-engineers/)</sup>。对于可变数据，如客户、产品或账户，可管理的数据集可以选择全量重载或定期快照，更大的数据集则选择通过时间戳、版本或CDC进行变更跟踪的增量加载；比较两个数据集状态的快照可以识别插入、更新和删除<sup>[7](https://datatoolbox.net/the-fundamentals/1-2-data-engineering-architecture/data-pipelines-guide-data-engineers/)</sup>。审计列（如最后修改日期）在仓库刷新中发挥同样的作用<sup>[3](https://www.ltimindtree.com/wp-content/uploads/2025/06/ETL-Testing.pdf)</sup>。

在时间序列系统中，边界是明确的。TimescaleDB在其目录中为每个聚合存储一个物化水位线，标记已汇总数据与仍在等待聚合的原始行之间的边界；刷新策略始终是增量的，在有界窗口内推进水位线，只有在以较宽的范围或空值范围参数显式调用时才进行全量重算<sup>[11](https://github.com/time-series-data/time-series-data.org/blob/main/content/continuous-aggregate-creation-refresh-management/incremental-vs-full-refresh-strategies/index.md)</sup>。

## 将源变更转换为目标操作

跟踪变更告诉管道上游发生了什么变化，而不是下游应该发生什么变化。源端的插入、更新和删除不一定对应目标端的插入、更新和删除：向一个源表插入数据可能需要更新一个聚合目标，而删除可能需要重新计算派生行<sup>[8](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)</sup>。因此，响应变更是一门将上游变更转换为最小正确目标操作集的学问，以主键为中心：计算其结果可能已变化的目标键，然后只对这些键重新运行常规查询<sup>[8](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)</sup>。

删除需要特殊处理。与全量加载不同，增量提取无法通过比较暂存区与目标之间的全部主键集来发现删除，因为暂存表只保存最少的记录集；删除逻辑必须针对查询进行定制，分析源变更的影响<sup>[8](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)</sup>。也存在一种优雅的退路：如果上游变更触及许多行，驱动集会扩大，但由于暂存表与目标进行了比较，只有真正变化的行才会被应用，增量加载的行为就像全量加载一样<sup>[8](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)</sup>。

同样的转换适用于模型层面。dbt区分增量模型（保留每个id的最新行）和快照（保留历史）；仅增量适用于列出变更或携带可靠变更时间戳的源，而仅快照适用于就地覆盖行的源，前提是需要历史且每次运行扫描源是可接受的<sup>[12](https://docs.getdbt.com/best-practices/how-we-handle-cdc/2-choosing-incremental-or-snapshots)</sup>。

## 增量与全量刷新

选择的关键在于将模式匹配到数据量、变更率、所需新鲜度和运维复杂度的约束上，而不是哪种模式普遍更好。一个常见的指导原则是：当数据量为小到中等（约100 GB或1亿行以下），且所需频率为每天或更低时，选择全量刷新，因为一个每晚只需15分钟就能重载的表不值得增加增量的复杂性；全量刷新还能提供提取时刻的快照，没有累积漂移，这对监管报告、财务对账和合规表很重要<sup>[13](https://www.systemoverflow.com/learn/etl-elt-patterns/full-refresh-vs-incremental/when-to-choose-full-refresh-vs-incremental-decision-framework)</sup>。增量适用于更大的数据量，需要在水位线管理、幂等写入和漂移监控方面具备运维成熟度<sup>[13](https://www.systemoverflow.com/learn/etl-elt-patterns/full-refresh-vs-incremental/when-to-choose-full-refresh-vs-incremental-decision-framework)</sup>。

经济学上存在一个交叉点。根据数据仓库实践的经验法则，一旦待更新记录数达到总量的约15%至25%，无论仓库是全量刷新还是增量更新，单条记录的加载成本大致相同<sup>[5](https://business.baylor.edu/gina_green/teaching/dw/spr16/Ponniah_data-warehousing-fundamentals-for-it-professionalsSecondEdition.pdf)</sup>。超过该比例后，截断重载的简洁性（无需通过主键或时间戳跟踪变更<sup>[3](https://www.ltimindtree.com/wp-content/uploads/2025/06/ETL-Testing.pdf)</sup>）使全量刷新更占优势。把每个管道都做成增量并没有什么额外好处；如果重载全部数据既便宜又简单，全量往往是更好的设计<sup>[7](https://datatoolbox.net/the-fundamentals/1-2-data-engineering-architecture/data-pipelines-guide-data-engineers/)</sup>。

增量处理的好处在大型、频繁变化的源上最为显著。对超大源的全量同步可能需要数小时、数天或数周，无法提供及时更新，可能超时，并在消耗过多网络带宽的同时拖慢源和目标<sup>[4](https://images.g2crowd.com/uploads/attachment/file/1304467/How-to-choose-the-most-cost-effective-data-pipeline-for-your-business.pdf)</sup>。在集成平台中，同样的逻辑适用：增量处理只更新变化的记录，减少高流量操作（否则需要重载整张表）的服务器负载和同步延迟<sup>[14](https://help.salesforce.com/s/articleView?id=000382007&language=en_US&type=1)</sup>。

## 与流式处理的关系

增量批处理和流处理共享一个核心属性：引擎跟踪已处理的内容，在后续运行中只处理新数据；在普通批处理语义下，每次处理源中当前可用的所有数据，并覆盖之前的结果<sup>[15](https://docs.databricks.com/gcp/en/data-engineering/batch-vs-streaming)</sup>。Databricks将云对象存储和[Delta Lake](https://www.edgepedia.org/zh/delta-lake)等源视为流式源以实现高效的增量处理，并将触发的管道归类为增量模式：按计划或触发器运行，处理自上次运行以来到达的所有数据，然后停止，延迟以分钟计而非连续模式的秒级<sup>[9](https://docs.databricks.com/aws/en/compute/serverless/streaming)</sup>。对于小数据量，增量加载可以实现为将变更持续流式传输到目标，事件流处理将同样的方法扩展到每秒数百万事件<sup>[1](https://aws.amazon.com/what-is/etl/)</sup>。这一领域的产品，如[Apache Hudi](https://www.edgepedia.org/zh/apache-hudi)，提供从流式源和云存储进行增量摄取的功能，并自动管理检查点<sup>[16](https://hudi.apache.org/docs/next/use_cases/)</sup>。

## 增量计算

除了移动数据，增量处理还扩展到重新计算派生结果。阿里云MaxCompute中的动态物化视图使用增量计算来捕获源表的变更，并以统一的SQL逻辑持续执行增量计算，同时支持流式和批处理<sup>[17](https://www.alibabacloud.com/help/en/maxcompute/incremental-calculation-overview)</sup>。研究系统将此形式化为[增量视图维护](https://www.edgepedia.org/zh/incremental-view-maintenance)：Enzyme系统提出了物化视图增量刷新规划和执行的设计决策，包括利用物化视图源之间批处理机会的优化<sup>[18](https://arxiv.org/abs/2603.27775)</sup>。这一思想也适用于分析工作负载本身：模型物化和增量模型复用保留已构建的机器学习模型而不是丢弃它们，并通过添加或删除数据来调整现有模型以构建新模型，文中描述了线性回归、朴素贝叶斯和逻辑回归的详细技术<sup>[19](https://arxiv.org/abs/1509.05066)</sup>。

## References

1. [What is ETL? Extract, Transform, Load Explained, AWS](https://aws.amazon.com/what-is/etl/)
2. [Configure incremental models, dbt Developer Hub](https://docs.getdbt.com/docs/build/incremental-models)
3. [Data Warehouse Testing, LTIMindtree](https://www.ltimindtree.com/wp-content/uploads/2025/06/ETL-Testing.pdf)
4. [How to choose the most cost-effective data pipeline for your business, G2 Crowd (Fivetran)](https://images.g2crowd.com/uploads/attachment/file/1304467/How-to-choose-the-most-cost-effective-data-pipeline-for-your-business.pdf)
5. [Data Warehousing Fundamentals for IT Professionals, Second Edition, Ponniah](https://business.baylor.edu/gina_green/teaching/dw/spr16/Ponniah_data-warehousing-fundamentals-for-it-professionalsSecondEdition.pdf)
6. [The ultimate guide to modern data movement on the Lakehouse, Fivetran](https://get.fivetran.com/rs/353-UTB-444/images/The-ultimate-guide-to-modern-data-movement-on-the-Lakehouse.pdf)
7. [Ultimate Guide to Data Pipelines for Data Engineers, Data Toolbox](https://datatoolbox.net/the-fundamentals/1-2-data-engineering-architecture/data-pipelines-guide-data-engineers/)
8. [Incremental load: responding to change, Principles of Data Engineering](https://principlesofdataengineering.org/docs/efficient-stable-pipeline/responding-to-change/)
9. [Streaming on serverless compute, Databricks on AWS](https://docs.databricks.com/aws/en/compute/serverless/streaming)
10. [Change data capture and snapshots, Databricks on Google Cloud](https://docs.databricks.com/gcp/en/data-engineering/what-is-cdc)
11. [Incremental vs Full Refresh Strategies, time-series-data.org](https://github.com/time-series-data/time-series-data.org/blob/main/content/continuous-aggregate-creation-refresh-management/incremental-vs-full-refresh-strategies/index.md)
12. [Choosing incremental models or snapshots, dbt Developer Hub](https://docs.getdbt.com/best-practices/how-we-handle-cdc/2-choosing-incremental-or-snapshots)
13. [When to Choose Full Refresh vs Incremental: Decision Framework, System Overflow](https://www.systemoverflow.com/learn/etl-elt-patterns/full-refresh-vs-incremental/when-to-choose-full-refresh-vs-incremental-decision-framework)
14. [Incremental processing for Heavy Bulk API and DML use cases, Salesforce Help](https://help.salesforce.com/s/articleView?id=000382007&language=en_US&type=1)
15. [Batch vs. streaming data processing in Databricks](https://docs.databricks.com/gcp/en/data-engineering/batch-vs-streaming)
16. [Use Cases, Apache Hudi](https://hudi.apache.org/docs/next/use_cases/)
17. [Incremental computing, MaxCompute, Alibaba Cloud Documentation Center](https://www.alibabacloud.com/help/en/maxcompute/incremental-calculation-overview)
18. [Enzyme: Incremental View Maintenance for Data Engineering, arXiv](https://arxiv.org/abs/2603.27775)
19. [Processing Analytical Workloads Incrementally, Gupta et al., arXiv:1509.05066](https://arxiv.org/abs/1509.05066)

---
*Initially written Oct 26, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Data mining, warehousing, and big data › Big data concepts

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/incremental-processing

English original: https://www.edgepedia.org/incremental-processing
