[erlang-questions] mnesia bound keys

Mikael Pettersson <>
Wed Nov 1 23:02:04 CET 2017


Consider a key-value store.  You might have records

-record(kv, {key, val}).

which would be stored as 3-tuples

{kv, Key, Val}

in a mnesia table kv.  A call

mnesia:match_object({kv, 42, '_'})

has a pattern where the key position is bound to an actual value (42) but
the value position is unbound ('_' is the wildcard which matches
anything).  This call would then find all records in the kv table having
key 42.  On the other hand, a call

mnesia:match_object({kv, '_', foo})

has a pattern where the key position is unbound but the value position is
bound to the value foo.  This call would then find all records in the kv
table having value foo.

The significance of the key is that it's the _index_ for the table, meaning
that any access to the table that supplies the key can quickly locate where
the relevant records are stored.  Accesses that do not supply the key
generally have to enumerate all records and inspect them to see which ones
match the pattern.  This means that accesses with a key are much quicker
than those without.


On Tue, Oct 31, 2017 at 10:48 PM, Karlo Kuna <> wrote:

> i still don't fully grasp the concept. short example would be
> greatly appreciated.
>
> On Tue, Oct 31, 2017 at 8:15 PM, Mikael Pettersson <>
> wrote:
>
>> Presumably you're referring to mnesia:match_object/1 or /3.  It means
>> that the key position of the pattern is a ground value and not a variable.
>>
>> On Tue, Oct 31, 2017 at 7:24 PM, Karlo Kuna <> wrote:
>>
>>> i was wondering what exactly "key is bound" means in context of mnesia
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> 
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171101/d6561541/attachment.html>


More information about the erlang-questions mailing list