问题描述
对于不断增长的文档,我可以选择使用 update()
或 findAndModify / code>到在数组字段中插入新元素。但是,
findAndModify()
将在我的特定应用程序中生成更干净的代码。但我可以使用 update()
实现相同的功能,更麻烦。
For a constantly growing document, I have the choice of using either update()
or findAndModify()
to insert new elements in an array field. However, findAndModify()
will generate cleaner code in my specific application. But I can achieve the same functionality using update()
, just more messy.
很多是使用 update()
而不是 findAndModify()
的性能增益。非常感谢!
I'm just wondering how much is the performance gain by using update()
instead of findAndModify()
. Thanks a lot!
推荐答案
我使用mongo shell运行了一个快速测试。对于此文档:
I ran a quick test using the mongo shell. For this document:
{
"c" : 50,
"growingArray" : [ 0 ]
}
我进行了两个测试:
for (i = 1; i < 10000; i++)
{
db.testids.update( { "c" : 50 }, { $addToSet : { "growingArray" : i } } );
}
在我的中间共计 40.926秒 -range laptop。
Took a total of 40.926s on my mid-range laptop.
findAndModify()
版本:
for (i = 1; i < 10000; i++)
{
db.testids.findAndModify({ query: { "c" : 50 }, update : { $addToSet : { "growingArray" : i } } } );
}
花了 42.445 秒。我平均每次跑五次。
took 42.445 seconds. I averaged five runs of each.
从中得到你想要的。弯腰的结论是,在我的特定环境中, findAndModify()
的开销大约为3%到4%,没有其他事情发生。请记住,findAndModify()获得写锁定,这样可以对应用程序的整体性能产生更大的影响。
Take from it what you will. The knee-jerk conclusion is that you have about a 3% to 4% overhead with findAndModify()
in my particular environment with nothing else going on. Keep in mind though that findAndModify() obtains write locks so you could have a much greater impact on performance of your application overall.
这篇关于Mongodb update()vs. findAndModify()performace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!