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