分页搜索Erlang Mnesia [英] Pagination search in Erlang Mnesia

查看:217
本文介绍了分页搜索Erlang Mnesia的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,给定记录

-record(item, {
  id,
  time,
  status}).

我想搜索1000到1100个项目,按时间排序,状态=:=<完成>>

I want to search the 1000 to 1100 items, ordered by time and status =:= <<"finished">>

任何建议?

推荐答案

这取决于你的查询的样子。如果您需要订购许多不同的列,那么我会考虑使用SQL而不是Mnesia。

It depends on what your queries look like. If you need to order by lots of different columns, then I'd consider using SQL instead of Mnesia.

但是,如果您只需要描述的那种查询,应该能够使用 ordered_set 类型的表来处理订单和 mnesia:select / 4 来处理分页和约束。

But if you only need the kind of query you described, you should be able to use the ordered_set type of table to handle the ordering and mnesia:select/4 to handle pagination and the constraint.

这是一些未经测试的代码,要点:

Here's some untested code to give you the gist:

% 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 给出您的结果页面和下一页结果的延续。没有一种内置的方式来跳转到第1000个结果,就像在SQL数据库中一样,所以如果你需要这个能力你自己构建它(保持时间的索引,所以你可以快速查找在你的表中的第1000次是 {{2015,4,12},{23,53,8}} ,然后在运行时选择

Basically, mnesia:select/4 gives you a page of results and a continuation for the next page of results. There's not a built-in way to jump to the 1000th result like in a SQL DB, so if you needed that ability you'd build it yourself (keep an index of times so you can quickly look up that the 1000th time in your table is {{2015,4,12},{23,53,8}} and then use that as a guard when running select.

这篇关于分页搜索Erlang Mnesia的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆