tags:

views:

40

answers:

1

I have objects that has a DateTime property, how can i query for the oldest object?

After asking on db4o forum, I get the answer:

It's quite easy: create a sorted SODA-Query and take the first / last object from the resulting ObjectSet. Don't iterate the ObjectSet (therefore the objects won't be activated), just take the required object directly via #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.

+1  A: 

Hi Benny,

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, e.g.

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. 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. Store all times in UTC, unless you're explicitly referring to local time, such as in the case of bus schedules.
  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. 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. See also my blogpost regarding that performance issue, which can become very annoying already at ~50-100k objects.

Best,
Chris

mnemosyn
thanks, Chris, glad to know about this. didn't see you on skype, and i am following you now on twitter.
Benny