简体   繁体   中英

MongoDB Count Perfomance very slow

We are on Mongodb version : 2.4.9

Looks like there was a fix on 2.3.2 for Count problem

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

But still my count operation is very slow I cannot able to do pagination just because of the count operation taking like 10 seconds for 3.5 million records.

Any one have idea on this?

Edit

Explain() results in:

{

    "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

Query (I am using C# Driver provided by MongoDB)

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

Document Structure:

{
    "_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"
}

When i try to create a compound index on by_dateCreated_termMointerIds_socialType like below

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

Its slowing up the Count query even further down.

Datecreated is the field that narrow down the search so much. So i left it with one index on Datecreated.

I used to mongo driver for .net core and my counter method;

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

usage link: https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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