Database / Mnesia intermediate to advanced Interview questions
What is the difference between mnesia:dirty_all_keys/1 and mnesia:all_keys/1?
Both return every primary key in a table, but they differ in the same way other dirty/transactional pairs
do: mnesia:all_keys/1 must run inside a transaction (or another activity context) and takes the
appropriate lock, giving you a result consistent with the rest of that transaction's view of the data.
mnesia:dirty_all_keys/1 reads directly with no transaction or locking.
%% transactional mnesia:transaction(fun() -> mnesia:all_keys(person) end). %% dirty mnesia:dirty_all_keys(person).
The dirty version is faster and simpler to call, but the key list it returns could be stale by the time you act on it if concurrent writers are actively adding/removing records at the same moment — acceptable for a rough count or a best-effort listing, but not for logic that depends on the key list being exactly consistent with other reads happening in the same transaction.
More Related questions...