简体   繁体   中英

MongoDB count query performance

I have problem with count performance in MongoDB.

I'm using ZF2 and Doctrine ODM with SoftDelete filter. Now when query "first time" collection with db.getCollection('order').count({"deletedAt": null}) , it takes about 30 seconds, sometimes even more. Second and more query takes about 150ms. After few minutes query takes again about 30 seconds. This is only on collections with size > 700MB.

Server is Amazon EC2 t2.medium instance, Mongo 3.0.1

Maybe it similar to MongoDB preload documents into RAM for better performance , but those answers do not solve my problem.

Any ideas what is going on?

/edit

explain

{
"executionSuccess" : true,
"nReturned" : 111449,
"executionTimeMillis" : 24966,
"totalKeysExamined" : 0,
"totalDocsExamined" : 111449,
"executionStages" : {
    "stage" : "COLLSCAN",
    "filter" : {
        "$and" : []
    },
    "nReturned" : 111449,
    "executionTimeMillisEstimate" : 281,
    "works" : 145111,
    "advanced" : 111449,
    "needTime" : 1,
    "needFetch" : 33660,
    "saveState" : 33660,
    "restoreState" : 33660,
    "isEOF" : 1,
    "invalidates" : 0,
    "direction" : "forward",
    "docsExamined" : 111449
},
"allPlansExecution" : []
}

The count will go through each document which is creating performance issues.

Care about the precise number if it's a small one. You're interested to know if there are 100 results or 500. But once it goes beyond, let's say, 10000, you can just say 'More than 10000 results' found to the user.

db.getCollection('order').find({"deletedAt": null}).limit(10000).count(true)

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