Skip to content Skip to sidebar Skip to footer

Is Cursor.skip() On Indexed Keys Always Faster?

I have 2 databases: slow and fast; each of which was fed with 4096 entries. The age key is a unique random integer that was generated with this script: var arr = [] while(arr.lengt

Solution 1:

Neither of your queries are doing a filter on age, so there is no reason to use the index.

If you add a condition on age, there will be a difference (even if minimal with so few documents)

> pageNumber=18;nPerPage=20; db.slow.find({age:{$gt:200}}).
      skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).
      explain("executionStats")

# "executionTimeMillis" : 14,# "inputStage" : {# "stage" : "COLLSCAN",
> pageNumber=18;nPerPage=20; db.fast.find({age:{$gt:200}}).
      skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).
      explain("executionStats"

# "executionTimeMillis" : 0,# "inputStage" : {# "stage" : "IXSCAN",

Solution 2:

As displayed you have index on AGE field, so search by indexed field is faster.

The paging issue comes from fact that, you query is not covered which means it need to fetch full document - so index do not come into play in this case.

Please see this for reference

Post a Comment for "Is Cursor.skip() On Indexed Keys Always Faster?"