这取决于您的查询是什么样的。如果您需要按许多不同的列进行排序,那么我会考虑使用 SQL 而不是 Mnesia。
但是,如果您只需要您所描述的查询类型,那么您应该能够使用ordered_set http://www.erlang.org/doc/man/mnesia.html#create_table-2处理排序的表类型和mnesia:select/4 http://www.erlang.org/doc/man/mnesia.html#select-4处理分页和约束。
以下是一些未经测试的代码,可以为您提供要点:
% time goes first because it's our primary sort key
-record(statuses, {time, id, status}).
...
create_table() ->
mnesia:create_table(statuses, [
{attributes, record_info(fields, statuses)}
,{type, ordered_set}
]).
-spec fetch_paged(integer()) -> {[tuple()], continuation()}|'$end_of_table'.
fetch_paged(PageSize) ->
MatchSpec = {#statuses{id = '$1', status = <<"finished">>, _ = '_'}, [], ['$1']},
mnesia:select(statuses, [MatchSpec], PageSize, read).
-spec next_page(continuation()) -> {[tuple()], continuation()}|'$end_of_table'.
next_page(Cont) ->
mnesia:select(Cont).
基本上,mnesia:select/4
为您提供一页结果以及下一页结果的延续。没有像 SQL 数据库那样跳转到第 1000 个结果的内置方法,因此如果您需要这种能力,您可以自己构建它(保留时间索引,以便您可以快速查找表中的第 1000 个结果)是{{2015,4,12},{23,53,8}}
然后在跑步时用它作为后卫select
.