-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Trivial - P5
-
None
-
Affects Version/s: None
-
Component/s: Querying
-
None
-
Query Optimization
-
Fully Compatible
Left-anchored regular expressions all of whose characters are non-special don't need to run the regex engine, but can just do the appropriate range query upfrom the prefix upto the prefix plus 1 (so to speak).
IndexBoundsBuilder already includes code to find the longest non-special prefix of an anchored regular expression. Seems like it ought to be straightforward to check if the index bounds string is the same length as the regex string plus 1 (for the caret) somewhere downstream in query.
No idea if this will make any use case appreciably faster, but it's an obvious "missing" optimization that should be easy to implement and maintain.
Presumably one way to test for the existence of (and so regressions in) this improvement would be to compare the explain() plans for these two queries:
db.foo.find({s:/^abc/}); db.foo.find({s:/^abc[qz]/});