1) Table per class hierarchy approachThis is the approach where all the data for the entire class hierarchy (parents & children included) are stored in the same table.
Advantages:
- Fast performance in most situations.
- Ease of implementation.
- Table will clutter up with too many columns & with a lot of null values if the children classes have a lot of columns unique to them.
- Which in turns makes it less flexible / scalable (e.g. when some of the child classes need more columns).
- Use only where there are very few class-specific columns (i.e. vast majority of columns are shared between classes in the hierarchy).
- And in addition if requirements to add columns to child classes are not likely to happen.
Advantages:
- Data is split to multiple tables. For large data-sets, there won't be any gigantic parent tables for shared data.
- For large data sets, queries on the child classes are much simpler with better performance (i.e. no joins needed).
- No common location to store common data. If you want to go into the DB and manually run a query at the abstracted parent level across all tables, good luck!
- Changes in DB structure for parent class requires changes to DB tables in all child classes.
- Queries at the parent class level will be expensive due to unions required.
- Use only when a very small percentage of queries are at the parent class level.
- This is more for situations where there is a need to run simple & infrequent logic at a more abstracted level, while most queries & operations are at the respective child-class levels.
- Use it only when the abstraction at the parent level is not important to you (i.e. only as a convenience in certain situations).
This is the approach where sub-classes & the parent share primary keys. The shared columns of the sub-classes will reside together in a parent table.
Advantages:
- Common data is stored in common parent tables. Therefore easy to maintain, easy to query.
- Sub-class tables are compact & do not contain redundant columns.
- Sub-classes can be very different in structure & this will still work well.
- Outer Joins used to query data for each instance. On big data-sets, this can be a very expensive option.
- The above is exacerbated when there are multiple layers & complex class hierarchies.
- Logically most elegant approach. This is the most recommended approach unless you have a large dataset.
- However with large data-set and/or complex class hierarchies, performance will be a very real concern.
Advantages:
- Same as (3) with the added advantage of having more control over the joins (via the fetch="select" option).
- Without the joins, this is more vulnerable to the N+1 query problem.
- Use this over (3) when you have large data sets & the N+1 query problem isn't severe in your situation.
No comments:
Post a Comment