MongoDB:查找count()命令在集合中数百万条记录上的执行时间? [英] MongoDB: find execution time for count() command on millions of records in a collection?

查看:1625
本文介绍了MongoDB:查找count()命令在集合中数百万条记录上的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下,我试图找到对包含数百万个testdata记录的集合执行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)我从1st Mongo shell中使用代码将数百万条记录插入到集合中

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)我试图从2ndMongo Shell上找到该集合的count()( Imp:,而插入仍在第一Mongo Shell上执行)

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

我研究了一下,但是发现explain()和stats()无法应用于count()命令.

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

一些

我需要找出在进行插入操作(例如现场场景)时count()需要花费多少时间?

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

还有其他好的方法吗?

推荐答案

MongoDB具有内置的填充器,您可以通过以下方式启用填充器:

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

db.setProfilingLevel(2)

您可以从下面的列表中选择任何选项来代替"2":

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

  • 0 -探查器已关闭,不收集任何数据. mongod对其日志的写入操作始终要比其slowOpThresholdMs阈值长.
  • 1 -收集性能分析数据,仅用于慢速操作.默认情况下,慢速操作是指那些慢于100毫秒的操作. 您可以使用slowOpThresholdMs运行时选项或setParameter命令修改慢速"操作的阈值.有关更多信息,请参见指定慢速操作阈值"部分.
  • 2 -收集所有数据库操作的性能分析数据.
  • 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.

然后您可以通过检查MongoDB中的 system.profile 集合来查看查询结果.

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

如果要测试性能,可以使用以下可从mongo控制台执行的代码片段:

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

我的结论如下:

  1. 添加索引(ID中的appart)在 170ms
  2. 内返回了 1000万条记录的计数
  3. 按ID计数(没有任何查询的计数)返回的计数以不到一毫秒
  4. 为单位
  5. 通过带有光标的id计数(请注意,.find()将充当集合上的光标)以不到一毫秒
  6. 的形式返回计数>
  1. adding an index (appart from the id) returned the count for 10 million records in around 170ms
  2. counting by id (count without any query) returned the count in less than a millisecond
  3. 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

因此,您集合中的更多索引具有查询速度变慢.如果您通过_id进行计数,它将是即时,如果您具有综合索引,它将根据索引数进行缩放.

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.

这篇关于MongoDB:查找count()命令在集合中数百万条记录上的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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