Reading the first or last N records from a secondary index in mnesia

Sam Bobroff sam@REDACTED
Wed Jan 13 07:25:29 CET 2010

Hi everyone,

I've been trying, unsuccessfully, to work out how to read either the
first, (or last) few keys from an mnesia table's secondary index in a
reasonably efficient manner.

(If I want to read records from a primary index I can make the table
ordered_set and use first(), last() and next() or prev().)

All I can come up with is something like this:

Q = qlc:cursor( qlc:sort( qlc:q( [ R || R <- mnesia:table(blah) ] ),
[{order, fun compare_secondary_index/2}] ) )
qlc:next_answers(QC, N).

But it seems to cause the entire table to be read and sorted before any
records are returned. It doesn't seem to use the secondary index at all.

Have I missed some obvious way to do this?

Is it possible to iterate over the secondary index?


Sam Bobroff | sam@REDACTED | M5 Networks
Why does my email have those funny headers? Because I use PGP to sign
my email (and you should too!): that's how you know it's really from me.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 258 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the erlang-questions mailing list