in ObjectBox, using ToMany vs using my own list of string ids in managing relations?

Viewed 90

Take the example from the documentation, using ToMany relations:

@Entity
public class Customer {
    @Id public long id;
    
    // 'to' is optional if only one relation matches.
    @Backlink(to = "customer")
    public ToMany<Order> orders;
    
}

@Entity
public class Order {
    @Id public long id;
    
    String stringId;
    ...
}

where stringId in order is a long string like "CS-DE-AA-order1234567890".

For the same purpose, if I keep a list of string ids and manage them myself, it would be like:

@Entity
public class Customer {
    @Id public long id;
    
    String orderIds;
}

where orderIds keeps Orders' stringId separated by space, like: "CS-DE-AA-order1234567891 CD-DF-AB-order1234567891 CT-SE-BB-order1234567892 ..."

So I would like to know how the two schemes compare in terms of performance, efficiency and disk storage.

1 Answers

Native relations have several advantages:

  • For 1:N relations, the ID is embedded in the object on the N side (pointing to a single object) consuming 8 bytes
  • Relations use the object ID, which can be looked up faster than any query
  • Relation properties are automatically indexed for efficient access
  • In queries relations allow you to link between types (aka "join")
  • Scalable; even if you had millions of relations, removing/changing/adding one relation would be very efficient

You can still ignore those advantages and be happy with what you are doing, as it might be good enough for your use case. You know, you did not include specifics and it all depends...

Related