MongoDB Count性能非常慢



我们正在使用Mongodb版本:2.4.9

看起来有一个修复2.3.2的计数问题

https://jira.mongodb.org/browse/server - 1752

但是我的计数操作仍然非常慢,我无法进行分页,因为计数操作需要大约10秒才能完成350万条记录。

有人知道这个吗?

编辑

Explain()导致:

{
    "cursor" : "BtreeCursor by_dateCreated",
    "isMultiKey" : false,
    "n" : 143736,
    "nscannedObjects" : 2893069,
    "nscanned" : 2893069,
    "nscannedObjectsAllPlans" : 2904859,
    "nscannedAllPlans" : 2904859,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 135,
    "nChunkSkips" : 0,
    "millis" : 117730,
    "indexBounds" : {
        "DateCreated" : [[ISODate("2014-06-24T13:36:26.952Z"), ISODate("2013-08-28T13:36:26.952Z")]]
    },
}

Edit2

查询(我正在使用MongoDB提供的c#驱动程序)

var entities2 = (from e in this.collection.AsQueryable<SocialRecord>()
                 where (e.DateCreated >= fr) && (e.DateCreated <= to) 
                 && bArray.Contains(e.TermMonitorIds) 
                 &&(sources.Contains(e.SocialType))
                 select e).OrderByDescending(e => e.DateCreated);
return entities2.Count();

Edit3

文档结构:

{
    "_id" : ObjectId("53a456b27f781d19f40ac76c"),
    "DateCreated" : ISODate("2014-06-20T15:35:56.000Z"),
    "SocialType" : "facebook",
    "RecordId" : "1474971936_10202431655820767",
    "UserId" : "1474971936",
    "UserProfileUrl" : "",
    "UserProfilePictureUrl" : "/Downloads/v3/432bfeb8-901e-45a4-b739-1f3f48b69d61/facebook/2014-6/1946689/10492432_10202426005479512_740185019259071925_t.jpg",
    "Description" : "",
    "MediaHiResUrl" : "",
    "MediaLowResUrl" : "",
    "MediaMedResUrl" : "",
    "SocialCount" : NumberLong(354),
    "SocialCountType" : "likes",
    "Sentiment" : "",
    "SentimentScore" : "0.0000000",
    "IsLocalContent" : true,
    "IsExactMatch" : true,
    "IsHashTag" : false,
    "IsActive" : false,
    "MediaType" : "image",
    "TermMonitorIds" : [ 
        "432bfeb8-901e-45a4-b739-1f3f48b69d61"
    ],
    "UserName" : "",
    "DisplayName" : "",
    "DirectUrl" : "",
    "IsUk" : true,
    "IsEnglish" : true,
    "Language" : "en",
    "Location" : "GB",
    "DataVersion" : "v3"
}

当我尝试创建一个复合索引by_dateCreated_termMointerIds_socialType如下

{
    "DateCreated" : -1,
    "SocialType" : 1,
    "TermMonitorIds" : 1
}

它进一步减慢了Count查询的速度。

Datecreated是缩小搜索范围的字段。所以我在Datecreated上留下了一个索引

我曾经为。net core和我的计数器方法mongo driver;

var result = collection.CountDocuments(_ => true);

使用链接:https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/

最新更新