Database / Mnesia basics Interview questions
What is a Mnesia index?
By default, only a table's primary key can be looked up efficiently; searching by any other field means scanning every record. A Mnesia index adds a secondary lookup structure on a non-key attribute so queries on that field can be answered directly, without a full table scan.
mnesia:add_table_index(person, name), mnesia:transaction(fun() -> mnesia:index_read(person, "Ada", name) end).
Once an index exists on the name field, mnesia:index_read/3 can fetch matching
records directly through that index rather than iterating the whole table checking each one's
name field by hand. Indexes add some write overhead (the index structure must be maintained
alongside every insert/update) in exchange for much faster reads on that field.
More Related questions...