Mnesia sorting/limiting

Barry Mitchelson barry.mitchelson@REDACTED
Tue Jun 9 22:16:52 CEST 2009


Hi,

I'm relatively new to Erlang, and as a mini project to work on as I
learn, I'm writing a simple message queue. I'm having some problems
retreiving data from the mnesia table I am using to store my messages.

My job records are defined as :

-record(job, { id, body, due = 0, priority = 1 }).

and after much playing around, the code to retreive the job with the
highest priority I have is

Q1 = qlc:q([ X || X <- mnesia:table(job)]),
Q2 = qlc:sort(Q1, {order, fun(Job1, Job2) -> Job1#job.priority >
Job2#job.priority end}),
C = qlc:cursor(Q2),
R = qlc:next_answers(C,1),

I know this is not the most efficient way, but I'm struggling to find
a fast way to do this, and this is without also sorting by the due
date which I also want to be able to do. In SQL, the query would be
something like

SELECT * FROM jobs ORDER BY priority, due_at LIMIT 1

which with the right indexing would return a result very quickly even
with a large amount of rows. The code above takes a few seconds with
100,000 records in the table.

I feel like I'm missing something somewhere, but am running out of ideas.

Thanks,

Barry


More information about the erlang-questions mailing list