映射减少索引排序



我们将messageinfo存储在RavenDB中。该类的简化版本如下所示

public class MessageInfo
{
    public string Id { get; set; }
    public string ChannelId { get; set; }
    public Message Message { get; set; }    
}

现在,我们需要通过通道id

获得消息概述
public class MessageOverview
{
    public string ChannelId { get; set; }
    public int Count { get; set; }
    public Message Message { get; set; }
}
并为 创建map reduce索引
    public MessageOverviewIndex()
    {
        this.Map = messages => from m in messages select new { m.ChannelId, Count = 1, m.Message };
        this.Reduce = results => from r in results
                                 group r by r.ChannelId
                                     into g
                                     select new MessageOverview
                                     {
                                         ChannelId = g.Key,
                                         Count = g.Sum(x => x.Count),
                                         Message = g.OrderByDescending(m => m.Message.ServerTime).First().Message,
                                     };
    }

在概述中返回最新消息的sort子句对性能的影响如何?它是更好地在服务器时间到MessageInfo和/或MessageOverview还是不相关?

还有其他更好的方法吗?

这是当前的实现:

  this.Map = messages => from m in messages
                               select new
                                    {
                                        m.Message.ChannelId,
                                        Count = 1,
                                        m.Message.ServerTime,
                                        MessageId = m.Id
                                    };
  this.Reduce = results => from r in results
                                 group r by r.ChannelId
                                     into g
                                     let t = g.OrderByDescending(x => x.ServerTime).First()
                                     select new MessageOverview
                                     {
                                         ChannelId = g.Key.ChannelId,
                                         Count = g.Sum(x => x.Count),
                                         MessageId = t.MessageId,
                                         ServerTime = t.ServerTime
                                     };

排序时间不是问题。但是,请注意,您正在输出消息,如果消息很大,则会扩展索引。注意,我们需要跟踪索引中的所有消息,以便计算最新的消息。跟踪频道可能会更容易。在一个单独的查询中计数和加载每个通道的最新消息。

最好放在索引中,但您也可以这样做。

results => from r in results
    orderby r.Message.ServerTime descending
    group r by r.ChannelId
    into g
    select new MessageOverview
        {
            ChannelId = g.Key,
            Count = g.Sum(x => x.Count),
            Message = g.First().Message,
        };

相关内容

  • 没有找到相关文章

最新更新