MongoDB 'count()' 非常慢.我们如何改进/解决它? [英] MongoDB 'count()' is very slow. How do we refine/work around with it?

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

问题描述

我目前正在使用具有数百万条数据记录的 MongoDB.我发现一件很烦人的事情.

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

当我使用'count()'函数进行少量查询数据收集时,速度非常快.但是,当查询到的数据集包含数千甚至数百万条数据记录时,整个系统就会变得很慢.

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();

如果您需要一些计数器,我建议尽可能预先计算它们.通过使用原子 $inc 操作而不使用 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.

但是 mongodb 的人在 mongodb 上努力工作,因此,根据 jira 错误.

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

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

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