Skip to content

Commit

Permalink
use text prefix in regex to speed up query
Browse files Browse the repository at this point in the history
for selector;

{"selector":{"_id":{"$regex":"doc.+"}}}

before;

{
  "include_docs": true,
  "view_type": "map",
  "reduce": false,
  "partition": null,
  "start_key": [],
  "end_key": [
    "<MAX>"
  ],
  "direction": "fwd",
  "stable": false,
  "update": true,
  "conflicts": "undefined"
}

after;

{
  "include_docs": true,
  "view_type": "map",
  "reduce": false,
  "partition": null,
  "start_key": [
    "doc"
  ],
  "end_key": [
    "doc�",
    "<MAX>"
  ],
  "direction": "fwd",
  "stable": false,
  "update": true,
  "conflicts": "undefined"
}

closes: #4775
  • Loading branch information
rnewson committed Sep 26, 2023
1 parent b6ea325 commit a6bf85a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/mango/src/mango_idx_view.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
-include("mango.hrl").
-include("mango_idx_view.hrl").

-define(PREFIX_RE, "^\\w+").

validate_new(#idx{} = Idx, _Db) ->
{ok, Def} = do_validate(Idx#idx.def),
{ok, Idx#idx{def = Def}}.
Expand Down Expand Up @@ -310,6 +312,8 @@ indexable({[{<<"$gte">>, _}]}) ->
% Making `$exists` indexable should not cause problems in other cases.
indexable({[{<<"$exists">>, _}]}) ->
true;
indexable({[{<<"$regex">>, _}]}) ->
true;
% All other operators are currently not indexable.
% This is also a subtle assertion that we don't
% call indexable/1 on a field name.
Expand Down Expand Up @@ -485,6 +489,14 @@ range({[{<<"$gt">>, Arg}]}, LCmp, Low, HCmp, High) ->
max ->
empty
end;
% use any text prefix in the regex to narrow the query
range({[{<<"$regex">>, Arg}]}, LCmp, Low, HCmp, High) ->
case re:run(Arg, ?PREFIX_RE, [{capture,first, binary}]) of
{match, [Prefix]} ->
{'$gte', Prefix, '$lte', <<Prefix/binary, 16#10FFFF>>};
nomatch ->
{LCmp, Low, HCmp, High}
end;
% There's some other un-indexable restriction on the index
% that will be applied as a post-filter. Ignore it and
% carry on our merry way.
Expand Down

0 comments on commit a6bf85a

Please sign in to comment.