Re: Performance of mnesia:select/2

Jacob <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <[email protected]>
Hi,

assuming that the match spec compiler does clever things with patterns,
I'd use select with the following

   MatchExpression = [ {{'_', K, '_'}, [], ['$_']} || K <- Keys ]

If did some quick measurements with plain ETS, timer:tc and 500 keys out
of 1000000 table entries and got:

   * using pattern, [ordered_set]: 4532605 us
   * using pattern, [set]              :  4645525 us
   * using pattern, [ordered_set, {keypos, 2}]: 3826 us (!!!)
   * using pattern, [set, {keypos, 2}]: 5714 us (!!!)

   * using guards, [ordered_set]: 12542928 us
   * using guards, [set]:               12310452 us
   * using guards, [ordered_set, {keypos, 2}]: 12365477 us
   * using guards, [set, {keypos, 2}]: 12277839 us

I have initialised the DB with [ ets:insert(t, {N, N,
integer_to_list(N)}) || N <- lists:seq(1, 1000000) ].

I don't know though, how this will translate to Mnesia, but I'd give
select with pattern on the primary key a try.

/Jacob


On 2/26/21 11:03 AM, Vance Shipley wrote:
> If I need to lookup a list of keys which is the better approach? Why?
>
> Fselect = fun(Keys) ->
>         MatchHead = {'_', '$1', '$2'},
>         F = fun(Key) ->
>                 {'=:=', '$1', Key}
>         end,
>         MatchConditions = [list_to_tuple(['or' | lists:map(F, Keys)]),
>         MatchBody = ['$_'],
>         MatchFunction = {MatchHead, MatchConditions, MatchBody},
>         MatchExpression = [MatchFunction],
>         mnesia:select(Table, MatchExpression)
> end,
> mnesia:transaction(Fselect, [Keys]).
>
> Fread = fun F([Key | T], Acc) ->
>                 [R] = mnesia:read(Table, Key),
>                 F(T, [R | Acc]);
>         F([], Acc) ->
>                 lists:reverse(Acc)
> end,
> mnesia:transaction(Fread, [Keys. []]).
>
>
> --
>      -Vance
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.