Mnesia Indexing

2009-03-23
Data retrieval and matching can be performed very efficiently if we know the key for the record. Conversely, if the key is not known, all records in a table must be searched. The larger the table the more time consuming it will become. To remedy this problem Mnesia’s indexing capabilities are used to improve data retrieval and matching of records.

The following two functions manipulate indexes on existing tables:
  • mnesia:add table index(Tab, AttributeName) -> {aborted, R} |fatomic, ok}
  • mnesia:del table index(Tab, AttributeName) -> {aborted, R} |fatomic, ok}
These functions create or delete a table index on field defined by AttributeName. To illustrate this, add an index to the table definition (employee, {emp_no, name, salary, sex, phone, room_no}). The function which adds an index on the element salary can be expressed in the following way:

1 > mnesia:add_table_index(employee, salary).

The indexing capabilities of Mnesia are utilized with the following three functions, which retrieve and match records on the basis of index entries in the database.
  • mnesia:index read(Tab, SecondaryKey, AttributeName) -> transaction abort | RecordList. Avoids an exhaustive search of the entire table, by looking up the SecondaryKey in the index to find the primary keys.
  • mnesia:index match object(Pattern, AttributeName) -> transaction abort | RecordList. Avoids an exhaustive search of the entire table, by looking up the secondary key in the index to find the primary keys. The secondary key is found in the AttributeName field of the Pattern. The secondary key must be bound.
  • mnesia:match object(Pattern) -> transaction abort | RecordList. Uses indices to avoid exhaustive search of the entire table. Unlike the other functions above, this function may utilize any index as long as the secondary key is bound.
转自《Mnesia Database Management System

M-OSCAR | Powered by Blogger | Entries (RSS) | Comments (RSS) | Designed by MB Web Design | XML Coded By Cahayabiru.com