开发者

How do I create and use (or simulate) multi-column indexes in Erlang Mnesia

I have looked through the Mnesia documentation and the 3 popular Erlang books. It seems only single column primary and secondary indexes can be created and used. Or maybe it is just what the examples cover? If I create a separate index on each of the columns will/is Mnesia able to use them intelligently together to simulate multi-column key index searches? If so would the performance be much bet开发者_如何学编程ter than a simple table scan?

If multi-column indexing is not supported by Mnesia, given its a native dbms has anyone simulated this functionality in Erlang.

Second Qestion: How about simulating constraints (referential, check), triggers and event-based notifications?


One way is to store {X, Y} directly in the "column" that has the key. This makes you able to search fast on queries of structure {_, _} but it also means slower searches when you only know one of the values in the tuple (By default, the tables are hash-tables).

As for your other DBMS wants: Mnesia was not really built to replace traditional databases as much as it was built to satisfy the needs of the Ericsson devs when writing their applications. Thus, you may be better off by storing data in a traditional database, if that is what you are aiming for.

You may be able to add the functionality with code around mnesia however - if that is what you want.


Mnesia has event based notifications. Its possible to have a process (gen_server) which subscribes to mnesia events. These events are categorised: Table events, System events and others. Read the mnesia documentation at the part concerning events. Its actually possible for a process to report events using mnesia event handler by calling: mnesia:report_event(Event). All process subscribed to mnesia events will get this message. Mnesia will report real-time information about all transactions on a table to subscribed processes. There can be read, write or delete transactions and a process in its loop can pattern match the type of event it may be interested in. There are detailed and simple table events. I have personally found the events very useful. You should be able to get the details from the documentation.

About events. Now mnesia tables store records of the same type. This info can be accessed by calling mnesia:table_info(Table_name::atom(),attributes). When applying indexes on a mnesia table, it will accept any field from those attributes as long as it is not the first record field( normally called "primary key").These indexes are better if applied at table creation than at runtime due to a number of reasons. Consider the code snippet below

-record(employee,{id,first_name,other_name,sex,age,job}).

install(Nodes)->
  mnesia:create_schema(Nodes),
  mnesia:start(),
  mnesia:create_table(employee,[{index,[age,sex,first_name,job]},
{attributes,record_info(fields,employee)}]), mnesia:stop(), ok.

if i have understood your question well, i can now say that the table employee has columns: age,sex,first_name,other_name, and job indexed and all mnesia APIs for record searching based on indexed attributes wiil work e.g. mnesia:index_read/3 or mnesia:index_match_object/2 or mnesia:index_match_object/4. good luck

/joshmuza@gmail.com

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜