Erlang OTP: Distributed & Fault-Tolerant Systems Programming · レッスン

Mnesiaのインデックス作成とクエリ最適化

セカンダリインデックス、QLCクエリ、適切なテーブル型の選択によってMnesiaの読み取りを高速化し、分散検索を速く保ちます。

レッスン 4/413 ステップ

「Mnesiaのインデックス作成とクエリ最適化」はCoddyKit上の無料Erlang OTP: Distributed & Fault-Tolerant Systems Programmingレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「Mnesiaのインデックス作成とクエリ最適化」レッスンは無料ですか?

はい。「Mnesiaのインデックス作成とクエリ最適化」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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に戻る