[erlang-questions] Mnesia fastest select on secondary index

Ulf Wiger ulf@REDACTED
Tue Feb 10 13:56:09 CET 2015


> On 10 Feb 2015, at 13:12, Roberto Ostinelli <roberto@REDACTED> wrote:
> 
> What is the recommended way (and fastest, dirty are desired, inconsistency is ok in the system) to retrieve the record with secondary = <<"a">>?

That would be mnesia:dirty_index_read(Tab, Value, IndexPos).

Eshell V5.10.3  (abort with ^G)
1> rd(my_records,{primary,secondary}).
my_records
2> #my_records{}.
#my_records{primary = undefined,secondary = undefined}
3> mnesia:start().
ok
4> mnesia:create_table(my_records,[{type,set},{ram_copies,[node()]},{attributes,record_info(fields,my_records)},{index,[#my_records.secondary]}]).
{atomic,ok}
5> mnesia:dirty_write(#my_records{primary = <<"1">>, secondary = <<"a">>}).
ok
6> mnesia:dirty_read({my_records,<<"1">>}).
[#my_records{primary = <<"1">>,secondary = <<"a">>}]
7> mnesia:dirty_index_read(my_records,<<"a">>,#my_records.secondary).
[#my_records{primary = <<"1">>,secondary = <<"a">>}]
8> timer:tc(mnesia,dirty_index_read,[my_records,<<"a">>,#my_records.secondary]).
{304,[#my_records{primary = <<"1">>,secondary = <<"a">>}]}
9> timer:tc(mnesia,dirty_index_read,[my_records,<<"a">>,#my_records.secondary]).
{28,[#my_records{primary = <<"1">>,secondary = <<"a">>}]}
10> timer:tc(mnesia,dirty_index_read,[my_records,<<"a">>,#my_records.secondary]).
{37,[#my_records{primary = <<"1">>,secondary = <<"a">>}]}


BR,
Ulf W

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150210/7bed4d0c/attachment.htm>


More information about the erlang-questions mailing list