Model Computed Data模型计算数据

On this page本页内容

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,您必须对放映标题为“水库狗”的电影的影院执行读取,并将这些字段的值相加。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.可以定期重新生成列表,同时独立更新基础数据。
←  Model IoT DataData Model Reference →