作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两条记录:
-record(foo, {timestamp, name}).
-record(bar, {timestamp, name}).
我想执行一个模拟以下 SQL 查询的 Mnesia 查询
SELECT f.* FROM foo f WHERE f.timestamp NOT IN ( SELECT b.timestamp FROM boo b)
什么是有效的 Mnesia 等价物?
最佳答案
好问题!现在,我想到了两种方法。一个我们使用 qlc
,另一个我们使用 mnesia 自己的带累加器的表迭代方法
。这是第一个选项:
%% Here i use 'qlc', with a guard%% which ensures that the timestamp%% in the given 'foo record'%% does NOT exist in table 'bar'
query()-> Is_not_in_bar = fun(Stamp)-> case mnesia:dirty_read({bar,Stamp}) of [] -> true; _ -> false end end,
Q = qlc:q([R || R <- mnesia:table(foo),
Is_not_in_bar(R#foo.timestamp) == true])), Fun = fun(QH)-> qlc:e(QH) end, mnesia:activity(transaction,Fun,[Q],mnesia_frag).
foo
,同时交叉引用表
bar
中的每个时间戳。如果在
bar
中找不到,则将其添加到累计金额中。看看下面这个
%% Here i iterate table 'foo'%% for each record i find,%% i try to cross reference%% its timestamp in table 'bar'%% If its not found, then i add that%% 'foo record' into the Buffer%% I accumulate this Buffer until%% the entire table 'foo' has been %% traversed我想根据表的大小、应用程序和用户偏好,这些功能中的每一个都会产生影响。但是,请同时尝试这两种方法,看看哪一种能很好地融入您的应用程序。好消息是,这完全是一项读取工作,没有写入,所以我希望效率足够高。成功!
query_vsn2()-> Iterator = fun(#foo{timestamp = T} = Rec,Buffer)-> NewBuffer = case mnesia:dirty_read({bar,T}) of [] -> [Rec|Buffer]; _ -> Buffer end, NewBuffer end, Find = fun(Loop)-> mnesia:foldl(Loop,[],foo) end, mnesia:activity(transaction,Find,[Iterator],mnesia_frag).
关于erlang - Mnesia 相当于 SQL NOT IN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9347753/
我是一名优秀的程序员,十分优秀!