本文介绍了Mongodb update()与findAndModify()性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于不断增长的文档,我可以选择使用update()findAndModify()在数组字段中插入新元素.但是,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.926s .

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()与findAndModify()性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:02