Database / Mnesia basics Interview questions
When should you choose bag over set for a Mnesia table?
Choose bag when a single key naturally has multiple associated values that all need to coexist
as separate records — a classic one-to-many relationship you'd otherwise model with a separate join
table in a relational database.
mnesia:create_table(item_tag, [{attributes, [item_id, tag]}, {type, bag}]), mnesia:write({item_tag, 101, "sale"}), mnesia:write({item_tag, 101, "clearance"}). %% both coexist under item_id 101
set remains the right default whenever each key genuinely identifies exactly one record —
most tables fall into this category. Reach for bag specifically when you catch yourself needing
to store several distinct values under what is conceptually "the same key," rather than forcing that into a
single record with a list-valued field, which would make querying and indexing individual associated values
harder.
More Related questions...