<div dir="ltr">Hi,<br>I use mnesia for a little project and I observed a strange (but maybe right) behavior from the function mnesia:write/1 when an extra index has alway the same value.<br>In this case the complexity of the insertion appears to be in O(n) where n is the number of tuple in the table.<br>
<br>My record :<br>
-record(minichat,<br>
{<br>
id,<br>
auteur_id, %indexed<br>
date,<br>
pseudo,<br>
contenu,<br>
troll_id = undefined, %indexed<br>
racine_id = undefined<br>
}).<br><br>So, I made a little benchmark to see what happen in three cases :<br>1) '#minichat.troll_id' is undefined for all tuple<br>2) same as 1) but without the index on 'troll_id'<br>3) 'troll_id' is indexed but some random values are put in it<br>
<br>You can see the results here :<br>1) O(n) : <a href="http://www.gburri.org/mnesia_write/results_100_000.png">http://www.gburri.org/mnesia_write/results_100_000.png</a><br>2) O(1) : <a href="http://www.gburri.org/mnesia_write/results_100_000_without_index_on_troll_id.png">http://www.gburri.org/mnesia_write/results_100_000_without_index_on_troll_id.png</a><br>
3) O(1) : <a href="http://www.gburri.org/mnesia_write/results_100_000_with_random_data_into_troll_id.png">http://www.gburri.org/mnesia_write/results_100_000_with_random_data_into_troll_id.png</a><br><br>Is this behavior normal ? Is this the result of the collisions of keys ?<br>
<br><br>Here is some additional informations about my tests :<br> <br>The creation of the table (called at the beginning of all test) :<br>[..]<br> mnesia:create_table(minichat, [<br> {attributes, record_info(fields, minichat)},<br>
{disc_copies, [node()]}<br> ]),<br> mnesia:add_table_index(minichat, auteur_id),<br> mnesia:add_table_index(minichat, troll_id). % commented for the second case test<br><br>The benchmark code :<br>[..]<br>bench_write_minichat(Filename) -><br>
Times = bench_write_minichat(1, []),<br> {ok, File} = file:open(Filename, [write]),<br> lists:foreach(<br> fun({Id, Time}) -><br> io:format(File, "~w ~w~n", [Id, Time])<br> end,<br> Times<br>
),<br> file:close(File). <br>bench_write_minichat(100000, Temps) -> Temps;<br>bench_write_minichat(Id, Temps) -><br> {T, _} = timer:tc(mnesia, transaction, [fun() -><br> mnesia:write(#minichat{<br> id = Id,<br>
auteur_id = random:uniform(1000),<br> date = now(),<br> pseudo = "Test",<br> contenu = "Blabla blabla bla.",<br> racine_id = random:uniform(1000)<br> % ,troll_id = random:uniform(1000) % uncommented for the third case test<br>
})<br> end]),<br> bench_write_minichat(Id + 1, if Id rem 500 =:= 0 -> [{Id, T} | Temps]; true -> Temps end).<br><br><br>/Greg Burri<br></div>