Erlang OTP: Distributed & Fault-Tolerant Systems Programming · 课时

Mnesia 索引与查询优化

使用二级索引、QLC 查询和合理的表类型选择加快 Mnesia 读取速度,让分布式查找保持高效。

第 4 / 4 课13 个步骤

Mnesia 索引与查询优化 是 CoddyKit 上的免费 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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
免费开始

用 AI 导师学习 Erlang — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「Mnesia 索引与查询优化」课时是免费的吗?

是的 — 「Mnesia 索引与查询优化」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。

「Mnesia 索引与查询优化」这节课中我会学到什么?

使用二级索引、QLC 查询和合理的表类型选择加快 Mnesia 读取速度,让分布式查找保持高效。 你通过在浏览器中直接运行的动手代码来练习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Mnesia 索引与查询优化」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课中编写并运行代码吗?

能。每节 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Mnesia 基础与架构
  2. 事务与数据操作
  3. 分布式 Mnesia 与复制
  4. Mnesia 索引与查询优化
← 返回 Erlang OTP: Distributed & Fault-Tolerant Systems Programming