paperbot · PL 论文追踪

全部论文 待读 收藏

P2 reformulator: Automated Refactoring of the N+1 Problem in Database-Backed Applications

reformulator:数据库支撑应用中 N+1 问题的自动重构
ASE Proceedings of the 37th IEEE/ACM International Conference on Automated Software Engineering2022匹配度 3/10 · 论文属于程序分析与自动重构方向,涉及静态分析/抽象解释和代码生成,但与用户关注的 PL→AI、AI→PL、浮点程序分析、贝叶斯程序分析以及 LLM/Agent 交叉均无直接关系,也未涉及 ML 系统或 Agentic 程序。引用 3 inbox
Alexi Turcotte, Mark W. Aldrich, Frank Tip
crossref: 2026-09-12

AI 速览

一句话:提出 reformulator,自动重构 ORM 应用中的 N+1 查询反模式,将多次数据库往返合并为单次查询。

对象关系映射(ORM)提供面向对象的数据库访问接口,使程序员无需编写 SQL,而是调用普通方法,由 ORM 映射为数据库查询。但该抽象层隐藏了数据库操作的高昂代价,误用 ORM 会生成远超必要的查询。其中“N+1 问题”尤为典型:首次查询得到 N 条结果,随后对每条结果各发一次查询,共产生 N+1 次数据库往返。在面向对象语言中迭代集合并调用 ORM 方法十分自然,却常导致性能次优。论文提出 reformulator,对数据库支撑应用中的此类代码进行自动重构,将多次往返合并为一次获取全部结果的较大查询。

问题
ORM 的抽象层隐藏数据库访问开销,程序员在迭代数据库来源的集合时逐条调用 ORM 方法,触发 N+1 查询,造成大量冗余数据库往返和性能下降。
方法
提出 reformulator,自动识别并重构数据库支撑应用中的 N+1 反模式,将逐条触发的查询改写为一次性获取全部目标结果的单次较大查询。
评估
摘要未报告具体评估方式或实验结果,仅指出在此类场景下可通过合并查询来减少数据库往返次数。
原文摘要(Abstract)

An Object-Relational Mapping (ORM) provides an object-oriented interface to a database and facilitates the development of database-backed applications. In an ORM, programmers do not need to write queries in a separate query language such as SQL, they instead write ordinary method calls that are mapped by the ORM to database queries. This added layer of abstraction hides the significant performance cost of database operations, and misuse of ORMs can lead to far more queries being generated than necessary. Of particular concern is the infamous “N+1 problem”, where an initial query yields N results that are used to issue N subsequent queries. This anti-pattern is prevalent in applications that use ORMs, as it is natural to iterate over collections in object-oriented languages. However, iterating over data that originates from a database and calling an ORM method in each iteration may result in suboptimal performance. In such cases, it is often possible to reduce the number of round-trips to the database by issuing a single, larger query that fetches all desired results at once.

链接与引用

DOI 原文 ·

BibTeX
@inproceedings{paperbot7240,
  title = {reformulator: Automated Refactoring of the N+1 Problem in Database-Backed Applications},
  author = {Alexi Turcotte and Mark W. Aldrich and Frank Tip},
  booktitle = {ASE},
  year = {2022},
  doi = {10.1145/3551349.3556911}
}