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

I looked at the Mnesia documentation and 3 popular Erlang books. It seems that you can only create and use primary and secondary indexes on a single column. Or maybe this is exactly what the examples cover? If I create a separate index for each of the columns, will Mnesia use them intelligently together to simulate keyword searches in multiple columns? If performance were much better than just scanning a table?

If indexing multiple columns is not supported by Mnesia, then, given its own dbms, someone modeled this functionality in Erlang.

Second Qestion: what about modeling constraints (referential, validation), triggers, and event-based notifications?

+4
source share
2 answers

One way is to save {X, Y} directly in the "column" with the key. This allows you to quickly search for queries in the {_, _} structure, but it also means slower searches when you know only one of the values ​​in a tuple (by default, tables are hash tables).

As is the case with your other DBMS: Mnesia was not created to replace traditional databases, because it was built to meet the needs of Ericsson developers when writing their applications. That way, you might be better off storing data in a traditional database, if that's what you are aiming for.

You might be able to add functionality with code in mnesia - if that is what you want.

+5
source

Mnesia has event notifications. It is possible to have a process (gen_server) that subscribes to mnesia events. These events are classified: events in the table, system events, and others. Read the mnesia documentation for events. In fact, this is possible for the event reporting process using the mnesia event handler by calling: mnesia:report_event(Event) . The entire process subscribed to mnesia events will receive this message. Mnesia will report in real time information about all transactions in the table for signed processes. Transactions can be read, written, or deleted, and the process in its cycle may coincide with the type of event of interest. There are detailed and simple table events. I personally found the events very helpful. You should be able to get information from the documentation.

About the events. Mnesia tables now store records of the same type. You can access this information by calling mnesia:table_info(Table_name::atom(),attributes) . When applying indexes to the mnesia table, it will accept any field from these attributes if it is not the first field in the record (usually called the "primary key"). These indexes are better used when creating a table 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 understand your question well, now I can say that the table employee has columns: age, gender, username, other name and indexed job and all mnesia APIs to search for records based on indexed wiil work attributes, for example. mnesia:index_read/3 or mnesia:index_match_object/2 or mnesia:index_match_object/4 . good luck

/ joshmuza@gmail.com

+1
source

Source: https://habr.com/ru/post/1333726/


All Articles