[Ericsson AB]





A query language support for the DBMS Mnesia


Queries are used for accessing the data in a Database Management System. The query specifies a relation (possibly complicated) to all of the selected data. This could involve several tables as well as conditions such as '<' (less than), function calls and similar.

Mnesia has two query interfaces which are used together:

The exact syntax of query list comprehensions are described in a separate section of this document.

The query list comprehensions only define the query and the syntax of the solutions to be returned. The actual evaluation is determined by calling different functions with a handle obtained by the list comprehension. For example:

      -record(person, {name,age}).
      Handle = 
               [ P.name || P <- table(person) ]
      L = mnesia:transaction(
           fun() ->

The example above matches a list of all names in the table "person" with the variable L. Note the following points:

After obtaining a handle from a query list comprehension, the query can be evaluated in three different ways:

Let us reconsider the previous example, this time with cursors. In the following example, we will get just five names without evaluating all of the answers:

      -record(person, {name,age}).
      Handle =
               [ P.name || P <- table(person) ]
      L = mnesia:transaction(
           fun() ->
               Cursor = mnemosyne:cursor(Handle),
               As = mnemosyne:next_answers(Cursor, 5, 5),

The third way of evaluating a query is by a further division of the query process. The cursor/1 function is now split into two. The reason for this is that we can set up the query when there is plenty of time and initialize it when answers are needed quickly. As in the previous example, we will get just five names:

      -record(person, {name,age}).
      Handle = 
               [ P.name || P <- table(person) ]
      QuerySetup = mnemosyne:setup_query(Handle),
      L = mnesia:transaction(
           fun() ->
               Cursor = mnemosyne:init_query(QuerySetup),
               mnemosyne:next_answers(Cursor, 5, 5)
      % Here we may call more init_query-next_answers constructions
      % with the same Handle


all_answers(Cursor) -> Answer

Returns all remaining answers from the query identified by Cursor. It can be applied after next_answers to obtain all answers that are left.

Note: This must be evaluated inside a transaction.

cursor(Handle) -> Cursor
cursor(Handle,Nprefetch) -> Cursor

Sets up a query for evaluation and starts an answer pre-fetch. Nprefetch gives the number of answers to pre-fetch and must be greater than 0. The default value is 1. A pre-fetch is the first part of a query evaluation. It is placed in a separate process which may on some occasions speed up the subsequent collection of answers.

Note: This must be evaluated inside a transaction.


Deletes the Cursor and associated query evaluation.

Note: This must be evaluated inside a transaction.


Deletes a query setup.

eval(Handle) -> Answers

Starts a query evaluation according to the Handle and collects all answers in one operation.

Note: This must be evaluated inside a transaction.

init_query(QuerySetup) -> Cursor
init_query(QuerySetup,Nprefetch) -> Cursor

Performs the last short step in starting a query from QuerySetup. Nprefetch defines the number of answers to pre-fetch as in cursor/2. The default value is 1.

Note: This must be evaluated inside a transaction.

next_answers(Cursor) -> Answers
next_answers(Cursor,Nmin,Nmax) -> Answers

Fetches the next answers from the query evaluation identified by Cursor. At least Nmin and at most Nmax answers are collected. If less than Nmin answers are returned; for example, 0, there are no more answers. If enough answers are not available, but more are expected, the functions wait for them.

Note: This must be evaluated inside a transaction.

reoptimize(Handle) -> Handle

Re-optimizes a query. Queries are always optimized, but the optimization takes into account the dynamic table statistics like size, attribute distribution etc. If a table has changed after obtaining the Handle from a query list comprehension, the query execution plan will no longer be appropriate (although semantically correct). This function will rearrange the execution plan according to the current statistics from the database.

setup_query(Handle) -> QuerySetup

Creates a query setup, that is, performs most of a query evaluation without actually initiating the actual evaluation.

version() -> String

Returns the current module version.

List Comprehension

There must be a directive in the Erlang file telling the compiler how to treat queries. This directive is:


A list comprehension consists of:

                query [ <pattern> || <body> ] end

The <pattern> is a description of the terms that are returned by a query. Details of how to obtain the actual values in the <pattern> is given by the <body>.

The <pattern> is an Erlang term without function calls. It typically has one or more variables from the <body> which are instantiated for each answer produced. Every element in the returned list is composed by instantiating this <pattern> and then adding it to the answers.

The <body> takes a sequence of goals separated by ",". The possible goals are:

A <logical-variable> is written exactly as an Erlang variable. The <table-name>, <table-type>, <rule-name> and <module> are atoms. The <table-name> and <table-type> may be an Erlang variable which must be bound at runtime. The logical variables are local to a list comprehension and shadows any Erlang variables with the same name.

An <expression> is any Erlang expression which may include function calls and <logical-variable>. The variants <erlang-list-expression> is an <expression> which must produce lists where all elements are records of the same type. The <logical-variable> must have the same associated record. The <erlang-test-expression> is an <expression> which only has the values true or false.

Erlang variables are allowed in all variants of <expression> and in <pattern>. They must always be bound in the query list comprehension.

logical variables is local to a query list comprehension and have an associated Erlang record. The associated record can in most cases be inferred by the query compiler. Therefore, the normal notation for the field f1 in variable X is just X.f1. The query compiler notifies when it cannot deduce the corresponding record. The explicit form is X#r.f1 as in ordinary Erlang. If the type of the record is not deducable at Erlang compile time, it is more efficient to use the explicit form as a help to the compiler. A variable receiving values from a table will have the record with the same name as the table.

Erlang variables are allowed in <expression> and in some places as described above. They must always be bound in the query list comprehension.

Errors in the description are reported as exceptions in the Erlang standard format as follows:

      {error, {Line,Module,Msg}}

The descriptive English text is returned by calling



A function used in a query list comprehension must never directly or indirectly:

  1. have side effects
  2. access the database either by a query
    or by Mnesia functions
  3. spawn processes
  4. send or receive messages

Rules (Views)

A rule (or view) is a declaration of how to combine data from sources as a kind of "subroutine". Assume that we have the following query list comprehension:

          [ Employee || Employee <- table(employee),
                        Employee.department = sales  ]

This retrieves a list of all sales employees. This could be formulated in the following rule:

      sales(E, employee) :-
              E <- table(employee),
              E.salary = sales.

The employee declaration in the head of the rule forces the rule argument to associate the employee record. If we omit the declaration, then the associated record would be the rule name, in this case sales. Note that the syntax used in previous versions of Mnemosyne by using an separate argtype declaration still works, but the above method is prefered.

The sales rule may now be used in a query list comprehension:

          [ SalesPerson || SalesPerson <- rule(sales) ]

The SalesPerson is an employee record because of the declaration of the rule above. Another example lists the names of all female sales people:

          [ SalesPerson.name || SalesPerson <- rule(sales),
                                SalesPerson.sex = female   ]

The rule must have one argument when used. Although the declaration of a rule looks similar to an ordinary function, no function of that name is constructed. Hence the name of the rule can be used for another function. All rules are automatically exported so they could be referred in other modules by the usual notation module:name. After the :-, there is the usual <body> as in the query list comprehension.

Generated Functions

When compiling queries some extra (hidden) functions are automatically generated and exported. Thus, there cannot be other functions with the same name and arity within the module. Three such generated functions exist. They are:


Hans Nilsson - support@erlang.ericsson.se

Copyright © 1991-2007 Ericsson AB