好问题 !现在,我想到了两个办法。我们使用的地方之一qlc
另一个我们使用的地方mnesia's own table iteration methods with accumulators
。这是第一个选项:
%% 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).
我想根据表的大小、应用程序和用户偏好,每个功能都会产生影响。不过,请尝试一下这两种方法,看看哪一种能很好地融入您的应用程序。好处是这完全是一个读取工作,没有写入,所以我希望足够高效。成功 !