0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · Lesson

Mnesia Indexing & Query Optimization

Speed up Mnesia reads with secondary indexes, QLC queries, and smart table type choices to keep distributed lookups fast.

Mnesia Indexing & Query Optimization is a free Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Cost of Scanning

By default Mnesia can find a record fast only by its primary key. Searching by any other field forces a full table scan — slow on large tables. Indexes and queries fix this.

Secondary Indexes

A secondary index lets Mnesia look up records by a non-key field in roughly constant time, at the cost of extra storage and slightly slower writes.

Creating an Index

Add an index on a field position with mnesia:add_table_index/2.

mnesia:add_table_index(user, email).

Querying via an Index

Use index_read/3 to fetch all records whose indexed field matches a value.

mnesia:index_read(user, "a@b.com", email).

Pattern Matching with match_object

match_object/1 finds records matching a pattern record where _ means any value.

Pattern = #user{id = '_', name = "Alice", _ = '_'},
mnesia:match_object(Pattern).

Introducing QLC

QLC (Query List Comprehensions) gives a SQL-like syntax over Mnesia tables, with the compiler optimizing index use.

-include_lib("stdlib/include/qlc.hrl").

A QLC Query

Write list-comprehension style queries against a table handle from mnesia:table/1.

Q = qlc:q([U || U <- mnesia:table(user), U#user.age > 18]),
mnesia:transaction(fun() -> qlc:e(Q) end).

Table Type Choices

Mnesia tables can be ram_copies (fast, volatile), disc_copies (RAM + disk), or disc_only_copies (disk only, low memory). Pick by speed vs durability needs.

set, ordered_set, bag

The table type matters too: set (one record per key), ordered_set (sorted keys, enables range queries), and bag (multiple records per key).

Measuring Performance

Use mnesia:table_info(user, size) and timing around queries to confirm an index actually helps before adding it everywhere.

mnesia:table_info(user, size).

Indexes and Writes

Every secondary index must be updated on each write, so adding many indexes slows inserts and updates. Index only the fields you actually query by, and drop unused indexes with del_table_index/2.

mnesia:del_table_index(user, email).

Quick Check

Test your Mnesia optimization knowledge.

Recap

You learned to optimize Mnesia reads:

  • Non-key searches scan the whole table unless indexed
  • Add indexes with add_table_index/2, query with index_read/3
  • QLC gives SQL-like queries that exploit indexes
  • Choose table copy type (ram/disc) and structure (set/ordered_set/bag) by need
  • Always measure before optimizing

Frequently asked questions

Is the “Mnesia Indexing & Query Optimization” lesson free?

Yes — the full text of “Mnesia Indexing & Query Optimization” is free to read here on the web, and the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course, upgrade to CoddyKit PRO.

What will I learn in “Mnesia Indexing & Query Optimization”?

Speed up Mnesia reads with secondary indexes, QLC queries, and smart table type choices to keep distributed lookups fast. You practise Erlang OTP: Distributed & Fault-Tolerant Systems Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

No prior experience is required. Erlang OTP: Distributed & Fault-Tolerant Systems Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Mnesia Indexing & Query Optimization” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson?

Yes. Every Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Mnesia Fundamentals & Schema
  2. Transactions & Data Manipulation
  3. Distributed Mnesia & Replication
  4. Mnesia Indexing & Query Optimization
← Back to Erlang OTP: Distributed & Fault-Tolerant Systems Programming