mongodb 搜索速度_MongoDB:查找count()命令在集合中数百万条记录上的执行时间?...

I am trying to find time required to perform a count() on a collection which is consisting of millions of testdata records, with following scenario:-

1) From 1st Mongo shell I am inserting millions of records into collection using a code

for (var i = 0; i < 10000000; ++i){

db.unicorns.insert({name: 'sampleName', gender: 'm', weight: '440' });

}

2) From 2ndMongo shell I am trying to find count() on that collection(Imp: while insertion is still getting executed on 1st Mongo Shell)

db.unicorns.count()

I researched but found that explain() and stats() cannot be applied to count() command.

some

I need to find out how much time it takes to count() when there are insertions going on collection(something like a live scenario)?

Is there any other good approach for doing this?

解决方案

MongoDB has a built-in profiller that you can enable via:

db.setProfilingLevel(2)

Instead of '2' you can choose any option from the list bellow:

0 - the profiler is off, does not collect any data. mongod always writes operations longer than the slowOpThresholdMs threshold to its log.

1 - collects profiling data for slow operations only. By default slow operations are those slower than 100 milliseconds.

You can modify the threshold for “slow” operations with the slowOpThresholdMs runtime option or the setParameter command. See the Specify the Threshold for Slow Operations section for more information.

2 - collects profiling data for all database operations.

And you can see the results of your queries by checking the system.profile collection in MongoDB..

EDIT:

If you want to test performance you could use the following snippets of code that can be executed from the mongo console:

> for (var i = 0; i < 10000000; ++i) { db.countTest.insert({a: i % 10}) }

> db.countTest.ensureIndex({a:1})

> db.countTest.count({a: 1})

> db.countTest.count()

> db.countTest.find().count()

And my conclusions are as following:

adding an index (appart from the id) returned the count for 10 million records in around 170ms

counting by id (count without any query) returned the count in less than a millisecond

counting by id with cursor (note that the .find() will act as a cursor over the collection) returned the count in less than a millisecond

So the more indexes your collection has the slower your query will be. If you count by _id it will be instant, if you have a composite index it will scale based on the number of indexes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值