Database / Mnesia intermediate to advanced Interview questions
How do you integrate Mnesia with QLC for complex queries?
QLC (Query List Comprehension) provides a SQL-like, declarative query syntax over any "queryable" data
source, and Mnesia tables can be exposed to it via mnesia:table/1, letting you write joins,
sorting, and filtering across one or more tables in a single expression.
mnesia:transaction(fun() -> Q = qlc:q([P#person.name || P <- mnesia:table(person), P#person.age > 30]), qlc:e(Q) end).
The real power shows up when joining across multiple tables — something a single
select/2 call on one table can't express directly:
Q = qlc:q([{P#person.name, O#order.total} || P <- mnesia:table(person), O <- mnesia:table(order), P#person.id =:= O#order.person_id]),
QLC queries still need to run inside a transaction (or another activity context) since they ultimately read from Mnesia tables, and QLC handles picking a reasonably efficient evaluation order/plan across the joined sources for you.
More Related questions...