I have performance issue when I use regex without prefix match to find any part in text or the end of text (find queries with explain plan below)
as per mongodb performance for regex search will be slow unless we use preffix match, but in my case I need to do search without prefix match.
what is the best way to get the best performance for regex search without prefix
db.PERSON.find( {"arNames.arName" : {$regex: "XAVIER"}}).explain("executionStats");
db.PERSON.find( {"arNames.arName" : {$regex: "XAVIER$"}}).explain("executionStats");
db.PERSON.find( {"arNames.arName" : {$regex: "^XAVIER"}}).explain("executionStats");
1) any part of text
db.PERSON.find( {"arNames.arName" : {$regex: "XAVIER"}}).explain("executionStats");
"executionStats" : {
"executionSuccess" : true,
"nReturned" : NumberInt(1397),
"executionTimeMillis" : NumberInt(70877),
"totalKeysExamined" : NumberInt(1730874),
"totalDocsExamined" : NumberInt(1414867),
"executionStages" : {
"stage" : "KEEP_MUTATIONS",
"nReturned" : NumberInt(1397),
"executionTimeMillisEstimate" : NumberInt(70600),
"works" : NumberInt(1730875),
"advanced" : NumberInt(1397),
"needTime" : NumberInt(1729477),
"needFetch" : NumberInt(0),
"saveState" : NumberInt(13526),
"restoreState" : NumberInt(13526),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"arNames.arName" : /XAVIER/
},
"nReturned" : NumberInt(1397),
"executionTimeMillisEstimate" : NumberInt(70560),
"works" : NumberInt(1730875),
"advanced" : NumberInt(1397),
"needTime" : NumberInt(1729477),
"needFetch" : NumberInt(0),
"saveState" : NumberInt(13526),
"restoreState" : NumberInt(13526),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"docsExamined" : NumberInt(1414867),
"alreadyHasObj" : NumberInt(0),
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : NumberInt(1414867),
"executionTimeMillisEstimate" : NumberInt(66820),
"works" : NumberInt(1730874),
"advanced" : NumberInt(1414867),
"needTime" : NumberInt(316007),
"needFetch" : NumberInt(0),
"saveState" : NumberInt(13526),
"restoreState" : NumberInt(13526),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"keyPattern" : {
"arNames.arName" : NumberInt(1)
},
"indexName" : "arNames.arName_1",
"isMultiKey" : true,
"direction" : "forward",
"indexBounds" : {
"arNames.arName" : [
"[\"\", {})",
"[/XAVIER/, /XAVIER/]"
]
},
"keysExamined" : NumberInt(1730874),
"dupsTested" : NumberInt(1730874),
"dupsDropped" : NumberInt(316007),
"seenInvalidated" : NumberInt(0),
"matchTested" : NumberInt(0)
}
}
}
}
2) end of text
db.PERSON.find( {"arNames.arName" : {$regex: "XAVIER$"}}).explain("executionStats");
"executionStats" : {
"executionSuccess" : true,
"nReturned" : NumberInt(0),
"executionTimeMillis" : NumberInt(66463),
"totalKeysExamined" : NumberInt(1730874),
"totalDocsExamined" : NumberInt(1414867),
"executionStages" : {
"stage" : "KEEP_MUTATIONS",
"nReturned" : NumberInt(0),
"executionTimeMillisEstimate" : NumberInt(66080),
"works" : NumberInt(1730875),
"advanced" : NumberInt(0),
"needTime" : NumberInt(1730874),
"needFetch" : NumberInt(0),
"saveState" : NumberInt(13526),
"restoreState" : NumberInt(13526),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"arNames.arName" : /XAVIER$/
},
"nReturned" : NumberInt(0),
"executionTimeMillisEstimate" : NumberInt(66050),
"works" : NumberInt(1730875),
"advanced" : NumberInt(0),
"needTime" : NumberInt(1730874),
"needFetch" : NumberInt(0),
"saveState" : NumberInt(13526),
"restoreState" : NumberInt(13526),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"docsExamined" : NumberInt(1414867),
"alreadyHasObj" : NumberInt(0),
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : NumberInt(1414867),
"executionTimeMillisEstimate" : NumberInt(62360),
"works" : NumberInt(1730874),
"advanced" : NumberInt(1414867),
"needTime" : NumberInt(316007),
"needFetch" : NumberInt(0),
"saveState" : NumberInt(13526),
"restoreState" : NumberInt(13526),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"keyPattern" : {
"arNames.arName" : NumberInt(1)
},
"indexName" : "arNames.arName_1",
"isMultiKey" : true,
"direction" : "forward",
"indexBounds" : {
"arNames.arName" : [
"[\"\", {})",
"[/XAVIER$/, /XAVIER$/]"
]
},
"keysExamined" : NumberInt(1730874),
"dupsTested" : NumberInt(1730874),
"dupsDropped" : NumberInt(316007),
"seenInvalidated" : NumberInt(0),
"matchTested" : NumberInt(0)
}
}
}
}
3) with prefix
db.PERSON.find( {"arNames.arName" : {$regex: "^XAVIER"}}).explain("executionStats");
"executionStats" : {
"executionSuccess" : true,
"nReturned" : NumberInt(1397),
"executionTimeMillis" : NumberInt(7),
"totalKeysExamined" : NumberInt(1398),
"totalDocsExamined" : NumberInt(1397),
"executionStages" : {
"stage" : "FETCH",
"nReturned" : NumberInt(1397),
"executionTimeMillisEstimate" : NumberInt(10),
"works" : NumberInt(1398),
"advanced" : NumberInt(1397),
"needTime" : NumberInt(0),
"needFetch" : NumberInt(0),
"saveState" : NumberInt(10),
"restoreState" : NumberInt(10),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"docsExamined" : NumberInt(1397),
"alreadyHasObj" : NumberInt(0),
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : NumberInt(1397),
"executionTimeMillisEstimate" : NumberInt(10),
"works" : NumberInt(1398),
"advanced" : NumberInt(1397),
"needTime" : NumberInt(0),
"needFetch" : NumberInt(0),
"saveState" : NumberInt(10),
"restoreState" : NumberInt(10),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"keyPattern" : {
"arNames.arName" : NumberInt(1)
},
"indexName" : "arNames.arName_1",
"isMultiKey" : true,
"direction" : "forward",
"indexBounds" : {
"arNames.arName" : [
"[\"XAVIER\", \"XAVIES\")",
"[/^XAVIER/, /^XAVIER/]"
]
},
"keysExamined" : NumberInt(1398),
"dupsTested" : NumberInt(1397),
"dupsDropped" : NumberInt(0),
"seenInvalidated" : NumberInt(0),
"matchTested" : NumberInt(0)
}
}
}