[erlang-questions] Selecting X number of items from Mnesia

Ulf Wiger ulf@REDACTED
Fri Feb 8 15:21:57 CET 2008


Note also the little performance tip that you should try
to run select /before/ writing anything to the table from
within the transaction. Otherwise, the select() operation
must read the transaction store and traverse its contents,
testing the match spec with test_ms/2, then merging
any hits into the sorted result (if it's an ordered_set table).

BR,
Ulf W

2008/2/8, Dave Bryson <daveb@REDACTED>:
> Great! That's exactly what I needed.  I was just confused a bit by
> the MatchSpec - the erts guide helped.  MatchSpec looks very powerful.
>
> For any other newbies out there trying to do the same thing, here's
> how you can pull the first X number of rows from a table:
>
> % return the first 10 records
> mnesia:select(message,[{'$1',[],['$1']}],10,read)
>
> It's also easy to experiment and test your MatchSpec using:
> ets:test_ms/2
>
> Thanks!
> Dave
>
>
> On Feb 8, 2008, at 4:08 AM, Ulf Wiger (TN/EAB) wrote:
>
> > Hynek Vychodil skrev:
> >> Just from qlc manual:
> >>
> >> To return just a few answers cursors can be used. The following code
> >> returns no more than five answers using an ETS table for storing the
> >> unique answers:
> >>
> >> 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.
> >
> > Note, thought, that this will be much more costly than using
> > select() with a limit on number of matching objects.
> >
> > The cursor support needs to create a temporary store, which in
> > the case of QLC is solved using a separate process. This will
> > involve quite some copying.
> >
> > BR,
> > Ulf W
> >
> >
> >
> >>
> >> On Feb 8, 2008 9:06 AM, Ulf Wiger <ulf@REDACTED
> >> <mailto:ulf@REDACTED>>
> >> wrote:
> >>
> >>     I'd have to check on how to do it with QLC, but using
> >>     mnesia:select(Tab, MatchSpec, NObjs, Lock), you
> >>     can quite easily pull the N first objects from an
> >>     ordered_set table.
> >>
> >>     BR,
> >>     Ulf W
> >>
> >>     2008/2/8, Dave Bryson <daveb@REDACTED
> >> <mailto:daveb@REDACTED>>:
> >>> I apologize in advance for the newbie question!
> >>>
> >>> I'm trying to treat an mnesia table as a FIFO queue. What I'd
> >>> like to
> >>> be able to do is pull X number of items off the head of the list
> >>> returned by a query. There's no conditional values to isolate the
> >>> records - I just want the top X number of records.  Is there an
> >>> efficient way to do this with qlc?
> >>>
> >>> Thanks in advance!
> >>>
> >>> Dave
> >>> _______________________________________________
> >>> erlang-questions mailing list
> >>> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> >>> http://www.erlang.org/mailman/listinfo/erlang-questions
> >>>
> >>     _______________________________________________
> >>     erlang-questions mailing list
> >>     erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> >>     http://www.erlang.org/mailman/listinfo/erlang-questions
> >>
> >>
> >>
> >>
> >> --
> >> --Hynek (Pichi) Vychodil
> >>
> >>
> >> ---------------------------------------------------------------------
> >> ---
> >>
> >> _______________________________________________
> >> erlang-questions mailing list
> >> erlang-questions@REDACTED
> >> http://www.erlang.org/mailman/listinfo/erlang-questions
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list