Erlang - how can I combine the contents of a tuple with qlc and mnesia?

I have a mnesia table for this entry.

-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 got 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.

+4
erlang mnesia
source share
2 answers

Using the order_set table type with the primary key of the tuple, such as {FileId, PeerId}, and then partially binding the tuple prefix of the type {RequiredFileId, _} will be very effective, since only the key range with this prefix will be checked, and not a full table scan. You can use qlc: info / 1 to check the query plan and ensure that all selected events bind a key prefix.

+3
source share

Your query time will grow linearly with the size of the table, since it requires scanning across all rows. Therefore, compare it with realistic tabular data to make sure that it is really working.

If you need to speed it up, you should focus on quickly finding all the peers that carry the file identifier. This can be done with a bag type table with [fileid, peerid] as attributes. Given the file id, you get all peer IDs. With this, you could create your peer table keys for your search.

Of course, you will also need to maintain a bag type table within each transaction, which changes the peer table.

Another option is to repeat fileid and add the mnesia index to this column. I'm just not getting into my own secondary indexes.

0
source share

All Articles