1

我有一个简单的字典,它定义了一个基本记录,如下所示:

record = {
    'h': site_hash, #combination of date (below) and site id hashed with md5
    'dt': d,                            # date - YYYYMMDD
    'si': data['site'],                 # site id
    'cl': data['client'],               # client id
    'nt': data['type'],                 # site type
}

然后我调用以下内容来更新记录,如果它不存在以下内容:

collection.update(
    record, 
    {'$inc':updates}, # updates contain some values that increase such as events: 1, actions:1, etc
    True # do upsert
);

我想知道如果我将上面的内容更改为以下内容是否会有更好的性能,因为下面的代码只查看现有的“h”值而不是 h/dt/si/cl/nt,我只需要在“h”上使用 ensureIndex ' 场地。但是,显然 $set 每次都会执行,导致更多的写入记录,而不仅仅是 $inc。

record = {
    'h': site_hash, #combination of date (below) and site id hashed with md5
}
values = {
    'dt': d,                            # date - YYYYMMDD
    'si': data['site'],                 # site id
    'cl': data['client'],               # client id
    'nt': data['type'],                 # site type
}
collection.update(
    record, 
    {'$inc':updates,'$set':values},
    True # do upsert
);

有人对这里的最佳实践有任何提示或建议吗?

4

1 回答 1

0

如果“h”已经是唯一的,那么您可以在 h 上创建一个索引,不需要索引“dt”、“si”等。在这种情况下,我希望您的第一个示例在非常重的负载下性能更高一些,因为它会在日志中创建较小的条目,原因有些模糊。

于 2013-05-30T21:31:29.363 回答