MongoDB计数性能非常慢

时间:2014-06-24 12:53:31

标签: performance mongodb mongodb-.net-driver mongodb-query

我们使用的是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上留下了一个索引。

1 个答案:

答案 0 :(得分:0)

我曾经使用过.go core的mongo驱动程序和我的计数器方法;

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

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