erlang - 我如何可以匹配元组内容与qlc和mnesia? [英] erlang - how can I match tuple contents with qlc and mnesia?
问题描述
-record(peer,{
peer_key,%% key是元组{FileId,PeerId}
last_seen,
last_event,
uploaded = 0,
已下载= 0,
left = 0,
ip_port ,
key
})。
Peer_key是一个元组{FileId,ClientId},现在我需要从所有对等端提取ip_port字段具有特定的FileId。
我想出了一个可行的解决方案,但我不知道这是否是一个好办法:
qlc:q([IpPort || #peer {peer_key = {FileId,_},ip_port = IpPort}< - mnesia:table(peer),FileId =:= RequiredFileId]
谢谢。
使用带有诸如{FileId,PeerId}的元组主键的ordered_set表类型,然后部分地绑定元组的前缀,如{RequiredFileId,_}将非常有效,因为只有具有该前缀的键范围将被检查,而不是全表扫描。您可以使用qlc:info / 1来检查查询计划,并确保发生的任何选择都绑定了密钥前缀。
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.
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.
这篇关于erlang - 我如何可以匹配元组内容与qlc和mnesia?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!