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

Indexação do Mnesia e Otimização de Consultas

Acelere as leituras do Mnesia com índices secundários, consultas QLC e escolhas inteligentes do tipo de tabela, mantendo rápidas as pesquisas distribuídas.

Indexação do Mnesia e Otimização de Consultas é uma aula grátis de Erlang OTP: Distributed & Fault-Tolerant Systems Programming no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Erlang OTP: Distributed & Fault-Tolerant Systems Programming, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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

Perguntas Frequentes

A aula “Indexação do Mnesia e Otimização de Consultas” é grátis?

Sim — o texto completo de “Indexação do Mnesia e Otimização de Consultas” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming, atualize para CoddyKit PRO. O curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming inclui 4 aulas no total.

O que vou aprender em “Indexação do Mnesia e Otimização de Consultas”?

Acelere as leituras do Mnesia com índices secundários, consultas QLC e escolhas inteligentes do tipo de tabela, mantendo rápidas as pesquisas distribuídas. Você pratica Erlang OTP: Distributed & Fault-Tolerant Systems Programming com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

Nenhuma experiência prévia é necessária. Erlang OTP: Distributed & Fault-Tolerant Systems Programming no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Indexação do Mnesia e Otimização de Consultas”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

Sim. Cada aula de Erlang OTP: Distributed & Fault-Tolerant Systems Programming inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Fundamentos e esquema do Mnesia
  2. Transações e manipulação de dados
  3. Mnesia distribuído e replicação
  4. Indexação do Mnesia e Otimização de Consultas
← Voltar para Erlang OTP: Distributed & Fault-Tolerant Systems Programming