我们在RavenDB中存储MessageInfos。 该类的简化版本如下所示
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; }
}
并为该
创建地图缩小索引 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子句对性能的影响如何在概述中返回最新消息。 在ServerTime到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
};
答案 0 :(得分:2)
排序时间不是问题。但是,请注意您正在输出消息,如果它很大,它将扩展索引。请注意,我们需要跟踪索引中的所有消息,以便我们计算最新消息。 跟踪频道可能更容易。计数,并在单独的查询中加载每个频道的最新消息。
答案 1 :(得分:1)
最好在索引中做到这一点,但你可以这样做。
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,
};