1

I heard from the mongodb docs that

For case sensitive regular expression queries, if an index exists for the field, then MongoDB matches the regular expression against the values in the index, which can be faster than a collection scan. Further optimization can occur if the regular expression is a “prefix expression”, which means that all potential matches start with the same string. This allows MongoDB to construct a “range” from that prefix and only match against those values from the index that fall within that range.

query:

db.getCollection('contacts').find({username: {$regex: 'an'}}).explain()

Here is the stats without indexing username

"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 14234,
"nscannedObjects" : 107721,
"nscanned" : 107721,
"nscannedObjectsAllPlans" : 107721,
"nscannedAllPlans" : 107721,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 841,
"nChunkSkips" : 0,
"millis" : 108,
"server" : "random-ubunto:3001",
"filterSet" : false

And stats with indexing username

"cursor" : "BtreeCursor username_1",
"isMultiKey" : false,
"n" : 14234,
"nscannedObjects" : 14234,
"nscanned" : 106898,
"nscannedObjectsAllPlans" : 14234,
"nscannedAllPlans" : 106898,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 835,
"nChunkSkips" : 0,
"millis" : 142,
"indexBounds" : {
    "username" : [ 
        [ 
            "", 
            {}
        ], 
        [ 
            /an/, 
            /an/
        ]
    ]
},
"server" : "random-ubunto:3001",
"filterSet" : false

Yes i can see the difference of nscannedObjects. Thats very good but the question is why the millis of the indexing one are greater than without the indexing one. If we talk about the performance, millis should be vice versa. Currently

millis (Without Indexing) : 108
millis (With Indexing) : 142
Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38

1 Answers1

0

You should have a look on this:

MongoDB, performance of query by regular expression on indexed fields

mentioned in above link:

For /Jon Skeet/ regex ,mongo will full scan the keys in the index then will fetch the matched documents, which can be faster than collection scan.

For /^Jon Skeet/ regex ,mongo will scan only the range that start with the regex in the index, which will be faster.

Community
  • 1
  • 1
Sachin
  • 2,912
  • 16
  • 25