mnesia secondary index

Evans, Matthew <>
Wed Jun 23 01:18:02 CEST 2010


Hi,

I have a fragmented mnesia table that I need to add a second index to.

It appears that to get this working I need to call mnesia:add_table_index/2 for each fragment.

i.e.

mnesia:add_table_index(content_table,file_id).
mnesia:add_table_index(content_table_frag2,file_id).
mnesia:add_table_index(content_table_frag3,file_id).
....
mnesia:add_table_index(content_table_fragN,file_id).

Likewise, to do an index read of the fragmented table I need to search each fragment in turn:

mnesia:activity(sync_dirty,fun mnesia:dirty_index_read/3,[content_table,{1,1,1},file_id],mnesia_frag).
mnesia:activity(sync_dirty,fun mnesia:dirty_index_read/3,[content_table_frag2,{1,1,1},file_id],mnesia_frag).
....
until match found, or
....
mnesia:activity(sync_dirty,fun mnesia:dirty_index_read/3,[content_table_fragN,{1,1,1},file_id],mnesia_frag).

I guess searching through multiple fragments is quicker than a brute-force mnesia:match_object/1 or mnesia:select/4, but I was wondering if there is a better way to do this?

I could, of course, have my own secondary index table (in the example above the key would be file_id and the data would be the primary key I ultimately want). But I now need to ensure the two tables remain in sync.

Is there another way anyone is aware of?

Thanks

Matt



More information about the erlang-questions mailing list