简体   繁体   中英

MongoDB first query is slow

I have noticed this problem for a while with mongo, yet haven't found any internal documentation about it, or how to mitigate this problem. Here's an explain on two of the exact same queries, one done right after the other.

> db.derp.find().explain()
{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 8418,
    "nscannedObjects" : 8418,
    "nscanned" : 8418,
    "nscannedObjectsAllPlans" : 8418,
    "nscannedAllPlans" : 8418,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 3,
    "nChunkSkips" : 0,
    "millis" : 3267,
    "indexBounds" : {

    },
    "server" : ...
}

Now the second run:

> db.derp.find().explain()
{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 8418,
    "nscannedObjects" : 8418,
    "nscanned" : 8418,
    "nscannedObjectsAllPlans" : 8418,
    "nscannedAllPlans" : 8418,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 6,
    "indexBounds" : {

    },
    "server" : ...
}

That's a pretty drastic difference in query speed, from 3.2 seconds down to 6 milliseconds. I'm looking for some information on this internal caching that's going on here, and if there's any way to tune this cache(in the interest of keeping that data cached).

The first time the query asked, pages were loaded into resident memory. This is why the second query did hardly cost in response time

You can run server status working set command before each time you run the query to examine the effect. This should be done on a "freshly started database" of course

第一个请求从磁盘读取数据,第二个请求从内存读取。

The difference could be due to multiple reasons. As mentioned previously the first query probably loaded from disk and the second had the data already available in RAM. Quick way to test this is to run mongostat and check on the resident RAM before the first run and after and see if this increased resident RAM and also showed page faults. More info on mongostat at http://docs.mongodb.org/manual/reference/program/mongostat/ . If you want to preload data into mongodb you can use the touch command as described at http://docs.mongodb.org/manual/reference/command/touch/ .

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