[erlang-questions] Question: Limiting size of result set in mnesia

6 views
Skip to first unread message

Ahmed Ali

unread,
Apr 28, 2008, 8:00:34 AM4/28/08
to Erlang-Questions (E-mail)
Hi All,

Does anyone have an idea of how to limit size of result set in mnesia
similar to the effect of LIMIT clause in the SQL query below in MySQL?

SELECT * FROM Topic LIMIT 5;

Best regards,

Ahmed Al-Issaei

Hynek Vychodil

unread,
Apr 28, 2008, 8:23:11 AM4/28/08
to Ahmed Ali, Erlang-Questions (E-mail)
mnesia:select/4

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions



--
--Hynek (Pichi) Vychodil

Kenneth Lundin

unread,
Apr 28, 2008, 8:32:31 AM4/28/08
to Ahmed Ali, Erlang-Questions (E-mail)
Hi,

You can use qlc
http://www.erlang.org/doc/man/qlc.html
or you can use
mnesia:select/4 in combination with mnesia:select/1
http://www.erlang.org/doc/man/mnesia.html#select-4

/Kenneth Erlang/OTP, Ericsson

Ahmed Ali

unread,
Apr 28, 2008, 8:42:37 AM4/28/08
to Kenneth Lundin, Erlang-Questions (E-mail)
Thanks Hynek and Kenneth for the quick response.

Just to confirm, for qlc, I should write the query first and then run
qlc:info/2 on it in order to do the limiting, as the example below
shows. Is this it?

e.g.:
Q = qlc:q(...),
qlc:info(Q, [{n_elements, 5}]

Best regards,

Ahmed Al-Issaei

Hynek Vychodil

unread,
Apr 28, 2008, 9:26:54 AM4/28/08
to Ahmed Ali, Erlang-Questions (E-mail)
No, qlc:info just returns info. You should use cursor and next_answers.

Just from documentation:
C = qlc:cursor(qlc:q([X || X <- qlc:append(QH1, QH2)],{unique,true})),
R = qlc:next_answers(C, 5),
ok = qlc:delete_cursor(C),
R.
--
--Hynek (Pichi) Vychodil

Ahmed Ali

unread,
Apr 28, 2008, 9:51:15 AM4/28/08
to Hynek Vychodil, Erlang-Questions (E-mail)
Hi Hynek,

Thanks for the clarification. However, I still don't understand what's
the point of n_elements in qlc:info(Q,[{n_elements,N}]). In QLC
documentation, it names 2 kinds of options, "Option = EvalOption |
ReturnOption" which suggests that qlc:info/2 can be used to set
evaluation time options.

Also, I'm assuming I can remove {unique, true} option from
qlc:append/2 and the result is still the same.

Best regards,

Ahmed Al-Issaei

Jani Launonen

unread,
Apr 28, 2008, 8:35:08 AM4/28/08
to Ahmed Ali, Erlang-Questions (E-mail)
----- Alkuperäinen viesti -----
Lähettäjä: Ahmed Ali <ahmed....@gmail.com>
Päiväys: maanantai, huhtikuu 28, 2008 3:21 pm
Aihe: [erlang-questions] Question: Limiting size of result set in mnesia
Vastaanottaja: "Erlang-Questions (E-mail)" <erlang-q...@erlang.org>

>
> Hi All,
>
> Does anyone have an idea of how to limit size of result set in mnesia
> similar to the effect of LIMIT clause in the SQL query below in MySQL?
>
> SELECT * FROM Topic LIMIT 5;

>From Mnesia Reference Manual (http://www.erlang.org/doc/man/mnesia.html) :

"select(Tab, MatchSpec, NObjects, Lock) -> transaction abort | {[Object],Cont} | '$end_of_table'

Matches the objects in the table Tab using a match_spec as described in ERTS users guide, and returns a chunk of terms and a continuation, the wanted number of returned terms is specified by the NObjects argument. The lock argument can be read or write. The continuation should be used as argument to mnesia:select/1, if more or all answers are needed.

Note: for best performance select should be used before any modifying operations are done on that table in the same transaction, i.e. don't use mnesia:write or mnesia:delete before a mnesia:select. For efficiency the NObjects is a recommendation only and the result may contain anything from an empty list to all available results.

select(Cont) -> transaction abort | {[Object],Cont} | '$end_of_table'

Selects more objects with the match specification initiated by mnesia:select/4.

Note: Any modifying operations, i.e. mnesia:write or mnesia:delete, that are done between the mnesia:select/4 and mnesia:select/1 calls will not be visible in the result.
"

Cheers,
Jani Launonen

Ulf Wiger

unread,
Apr 28, 2008, 2:38:02 PM4/28/08
to Ahmed Ali, Erlang-Questions (E-mail)
When selecting over a very large set, you may want QLC to retrieve
the data in chunks. Then, n_elements can be used to instruct the
table generators to return only a number of elements at a time.
This is something that might specify when implementing a QLC
table generator, I think.

This can have a beneficial effect on garbage collection, as the
process may be able to perform the work with a fairly small
heap. In simple queries (no joins or complex filter expressions),
it's not likely to help much, but it can improve memory
characteristics in some cases.

BR,
Ulf W

2008/4/28 Ahmed Ali <ahmed....@gmail.com>:

Ahmed Ali

unread,
Apr 29, 2008, 2:19:48 AM4/29/08
to Ulf Wiger, Erlang-Questions (E-mail)
Thanks All. Now it is more clear to me.
Reply all
Reply to author
Forward
0 new messages