有没有办法在 Google App Engine 上做聚合函数? [英] Is there a way to do aggregate functions on Google App Engine?
问题描述
关系数据库支持的好处之一是聚合函数,如 count、sum、avg 等.但似乎如果您使用 GAE,在插入或更新记录时,您必须计算和存储 count、sum、avg等整个表的值.但是如果你有很多条件分组呢?给定一个人:
One of the nice things relational databases support are the aggregate functions like count, sum, avg etc. But it seems that if you are using GAE, when inserting or updating a record you must calculate and store the count, sum, avg, etc. values of the whole table. But what if you have many conditional groupings? Given a Person:
class Person {
@Id
Integer age;
String city;
}
如果我愿意
- 总人数和
- 平均年龄
每次我创建、更新或删除一个人时,我还应该计算两个聚合并将它们作为单独的列存储在同一个表中是否正确.如果我还想要每个城市的总值和平均值,我是否应该将每个城市的这些值也作为单独的列存储在同一个表中?
Is it correct that everytime I create, update or delete a person I should also calculate both aggregates and store them as separate columns in the same table. If I also want the total and average values per city, should I store these values for each city also as separate columns in the same table?
推荐答案
正确:要正确使用 GAE 存储,您需要有选择地对模型的某些方面进行反规范化,保留冗余"数据,这些数据在正常的 DB 中形式,您可以即时重新计算,例如聚合(整体和分组").
Right: to use GAE storage properly, you need to selectively de-normalize some aspects of your model, keeping "redundant" data which, in a DB in normal form, you'd recompute on the fly, such as aggregates (overall and "grouped by" ones).
但是,在您的情况下,不要将此类字段添加到 Person
表中——这毫无意义!制作另一个 PersonAggregates
表,其中包含 City(总体总数为空/缺失)、Count、TotalAges 等列(更易于维护:随时计算平均值为总数除以计数).
However, don't add such fields to the Person
table in your case -- that makes little sense! Make another PersonAggregates
table with columns such as City (null/missing for overall totals), Count, TotalAges (easier to maintain: compute the average at any time as total divided by count).
这篇关于有没有办法在 Google App Engine 上做聚合函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!