First query after index creation is slow

I added an index to the collection. The first query I run is slower than the same one without an index. The following are faster than those without an index, so this makes sense.

I'm wondering why this might be happening because the index had to go from disk to memory? Then it's even harder for me to figure out that I drop the index, restart the mangon, I create the index again and it goes fast and not the first time. In case I restart my computer, it behaves like the first time, so it only goes slow the first time with the index.

Can anyone explain this behavior clearly?

Below I will cover information about documents, indexes and queries. The documents inside the collection look like this:

> db.posts.findOne()
{
        "_id" : ObjectId("557d73e1fab73211b00f3080"),
        "title" : "aaa",
        "author" : "nuevo",
        "body" : "aaa",
        "permalink" : "aaa",
        "tags" : [
                "a"
        ],
        "comments" : [ ],
        "date" : ISODate("2015-06-14T12:30:25.733Z")
}

      

Collection size:

> db.posts.find().count()
1008

      

Query without index, it takes 3ms (I am not putting the entire output of the explanation, only the relevant parts):

> db.posts.explain("executionStats").find({ permalink: "ambzrbxvnorazgnqvzbw"});

{
....
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 3,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 1008,
....
}

      

Index creation:

> db.posts.createIndex({permalink:1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 3,
        "numIndexesAfter" : 4,
        "ok" : 1
}

      

Query with generated index (71 ms):

> db.posts.explain("executionStats").find({ permalink: "ambzrbxvnorazgnqvzbw"});

{
....
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 71,
                "totalKeysExamined" : 1,
                "totalDocsExamined" : 1,
....
}

      

Restart the same query with a different permalink to avoid it from memory (or something similar). It takes 0ms:

> db.posts.explain("executionStats").find({ permalink: "orrjnueekntvjegzvbjk"});

{
....
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 1,
                "totalDocsExamined" : 1,
....
}

      

+3


source to share


1 answer


Are you on Linux? Linux uses all free memory as disk cache. Even after restarting mongo, this cache remains until the system is needed for something else. Queries that hit the cache will be fast even without indexes - because they hit memory. There are commands to prove that - caching check and miss.

An unbuffered read (which should hit the platters on the hard drive) takes too much time than reading into memory (no matter how small the amount of data you read, disk cache, memory buffers, etc. read megabytes forward, even if you are interested in one byte).



See https://gist.github.com/jboner/2841832 for some real numbers.

I think if you take a look at http://docs.mongodb.org/manual/administration/analyzing-mongodb-performance/#administration-monitoring-page-faults and http://docs.mongodb.org/manual/reference/glossary / # term-page-fault you will be able to confirm that slow access is basically a 100% page fault (everything has to be read from the hard drive), where fast access will be almost 100% hit (cached reads).

+2


source







All Articles