Match/Select 来自 mnesia 使用前缀

Match/Select from mnesia using prefix

我有一个失忆症 table,其中包含 ID、姓名、城市等字段... 我想 select/match 使用前缀命名,例如,如果有多个名称以 "joe" 开头的记录,我想 select 所有这些都有效而不迭代所有记录。

就像sql查询"select id from mytbl where name like joe%"一样。谢谢

更新:

基于 1000 条记录的基准:

MySql(名称也是主键):450-500 微秒

Mnesia 实际 table(非关键):300-370 微秒

分离 ordered_set mnesia table 以名称为键:60-108 微秒

注意: mnesia tables 只是 ram,即使在为实际 table 索引名称字段后,我也没有注意到任何性能差异。 =11=]

类似于:

mnesia:select(mytbl, ets:fun2ms(fun(#mytbl{name = "joe" ++ _, id = Id}) -> Id end)).

mnesia:select(mytbl, [{#mytbl{name = "joe" ++ '_', id = '', _ = '_'},[],['']}]).

(这是 ets:fun2ms 如何处理 ++match specification. The two variants are equivalent, but it's usually clearer to use ets:fun2ms to generate match specifications. You'll need -include_lib("stdlib/include/ms_transform.hrl"). for this to be accepted by the compiler. Note that if you try this in the shell, only the latter will work, because of an inconsistency。)

请注意,在幕后这将仍然遍历所有记录,除非您的table是ordered_setname类型是主键。


如果您想要获取完整记录,而不仅仅是 id 字段,那就是:

mnesia:select(mytbl, ets:fun2ms(fun(#mytbl{name = "joe" ++ _} = X) -> X end)).

mnesia:select(mytbl, [{#mytbl{name = "joe" ++ '_', _ = '_'},[],['$_']}]).