简体   繁体   English

如何有效地从索引字段上排序的DB4O中检索每个第N个对象

[英]How to efficiently retrieve every Nth object from DB4O ordered on an indexed field

I store a lot of events in a DB4O db. 我在DB4O db中存储了很多事件。 The events are timestamped and I've indexed the field. 这些事件带有时间戳,我已将该字段编入索引。 Retrieving (an enumerator of) all events, ordered by timestamp, takes almost no time at all (as they aren't activated). 检索所有事件(枚举数)(按时间戳排序)几乎根本不需要时间(因为它们没有被激活)。 However, what if I only want to retireve every 10. I can loop, but then I'll have to do a count, which takes a significant time. 但是,如果我只想每隔10退役一次,我可以循环,但随后我必须做一个计数,这需要花费大量时间。 Even returning FirstOrDefault() from the Enumerable (yes, I'm in C# and LINQ) takes time. 即使从Enumerable返回FirstOrDefault()(是的,我在C#和LINQ中)也需要时间。 If the field is indexed, shouldn't it be pretty easy to find the first one in an ordered set? 如果该字段已建立索引,在有序集中查找第一个字段不是很容易吗?

Yes, I don't have a lot of experience using DB4O, but the app I've written works well. 是的,我没有使用DB4O的丰富经验,但是我编写的应用程序运行良好。 Normally I push millions of events to the db and then retrieve a few based on a short timespan. 通常,我将数百万个事件推送到数据库,然后根据较短的时间跨度检索一些事件。 No problem at all and the app performs very well. 完全没有问题,该应用程序运行良好。 Response time for a typical query is less than 100 ms. 典型查询的响应时间少于100毫秒。

Now I want to retrieve a sample of all the events. 现在,我想检索所有事件的样本。 Every ten, or so ... How do I accomplish that? 每十个左右...我该如何实现?

You are using LINQ right know? 您正在使用LINQ对吗? Are you sure that the query takes no time? 您确定查询不会花费时间吗? Because LINQ queries are executed when you start iteration over it. 因为LINQ查询是在您开始对其进行迭代时执行的。 Not before. 没过

Also note that db4o doesn't use the index for sorting right now. 还要注意,db4o现在不使用索引进行排序。 That means for huge result sets sorting will be slow. 这意味着对于庞大的结果集,排序将很慢。

FirstOrDefault() already takes a lot of time? FirstOrDefault()已经花费很多时间了吗? There can be two reasons. 可能有两个原因。 Either it takes time because the query is executed there. 由于在此执行查询,因此都需要花费时间。 Or because db4o need time to activate the object . 还是因为db4o需要时间来激活对象 Is it a very complex object. 这是一个非常复杂的对象。 My guess is that the query time could be the issue, because you said that the .Count() operation takes its time. 我的猜测是查询时间可能是个问题,因为您说过.Count()操作需要花费时间。

Now you want to get only every 10th object. 现在,您只想获取第10个对象。 Well I believe that the .ElementAt() operator hasn't been yet implemented in an optimized form for db4o. 好吧,我相信.ElementAt()运算符尚未以针对db4o的优化形式实现。 That means it will activate every object in between. 这意味着它将激活之间的每个对象。 In the latest db4o release the .Skip() operated shouldn't activate every object in between. 在最新的db4o发行版中,操作的.Skip()不应激活之间的每个对象。 So you could skip to the right position an the use .FirstOrDefault() there. 因此,您可以在此处使用.FirstOrDefault()跳到正确的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM