Docs HomeMongoDB Manual

Model Computed Data模型计算数据

Overview概述

Often, an application needs to derive a value from source data stored in a database. 通常,应用程序需要从存储在数据库中的源数据中派生值。Computing a new value may require significant CPU resources, especially in the case of large data sets or in cases where multiple documents must be examined.计算一个新值可能需要大量的CPU资源,尤其是在大型数据集或必须检查多个文档的情况下。

If a computed value is requested often, it can be more efficient to save that value in the database ahead of time. 如果经常请求计算值,那么提前将该值保存在数据库中会更有效。This way, when the application requests data, only one read operation is required.这样,当应用程序请求数据时,只需要一次读取操作。

Computed Pattern计算模式

If your reads significantly outnumber your writes, the computed pattern reduces the frequency of having to perform computations. 如果您的读取次数明显多于写入次数,则计算模式会降低执行计算的频率。Instead of attaching the burden of computation to every read, the application stores the computed value and recalculates it as needed. 应用程序存储计算值并根据需要重新计算,而不是将计算负担附加到每次读取。The application can either recompute the value with every write that changes the computed value's source data, or as part of a periodic job.应用程序可以在每次写入更改计算值的源数据时重新计算值,也可以作为周期性作业的一部分。

Note

With periodic updates, the computed value is not guaranteed to be exact in any given read. 在定期更新的情况下,不能保证计算值在任何给定的读取中都是准确的。However, this approach may be worth the performance boost if exact accuracy isn't a requirement.然而,如果不要求精确的精度,这种方法可能值得提高性能。

Example实例

An application displays movie viewer and revenue information.应用程序显示电影查看器和收入信息。

Consider the following screenings collection:考虑以下screenings集合:

// screenings collection

{
"theater": "Alger Cinema",
"location": "Lakeview, OR",
"movie_title": "Reservoir Dogs",
"num_viewers": 344,
"revenue": 3440
}
{
"theater": "City Cinema",
"location": "New York, NY",
"movie_title": "Reservoir Dogs",
"num_viewers": 1496,
"revenue": 22440
}
{
"theater": "Overland Park Cinema",
"location": "Boise, ID",
"movie_title": "Reservoir Dogs",
"num_viewers": 760,
"revenue": 7600
}

Users often want to know how many people saw a certain movie and how much money that movie made. 用户通常想知道有多少人看过某部电影,以及这部电影赚了多少钱。In this example, to total num_viewers and revenue, you must perform a read for theaters that screened a movie with the title "Reservoir Dogs" and sum the values of those fields. 在本例中,为了合计num_viewersrevenue,必须对放映标题为“Reservoir Dogs”的电影的影院执行读取操作,并对这些字段的值求和。To avoid performing that computation every time the information is requested, you can compute the total values and store them in a movies collection with the movie record itself:为了避免每次请求信息时都执行这种计算,您可以计算总值,并将它们与movies记录本身一起存储在电影集合中:

// movies collection

{
"title": "Reservoir Dogs",
"total_viewers": 2600,
"total_revenue": 33480,
...
}

In a low write environment, the computation could be done in conjunction with any update of the screenings data.在低写环境中,可以结合screenings数据的任何更新来进行计算。

In an environment with more regular writes, the computations could be done at defined intervals - every hour for example. 在具有更规则写入的环境中,计算可以按定义的间隔进行,例如每小时一次。The source data in screenings isn't affected by writes to the movies collection, so you can run calculations at any time.screenings中的源数据不受写入movies集合的影响,因此您可以随时运行计算。

This is a common design pattern that reduces CPU workload and increases application performance. 这是一种常见的设计模式,可以减少CPU工作负载并提高应用程序性能。Whenever you are performing the same calculations repeatedly and you have a high read to write ratio, consider the Computed Pattern.每当重复执行相同的计算并且读写比较高时,请考虑“计算模式”。

Other Sample Use Cases其他示例用例

In addition to cases where summing is requested frequently, such as getting total revenue or viewers in the movie database example, the computed pattern is a good fit wherever calculations need to be run against data. 除了频繁请求求和的情况外,例如在电影数据库示例中获得总收入或观众,计算模式非常适合需要根据数据进行计算的地方。For example:例如:

  • A car company that runs massive aggregation queries on vehicle data, storing results to show for the next few hours until the data is recomputed.一家汽车公司,对车辆数据进行大规模聚合查询,存储结果,在接下来的几个小时内显示,直到重新计算数据。
  • A consumer reporting company that compiles data from several different sources to create rank-ordered lists like the "100 Best-Reviewed Gadgets". 一家消费者报告公司,它汇编来自多个不同来源的数据,以创建排名有序的列表,如“100个最佳评论小工具”。The lists can be regenerated periodically while the underlying data is updated independently.可以定期重新生成列表,同时独立更新基础数据。