mnesia query

Ulf Wiger <>
Thu Jul 14 17:14:59 CEST 2005

Den 2005-07-14 15:49:53 skrev Sebastian Bello <>:

> Hi all,
> imagine I have a query which returns a very big number of records, but  
> am interested in just a fixed small number of them. Is there a way to  
> retrieve, let's say, just the first N or just N of them? Not doing, ie  
> getting the whole result, implies a performance penalty other than  
> memory consumption?

What type of query is it? Mnemosyne? QLC? or is it just select()?
With select(), you can add an argument, Limit, that makes the function
break out, and return the objects found so far, together with a
continuation. This works for ets and mnesia, even though with mnesia,
I think you also have to specify a bunch of other arguments.

With QLC and Mnemosyne, you can use cursors. Read the appropriate
manual to figure out how they work.

In the Mnemosyne user guide, you can find the following example:

"Use the cursor with a query evaluation to produce a few solutions only.
With a handle we create a cursor by calling mnemosyne:cursor/1. With the
cursor we can repeatedly call mnemosyne:next_answers to get more solutions.
When an empty list is returned there are no more possible solutions. Delete
the cursor with mnemosyne:delete_cursor/1.

     Handle =
            [ S.snb || S <- table(subscriber),
              = none]

     AFewAnswers =
              fun() ->
                      Cursor = mnemosyne:cursor(Handle),
                      % now get at least two but not
                      % more than five solutions:
                      L = mnemosyne:next_answers(Cursor,2,5),


More information about the erlang-questions mailing list