dirty_select a limited number of records

Roberto Ostinelli ostinelli@REDACTED
Tue Nov 26 18:33:22 CET 2019


All,
I've got a mnesia table that I create with:

mnesia:create_table(my_table, [
    {type, set},
    {attributes, record_info(fields, my_table)},
    {index, [#my_table.pid]},
    {storage_properties, [{ets, [{read_concurrency, true},
{write_concurrency, true}]}]}
]).

If I want to get all records for the secondary index, I do:

mnesia:dirty_index_read(my_table, Pid, #my_table.pid).

This operation if very fast. Now, sometimes I just need the *first*
matching record and if there are many entries with the secondary index, the
operation becomes a bottleneck. So I'm trying:

S = fun() ->
    MatchHead = #my_table{pid = Pid, _ = '_'},
    Guards = [],
    Result = '$_',
    mnesia:select(my_table, [{MatchHead, Guards, [Result]}], 1, read)
end,
case mnesia:activity(sync_dirty, S) of
    {[Entry], _} -> Entry;
    _ -> undefined
end.

This works, however this operation does not seem to be using the secondary
index as it becomes extremely slow. I also tried:

S = fun() ->
    MatchHead = #my_table{pid = '$1', _ = '_'},
    Guard = {'=:=', '$1', Pid},
    Result = '$_',
    mnesia:select(my_table, [{MatchHead, [Guard], [Result]}], 1, read)
end,
case mnesia:activity(sync_dirty, S) of
    '$end_of_table' -> undefined;
    {[Entry], _} -> Entry
end.

And this is also slow. What can I do to dirty_select a limited number of
records from a mnesia table?

Thank you,
r.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20191126/30e79cb8/attachment.htm>


More information about the erlang-questions mailing list