简体   繁体   English

MongoDB数百万行的计数非常慢

[英]MongoDB Count on millions of row is very slow

Using MongoDB 2.4 with mongoDB .NET driver 3.2 将MongoDB 2.4与mongoDB .NET驱动程序3.2结合使用

I have a collection with 30 million entries. 我的收藏有3000万个条目。

var visits = new MongoHelper<CampaignVisitLog>()
         .GetCollection().AsQueryable().Count(t => t.campaignId == campaignId);

campaignId is indexed. campaignId已建立索引。 Depending on many entries campaignId has it will take from 30 seconds to several minutes to return the count. 根据CampaignId是否有很多条目,返回计数将需要30秒到几分钟的时间。

Whats the correct way to count this collection? 什么是计算此收藏集的正确方法?

I have a collection with 30 million entries. 我的收藏有3000万个条目。

Independently of optimizing the whole query, you can't think that you're going to get blazing-fast responses with millions of items. 独立于优化整个查询,您不能认为您将获得数百万个项目的快速响应。

If you're performing other queries to get stats, maybe it's the time to schedule this calculations and do them with some asynchronous service (ie a Windows service , Windows scheduled task , Quartz.NET ...), and get their results also asynchronously. 如果您正在执行其他查询以获取统计信息,也许是时候安排此计算并使用一些异步服务( 例如Windows服务Windows计划任务Quartz.NET ...)进行处理,并且还异步获取其结果了。

You can either use MongoDB to store your calculation service results or go for a more specific solution: a service bus (ie RabbitMQ , Azure Service Bus , NServiceBus ...). 您可以使用MongoDB存储您的计算服务结果,也可以使用更特定的解决方案: 服务总线 (即RabbitMQAzure Service BusNServiceBus ...)。

MongoDB C# driver is sick. MongoDB C#驱动程序有病。

LINQ queries are always translated to aggregation framework pipelines. LINQ查询始终会转换为聚合框架管道。

var pipeline = [ { "$group" : { "_id" : 1, "__result" : { "$sum" : 1 } } }]
db.test.aggregate(pipeline)

This results in a full collection scan on the server because the LINQ query did not specify any constraints. 由于LINQ查询未指定任何约束,因此将在服务器上进行完整的集合扫描。

ensure you have enough memory to store your index in ram. 确保您有足够的内存将索引存储在ram中。

https://docs.mongodb.org/manual/tutorial/ensure-indexes-fit-ram/ https://docs.mongodb.org/manual/tutorial/ensure-indexes-fit-ram/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM