Following up on my earlier question regarding GAE Datastore entity hierarchies, I'm still confused about when to use entity groups.
Take this simple example:
Company
has one or more Employee
entitiesEmployee
cannot be moved to another Company
, and users that deal with one Company
can never see the Employee
s of another Company
This looks like a case where I could make Employee
a child entity of Company
, but what are the practical consequences? Does this improve scalability, hurt scalability, or have no impact?
What are other advantages/disadvantages of using or not using an entity hierarchy?
(Entity groups enable transactions, but assume for this example that I do not need transactions).
Data objects in Firestore in Datastore mode are known as entities. An entity has one or more named properties, each of which can have one or more values. Entities of the same kind do not need to have the same properties, and an entity's values for a given property do not all need to be of the same data type.
Attributes are properties of entities.
In Java, you create a new entity by constructing an instance of class Entity , supplying the entity's kind as an argument to the Entity() constructor. After populating the entity's properties if necessary, you save it to the datastore by passing it as an argument to the DatastoreService. put() method.
Entities are Python classes which store an object's state in the database. Each instance of an entity corresponds to a row in the database table. Often entities represent objects from the real world (e.g. Customer, Product). Before creating entity instances you need to map entities to the database tables.
If you don't need transactions, don't use entity groups. They slow things down in some cases, and never speed anything up. Their only benefit is that they enable transactions.
As far as I can tell, the best place to use entity groups is on data that isn't likely to be accessed by many users at the same time, and that you'll frequently want to include in a transaction. So, if you stored the contents of a shopping cart, which probably only the owner of that cart will deal with frequently, those contents might be good for an entity group - it'll be nice to be able to use a transaction for that data when you're adding or updating an entity, and you're not locking anyone else out of anything when you do so.
Nick stated clearly that you should not make the groups larger than necessary, the Best practices for writing scalable applications has some discussion one why.
Use entity groups when you need transactions. In the example you gave, a ReferenceProperty on employee will achieve a similar result.
Aside from transactions, entity groups can be helpful because key-fetches and queries can be keyed off of a parent entity. However, you might want to consider multitenancy for these types of use-cases.
Ultimately large entity groups might hurt scalability, entities within an entity group are stored in the same tablet. The more stuff you cram into one entity group, the more you reduce the amount of work that can be done in parallel -- it needs done serially instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With