本文介绍了MongoDB'count()'非常慢。我们如何改进/解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用MongoDB数百万的数据记录。我发现了一件很讨厌的事情。



当我使用count()函数和少量查询数据集合时,它非常快。但是,当查询的数据集合包含千或甚至数百万的数据记录时,整个系统变得非常慢。



我确保我已索引所需的字段。 / p>

有人遇到过同样的事吗?

解决方案

现在有另一个优化比创建适当的索引。

  db.users.ensureIndex({name:1}); 
db.users.find({name:Andrei})。count();

如果你需要一些计数器,我建议尽可能的预先计算它们。使用原子性操作,而不使用 count({} )



但是mongodb们在mongodb上努力工作,所以, count({})根据jira ,在mongodb 2.1中规划的改进。


I am currently using MongoDB with millions of data records. I discovered one thing that's pretty annoying.

When I use 'count()' function with a small number of queried data collection, it's very fast. However, when the queried data collection contains thousand or even millions of data records, the entire system becomes very slow.

I made sure that I have indexed the required fields.

Has anybody encountered an identical thing? How do you do to improve that?

解决方案

There is now another optimization than create proper index.

db.users.ensureIndex({name:1});
db.users.find({name:"Andrei"}).count();

If you need some counters i suggest to precalculate them whenever it possible. By using atomic $inc operation and not use count({}) at all.

But mongodb guys working hard on mongodb, so, count({}) improvements they are planning in mongodb 2.1 according to jira bug.

这篇关于MongoDB'count()'非常慢。我们如何改进/解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:27