Tyhou
Tyhou

Reputation: 321

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).

Upvotes: 1

Views: 4367

Answers (3)

Sridhar Nanjundeswaran
Sridhar Nanjundeswaran

Reputation: 754

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/.

Upvotes: 1

Ori Dar
Ori Dar

Reputation: 19000

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

Upvotes: 3

baklarz2048
baklarz2048

Reputation: 10938

First request read data from disk Second request is from memory.

Upvotes: 1

Related Questions