2

I asked Is cursor.skip() on indexed keys always faster?

Now I am trying to leverage the multi-key indexing (page 94) to perform faster queries.

Used this script to create 2 databases:

var a = 0;
while(a++ < 4096*2){
  var randomnumber=Math.ceil(Math.random()*1000000)

  // create string in length of 3
  var name = Math.random().toString(36).substring(2,5);

  db.fast.insert({name:name, email:name + ".email@example.com", age:randomnumber});
  db.slow.insert({name:name, email:name + ".email@example.com", age:randomnumber});

}

The databases are indexed as follows:

> db.fast.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.fast"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test.fast"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 1,
            "name" : 1,
            "email" : 1
        },
        "name" : "age_1_name_1_email_1",
        "ns" : "test.fast"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 0,
            "name" : 0,
            "email" : 0
        },
        "name" : "age_0_name_0_email_0",
        "ns" : "test.fast"
    }
]
> 
> db.slow.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.slow"
    }
]

I executed these 2 queries:

>pageNumber=18;nPerPage=20; db.slow.find({name:{$gt:"1234", $lt:"z"}, age:{$gt:200, $lt:777217}, email:{$gt:"bdnsa28831283d", $lt:"z"} }).skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")

    "executionSuccess" : true,
    "nReturned" : 20,
    "executionTimeMillis" : 93,
    "totalKeysExamined" : 0,
    "totalDocsExamined" : 688,


>pageNumber=18;nPerPage=20; db.fast.find({name:{$gt:"1234", $lt:"z"}, age:{$gt:200, $lt:777217}, email:{$gt:"bdnsa28831283d", $lt:"z"} }).skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")

    "executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 20,
    "executionTimeMillis" : 70,
    "totalKeysExamined" : 559,
    "totalDocsExamined" : 360,

As you can see, the execution time of the fast db was better as expected.

But after that I executed the following two and the slow was much better:

>pageNumber=58;nPerPage=300; db.slow.find({name:{$gt:"1Adsa34", $lt:"zZ123z"}, age:{$gt:4555, $lt:988888}, email:{$gt:"abdnsa28831283d", $lt:"z2"} }).skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 0,
        "executionTimeMillis" : 30,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 8192,

>pageNumber=58;nPerPage=300; db.fast.find({name:{$gt:"1Adsa34", $lt:"zZ123z"}, age:{$gt:4555, $lt:988888}, email:{$gt:"abdnsa28831283d", $lt:"z2"} }).skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")
        "executionSuccess" : true,
        "nReturned" : 0,
        "executionTimeMillis" : 101,
        "totalKeysExamined" : 8058,
        "totalDocsExamined" : 5484,

Might be somehow service's cache issue, although I restarted the mongod.


Slow execution plan:

"queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "test.slow",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [
            {
                "age" : {
                    "$lt" : 988888
                }
            },
            {
                "email" : {
                    "$lt" : "z2"
                }
            },
            {
                "name" : {
                    "$lt" : "zZ123z"
                }
            },
            {
                "age" : {
                    "$gt" : 4555
                }
            },
            {
                "email" : {
                    "$gt" : "abdnsa28831283d"
                }
            },
            {
                "name" : {
                    "$gt" : "1Adsa34"
                }
            }
        ]
    },
    "winningPlan" : {
        "stage" : "LIMIT",
        "limitAmount" : 300,
        "inputStage" : {
            "stage" : "SKIP",
            "skipAmount" : 11616,
            "inputStage" : {
                "stage" : "COLLSCAN",
                "filter" : {
                    "$and" : [
                        {
                            "age" : {
                                "$lt" : 988888
                            }
                        },
                        {
                            "email" : {
                                "$lt" : "z2"
                            }
                        },
                        {
                            "name" : {
                                "$lt" : "zZ123z"
                            }
                        },
                        {
                            "age" : {
                                "$gt" : 4555
                            }
                        },
                        {
                            "email" : {
                                "$gt" : "abdnsa28831283d"
                            }
                        },
                        {
                            "name" : {
                                "$gt" : "1Adsa34"
                            }
                        }
                    ]
                },
                "direction" : "forward"
            }
        }
    },
    "rejectedPlans" : [ ]
},

Fast DB execution plan:

"queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "test.fast",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [
            {
                "age" : {
                    "$lt" : 988888
                }
            },
            {
                "email" : {
                    "$lt" : "z2"
                }
            },
            {
                "name" : {
                    "$lt" : "zZ123z"
                }
            },
            {
                "age" : {
                    "$gt" : 4555
                }
            },
            {
                "email" : {
                    "$gt" : "abdnsa28831283d"
                }
            },
            {
                "name" : {
                    "$gt" : "1Adsa34"
                }
            }
        ]
    },
    "winningPlan" : {
        "stage" : "LIMIT",
        "limitAmount" : 300,
        "inputStage" : {
            "stage" : "SKIP",
            "skipAmount" : 17100,
            "inputStage" : {
                "stage" : "FETCH",
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "age" : 0,
                        "name" : 0,
                        "email" : 0
                    },
                    "indexName" : "age_0_name_0_email_0",
                    "isMultiKey" : false,
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 1,
                    "direction" : "forward",
                    "indexBounds" : {
                        "age" : [
                            "(4555.0, 988888.0)"
                        ],
                        "name" : [
                            "(\"1Adsa34\", \"zZ123z\")"
                        ],
                        "email" : [
                            "(\"abdnsa28831283d\", \"z2\")"
                        ]
                    }
                }
            }
        }
    },
    "rejectedPlans" : [
        {
            "stage" : "LIMIT",
            "limitAmount" : 300,
            "inputStage" : {
                "stage" : "SKIP",
                "skipAmount" : 17100,
                "inputStage" : {
                    "stage" : "FETCH",
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "age" : 1,
                            "name" : 1,
                            "email" : 1
                        },
                        "indexName" : "age_1_name_1_email_1",
                        "isMultiKey" : false,
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 1,
                        "direction" : "forward",
                        "indexBounds" : {
                            "age" : [
                                "(4555.0, 988888.0)"
                            ],
                            "name" : [
                                "(\"1Adsa34\", \"zZ123z\")"
                            ],
                            "email" : [
                                "(\"abdnsa28831283d\", \"z2\")"
                            ]
                        }
                    }
                }
            }
        },
        {
            "stage" : "LIMIT",
            "limitAmount" : 300,
            "inputStage" : {
                "stage" : "SKIP",
                "skipAmount" : 17100,
                "inputStage" : {
                    "stage" : "FETCH",
                    "filter" : {
                        "$and" : [
                            {
                                "email" : {
                                    "$lt" : "z2"
                                }
                            },
                            {
                                "name" : {
                                    "$lt" : "zZ123z"
                                }
                            },
                            {
                                "email" : {
                                    "$gt" : "abdnsa28831283d"
                                }
                            },
                            {
                                "name" : {
                                    "$gt" : "1Adsa34"
                                }
                            }
                        ]
                    },
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "age" : 1
                        },
                        "indexName" : "age_1",
                        "isMultiKey" : false,
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 1,
                        "direction" : "forward",
                        "indexBounds" : {
                            "age" : [
                                "(4555.0, 988888.0)"
                            ]
                        }
                    }
                }
            }
        }
    ]
},
Community
  • 1
  • 1
0x90
  • 39,472
  • 36
  • 165
  • 245
  • You are querying on non-index keys and it's understood to be slower. I'm sorry but I don't see point. – Saleem Mar 14 '16 at 20:12
  • @Saleem, what do you mean by non index-keys. see the output of `db.fast.getIndexes()`. Obviously all keys are strings and are sortable and can be stored in a B-Tree datastructure. – 0x90 Mar 14 '16 at 20:21
  • Well, you know having index on all keys doesn't mean index is actually being used in query. Please if possible, show us full execution plan – Saleem Mar 14 '16 at 20:36
  • @Saleem, attached. – 0x90 Mar 14 '16 at 20:42

1 Answers1

1

Yes, you are right, I have verified and ironically query assumed to be fast with covered index, is ignoring fastest execution plan.

I have raised this issue with MongoDB. Please see Jira Ticket

Saleem
  • 8,728
  • 2
  • 20
  • 34