Indicizzazione di Mnesia e ottimizzazione delle query
Velocizzi le letture da Mnesia con indici secondari, query QLC e scelte appropriate del tipo di tabella, mantenendo rapide le ricerche distribuite.
Indicizzazione di Mnesia e ottimizzazione delle query è una lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Erlang OTP: Distributed & Fault-Tolerant Systems Programming, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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 withindex_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
Impara Erlang con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Indicizzazione di Mnesia e ottimizzazione delle query» è gratuita?
Sì — il testo completo di «Indicizzazione di Mnesia e ottimizzazione delle query» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming, passa a CoddyKit PRO. Il corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming include 4 lezioni in totale.
Cosa imparerò in «Indicizzazione di Mnesia e ottimizzazione delle query»?
Velocizzi le letture da Mnesia con indici secondari, query QLC e scelte appropriate del tipo di tabella, mantenendo rapide le ricerche distribuite. Eserciti Erlang OTP: Distributed & Fault-Tolerant Systems Programming con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Non è richiesta alcuna esperienza precedente. Erlang OTP: Distributed & Fault-Tolerant Systems Programming su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Indicizzazione di Mnesia e ottimizzazione delle query»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Sì. Ogni lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Fondamenti e schema di Mnesia
- Transazioni e manipolazione dei dati
- Mnesia distribuita e replica
- Indicizzazione di Mnesia e ottimizzazione delle query