简体   繁体   English

如何从db4o查询最早的对象?

[英]How to query for the oldest object from db4o?

I have objects that has a DateTime property, how can i query for the oldest object? 我的对象具有DateTime属性,如何查询最早的对象?

After asking on db4o forum, I get the answer: 在db4o论坛上询问后,我得到了答案:

It's quite easy: create a sorted SODA-Query and take the first / last object from the resulting ObjectSet . 这很容易:创建一个排序的SODA-Query and从结果ObjectSet获取第一个/最后一个对象。 Don't iterate the ObjectSet (therefore the objects won't be activated), just take the required object directly via #ObjectSet.Get(index) . 不要迭代ObjectSet (因此将不会激活对象),只需直接通过#ObjectSet.Get(index)获取所需的对象。

Please note: db4o supports just a limited set of performant sortings (alphabetical, numbers, object ids) in query execution, so maybe you have to store your DateTime as milliseconds to achieve good performance. 请注意:db4o在查询执行中仅支持一组有限的性能排序(字母,数字,对象ID),因此也许您必须将DateTime存储为毫秒才能获得良好的性能。

first of all, your object needs to keep track of the time itself, so it depends on your requirements: 首先,您的对象需要跟踪时间本身,因此这取决于您的要求:

class Customer
{
    public DateTime DateSignedUp {get; private set;}
    // ...
}

Now, you can query for the object in whatever way you like, using Linq, SODA, or Native Queries, eg 现在,您可以使用Linq,SODA或本机查询以任意方式查询对象。

IObjectContainer container = ...;
Customer oldestCustomer = container.Query<Customer>().OrderBy(p => p.DateSignedUp).First();

However, there is a set of pitfalls: 但是,有一些陷阱:

  1. Don't use DateTime in your persisted object. 不要在持久对象中使用DateTime I have had massive problems with them. 我遇到了很多问题。 I can't reproduce the problem so I couldn't report it yet, but I can personally not recommend using them. 我无法重现该问题,因此无法报告,但是我个人不建议使用它们。 Use a long instead and copy the ticks from the respective DateTime . 请改用long并复制相应DateTime的刻度。 Store all times in UTC, unless you're explicitly referring to local time, such as in the case of bus schedules. 将所有时间都存储在UTC中,除非您明确引用本地时间,例如在公交车时刻表中。
  2. Put an index on the time 在时间上建立索引
  3. The order operation could be very, very slow for large amounts of objects because of issue COR-1133 . 由于问题COR-1133 ,对于大量对象,订单操作可能非常非常慢。 If you have a large amount of objects and you know the approximate age of the object, try to impose a where constrain, because that will be fast. 如果您有大量对象,并且知道对象的大致年龄,请尝试在where施加约束,因为这样做会很快。 See also my blogpost regarding that performance issue, which can become very annoying already at ~50-100k objects. 另请参阅有关此性能问题的博客 ,该问题在大约50-100k个对象上已经非常令人讨厌。

Best, 最好,
Chris 克里斯

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

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