<div dir="ltr">Interesting, and times do you get for 500 ets lookup on that data?</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 26, 2021 at 3:12 PM Jacob <<a href="mailto:jacob01@gmx.net">jacob01@gmx.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
assuming that the match spec compiler does clever things with patterns,<br>
I'd use select with the following<br>
<br>
   MatchExpression = [ {{'_', K, '_'}, [], ['$_']} || K <- Keys ]<br>
<br>
If did some quick measurements with plain ETS, timer:tc and 500 keys out<br>
of 1000000 table entries and got:<br>
<br>
   * using pattern, [ordered_set]: 4532605 us<br>
   * using pattern, [set]              :  4645525 us<br>
   * using pattern, [ordered_set, {keypos, 2}]: 3826 us (!!!)<br>
   * using pattern, [set, {keypos, 2}]: 5714 us (!!!)<br>
<br>
   * using guards, [ordered_set]: 12542928 us<br>
   * using guards, [set]:               12310452 us<br>
   * using guards, [ordered_set, {keypos, 2}]: 12365477 us<br>
   * using guards, [set, {keypos, 2}]: 12277839 us<br>
<br>
I have initialised the DB with [ ets:insert(t, {N, N,<br>
integer_to_list(N)}) || N <- lists:seq(1, 1000000) ].<br>
<br>
I don't know though, how this will translate to Mnesia, but I'd give<br>
select with pattern on the primary key a try.<br>
<br>
/Jacob<br>
<br>
<br>
On 2/26/21 11:03 AM, Vance Shipley wrote:<br>
> If I need to lookup a list of keys which is the better approach? Why?<br>
><br>
> Fselect = fun(Keys) -><br>
>         MatchHead = {'_', '$1', '$2'},<br>
>         F = fun(Key) -><br>
>                 {'=:=', '$1', Key}<br>
>         end,<br>
>         MatchConditions = [list_to_tuple(['or' | lists:map(F, Keys)]),<br>
>         MatchBody = ['$_'],<br>
>         MatchFunction = {MatchHead, MatchConditions, MatchBody},<br>
>         MatchExpression = [MatchFunction],<br>
>         mnesia:select(Table, MatchExpression)<br>
> end,<br>
> mnesia:transaction(Fselect, [Keys]).<br>
><br>
> Fread = fun F([Key | T], Acc) -><br>
>                 [R] = mnesia:read(Table, Key),<br>
>                 F(T, [R | Acc]);<br>
>         F([], Acc) -><br>
>                 lists:reverse(Acc)<br>
> end,<br>
> mnesia:transaction(Fread, [Keys. []]).<br>
><br>
><br>
> --<br>
>      -Vance<br>
</blockquote></div>