erlang - how can I match tuple contents with qlc and mnesia?

erlang, mnesia

Solution

Using on ordered_set table type with a tuple primary key like { FileId, PeerId } and then partially binding a prefix of the tuple like { RequiredFileId, _ } will be very efficient as only the range of keys with that prefix will be examined, not a full table scan. You can use qlc:info/1 to examine the query plan and ensure that any selects that are occurring are binding the key prefix.

Problem

I have a mnesia table for this record. ``` -record(peer, { peer_key, %% key is the tuple {FileId, PeerId} last_seen, last_event, uploaded = 0, downloaded = 0, left = 0, ip_port, key }). ``` Peer_key is a tuple {FileId, ClientId}, now I need to extract the ip_port field from all peers that have a specific FileId. I came up with a workable solution, but I'm not sure if this is a good approach: ``` qlc:q([IpPort || #peer{peer_key={FileId,_}, ip_port=IpPort} <- mnesia:table(peer), FileId=:=RequiredFileId]) ``` Thanks.

Original source