如何提高MongoDB中update()和save()的性能? [英] How to improve performance of update() and save() in MongoDB?

查看:169
本文介绍了如何提高MongoDB中update()和save()的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下,我正在寻找有关如何提高数据库性能的提示.

I'm looking for tips on how to improve the database performance in the following situation.

作为一个示例应用程序,我今天编写了一个相当简单的应用程序,该应用程序使用Twitter流API来搜索某些关键字,然后将结果存储在MongoDB中.该应用程序是使用Node.js编写的.

As a sample application, I wrote a fairly simple app today that uses the Twitter streaming API to search for certain keywords, then I am storing the results in MongoDB. The app is written with Node.js.

我要存储2个收藏集.一个存储关键字和一组推特ID的数组,该ID引用找到的每个提及该关键字的推文.这些将通过带有{upsert:true}的.update()添加到数据库中,以便将新的id附加到'ids'数组中.

I'm storing 2 collections. One stores the keyword and an array of tweet id's that reference each tweet found mentioning that keyword. These are being added to the database using .update() with {upsert:true} so that new id's are appended to the 'ids' array.

此集合中的示例文档如下:

A sample document from this collection looks like this:

{ "_id":ObjectId("4e00645ef58a7ad3fc9fd9f9"), "ids":["id1","id2","id3"], 关键字":#chocolate" }

{ "_id": ObjectId("4e00645ef58a7ad3fc9fd9f9"), "ids": ["id1","id2","id3"], "keyword": "#chocolate" }

更新代码:

 keywords.update({keyword: key_word},{$push:{ids: id}},{upsert:true}, function(err){})

第二个集合看起来像这样,只需使用.save()即可添加

The 2nd collection looks like this and are added simply by using .save()

 {
     "twt_id": "id1",
     "tweet": { //big chunk of json that doesn't need to be shown }
 }

我现在已经在Macbook上运行了它,并且运行了大约2个小时.我正在存储很多数据,可能每分钟有几百个文档.现在,Mongodb中的对象数为120k +.

I've got this running on my Macbook right now and its been going for about 2 hours. I'm storing a lot of data, probably several hundred documents per minute. Right now the number of objects in Mongodb is 120k+.

我要注意的是,自从我开始最新的测试运行以来,数据库进程的CPU使用率已高达84%,并且一直在逐渐上升.

What I'm noticing is that the cpu usage for the database process is hitting as high as 84% and has been constantly going up gradually since I started the latest test run.

我正在阅读设置索引,但是由于我正在添加文档并且未针对它们运行查询,因此我不确定索引是否会有所帮助.我想到的一个侧面是,由于我正在使用$ push,因此update()可能正在执行查找,而索引可能对此有所帮助.

I was reading up on setting indexes, but since I'm adding documents and not running queries against them, I'm not sure if indexes will help. A side thought that occurred to me is that update() might be doing a lookup since I'm using $push and that an index might help with that.

我应该怎么看才能使MongoDB避免消耗越来越多的CPU?

What should I be looking at to keep MongoDB from eating up ever increasing amounts of CPU?

推荐答案

您在正确的道路上.更新的查询部分需要索引,否则它正在运行表扫描.关键字缩进索引,您会发现更新性能显着提高.

You're on the right path. The query portion of your update needs an index, else it is running a table scan. An indent index on keyword and you'll see update performance increase significantly.

这篇关于如何提高MongoDB中update()和save()的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆