Prev Next

Database / Mnesia intermediate to advanced Interview questions

Why would you use qlc:q with mnesia:table/1 instead of mnesia:select/2?

select/2 is a single-table, match-specification-based query — excellent for filtering one table efficiently, but it can't express a relationship spanning multiple tables in one query. QLC's comprehension syntax reads much closer to how you'd describe the query in plain language (join these two tables, filter, sort), and it can combine several mnesia:table/1 sources in one expression.

%% Multi-table join with QLC
Q = qlc:q([{P#person.name, T#tag.tag} ||
    P <- mnesia:table(person),
    T <- mnesia:table(item_tag),
    P#person.id =:= T#item_tag.item_id]),
qlc:e(Q).

The tradeoff is that QLC's flexibility comes with more overhead than a tightly targeted select/2 call on a single table — for a simple single-table filter, plain select/2 (or even match_object/1) is usually the leaner, faster choice; QLC earns its keep specifically once the query genuinely needs to correlate data across more than one table.

What can QLC do that select/2 alone cannot?
When is plain select/2 usually the better choice over QLC?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!
Acorns Logo

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Robinhood Logo

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull Logo

Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is the difference between mnesia:transaction/1 and mnesia:sync_transaction/1? What is the difference between async_dirty and sync_dirty activity contexts? How does mnesia:activity/4 let you customize the access context of an operation? What is table fragmentation in Mnesia and why would you use it? How do you configure a fragmented Mnesia table? Why does fragmentation help Mnesia scale beyond what one table replica set can handle? What is the difference between mnesia:match_object/1 and mnesia:select/2? What is mnesia:all_keys/1 used for? How do you integrate Mnesia with QLC for complex queries? Why would you use qlc:q with mnesia:table/1 instead of mnesia:select/2? What is a sticky lock in Mnesia and why does it improve performance? When can sticky locks cause a problem in a distributed Mnesia cluster? How do you dynamically add a new node to a running Mnesia cluster? What is mnesia:change_config(extra_db_nodes, Nodes) used for? How do you remove a table replica from a node without dropping the whole table? What is mnesia:del_table_copy/2 used for? How do you move a table copy from one node to another while the system stays live? What is mnesia:move_table_copy/3 used for? What is the {majority, true} table option and what tradeoff does it introduce? Why would you enable the majority option on a table prone to network partitions? What is a local_content table in Mnesia? When would you use a local_content table instead of a normal replicated table? What is mnesia:set_master_nodes/2 used for? How does setting master nodes influence conflict resolution after a network partition? What is mnesia_tm and what role does it play internally? How does Mnesia's transaction manager serialize concurrent transactions touching the same records? Why can a "hot" record or table become a bottleneck under heavy Mnesia write load? How can you redesign a schema to reduce lock contention on a frequently-updated record? What is mnesia:subscribe/1 used for? How do you react to table events using Mnesia's subscription mechanism? What is a nested Mnesia transaction and how does it behave differently from a top-level one? Why should nested transactions generally be avoided or used carefully in Mnesia? What is the dc_dump_limit configuration parameter used for? Why does tuning the transaction log dump frequency matter for write-heavy Mnesia workloads? How do you merge two previously-separate Mnesia clusters into one? What complications arise when merging schemas from two independently-created Mnesia databases? How does Mnesia decide which node's data wins during schema merge conflicts? What is the difference between a global lock and a per-record lock in Mnesia transactions? When would you use mnesia:read_lock_table/1 instead of relying on per-record locks? How do you profile and optimize Mnesia transaction throughput in a write-heavy system? Why is batching multiple related writes into a single transaction usually better than many small transactions? How does Mnesia's schema interact with OTP release upgrades (appup/relup)? What is the difference between mnesia:dirty_all_keys/1 and mnesia:all_keys/1? Explain the internal working of how Mnesia chooses which replica to read from in a load-balanced cluster? What is the where_to_read table_info item used for? What is mnesia:dump_log/0 used for? How do you handle a transaction that needs to retry after being aborted due to a deadlock? What is the difference between mnesia:transaction/1's built-in behavior and manual retry logic you might add?
Show more question and Answers...

Integration

Comments & Discussions