Scrivio Interview library
LeadershipAdvanced7 min read · updated 12 November 2026

A core entity hierarchy mapped with JOINED inheritance across 12 subclasses now dominates database CPU through wide joins. Changing it affects 6 teams. How would you lead the change?

I would lead it as a measured migration, not a rewrite. First measure the cost and find the queries that suffer, then evaluate the options (SINGLE_TABLE, splitting the hierarchy, or read models and projections for the hot queries) against those queries. The migration runs as expand and contract, with new structures populated in parallel and readers switched gradually, and the six teams agree ownership and a timeline, supported by adapters that reduce their effort. Finally the decision and the lesson about inheritance in shared models are written down.

Why this question is asked

A JOINED hierarchy with 12 subclasses is a common legacy shape: it looked clean on the class diagram, and over the years every polymorphic query grew into a twelve-way outer join that now dominates database CPU. The technical options are well known; what makes this a leadership question is that six teams depend on the entities, each with its own priorities. Interviewers want to hear how we get from evidence to a decision to a safe migration that people actually carry out. In this answer, we will recap why JOINED gets expensive, measure the problem, evaluate the options, plan the expand and contract migration, organise the teams, and record what we learned.

1A Quick Recap: Why JOINED Gets Expensive

With the JOINED strategy, the root class has a table with shared columns, and each of the 12 subclasses has its own table joined by primary key. When code queries the root type, for example all policies for a customer, Hibernate does not know which subclass each row belongs to, so it left outer joins all 12 subclass tables. Every such query touches 13 tables and builds wide intermediate rows, even if the page shows only shared columns. Multiply that by a few thousand requests per second and the joins dominate CPU, even when each one looks reasonable in isolation.

2Measure and Find the Queries That Suffer

Before proposing anything, we collect evidence that the teams can trust. From the database's statistics (pg_stat_statements on PostgreSQL, the performance schema on MySQL) we list the top statements by total CPU time and identify which involve the hierarchy. Typically a few query shapes account for most of the load, for example 70 percent of CPU coming from four polymorphic list queries, while queries for a single subclass are cheap.

SQL
-- PostgreSQL: where does the time go?
SELECT left(query, 80) AS q, calls, round(total_exec_time) AS ms
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
-- select ... from policy p left join motor_policy m ... (12 joins) calls 9.1M ms 41,200,000
-- select ... from policy p left join motor_policy m ... where ... calls 2.3M ms 12,900,000
-- select ... from motor_policy m join policy p ... calls 1.1M ms 310,000

Notice what this tells us. The problem is not inheritance in general; it is a small number of polymorphic queries. That changes the options: we might not need to remap the whole hierarchy at all. We also record which team issues each query, which tells us who must be involved first.

3Evaluate the Options Against Those Queries

Converting to SINGLE_TABLE puts all 13 tables into one wide table with a discriminator, so polymorphic queries need no joins. With 12 subclasses the table may have well over a hundred nullable columns, which needs check constraints for integrity and is a large migration touching every team. Splitting the hierarchy recognises that some subclasses are not really used polymorphically: if only 4 of the 12 appear in the hot list queries, they can form a smaller hierarchy, and the rest become independent entities. Read models or projections leave the entities alone and serve the hot queries from a purpose-built structure, for example a policy_summary table with exactly the columns the list pages show, maintained on write or from change events.

Options for the hot polymorphic queries
OptionFixes hot queriesEffort for teamsRisk
SINGLE_TABLE for all 12yes, no joinshigh: every team migratesbig migration, wide table
Split the hierarchypartly, fewer joinsmedium: some teamsmoderate
Read model for hot queriesyes, for those querieslow: switch readerssync lag, extra writes
DTO projections on root onlyyes, if shared columns sufficelowlow
Often the cheapest option that fixes the measured queries wins.

4Migrate With Expand and Contract

Whichever option we pick, we change the system in small reversible steps rather than a single cut-over. In the expand phase, we add the new structure, such as the summary table or the new single table, and populate it in parallel: a backfill for existing rows plus dual writes or change data capture for new ones. We verify that the new structure matches the old with reconciliation queries that compare counts and checksums every night. Then we switch readers gradually, query by query and team by team, behind feature flags, watching CPU and error rates. Only in the contract phase, when nothing reads the old tables, do we remove them or the code paths that used them.

Step throughExpand and contract for the hierarchy
  1. Expandadd new table or read model
  2. Backfill and dual writeor change data capture
  3. Reconcile nightlycounts and checksums match
  4. Switch readers graduallyper query, behind flags
  5. Contractremove old reads, then old tables

5 steps in order. Play them, or step through them one at a time.

Every step can be paused or reversed until the last one.

A quick win often comes first. If the list pages only need shared columns, rewriting those queries as DTO projections on the root table (select new PolicyRow(p.id, p.number, p.status) from Policy p) can avoid the subclass joins, because Hibernate no longer needs to build subclass entities; Hibernate 6 and later can leave out subclass tables a query does not need, but confirm it in the SQL log for your version. That can cut CPU substantially within weeks, buying time for the structural change.

5Agree Ownership and a Timeline With the Teams

Six teams will each need to change some code, and none of them planned for it. I would present the evidence, the options and the recommendation in one session, so everyone sees the same numbers, and ask each team to confirm which queries are theirs. We then agree a shared timeline with milestones per team and a named owner for the shared parts, usually the team that owns the root entity or a data platform team.

To keep the effort small, the central team provides adapters: a repository or query service for the new read model with the same method signatures as the old queries, migration scripts, the reconciliation job and dashboards. Teams then switch by changing a dependency or a flag rather than rewriting queries. Progress is visible to everyone on the CPU dashboard, which also makes the benefit concrete.

6Record the Decision and the Lesson

Finally we write an architecture decision record: what we measured, the options, why we chose what we chose, and what we did not do. We also capture the broader lesson for future models. Inheritance in a shared model should be chosen from query patterns, deep hierarchies used polymorphically across teams are expensive whatever the strategy, and composition or separate entities are often a better fit for types that differ in data rather than behaviour. Shared through design reviews, that lesson is what prevents the next twelve-subclass hierarchy.

7Follow-up questions

Why not just add indexes?

The subclass tables are already joined by primary key, which is indexed. The cost is the number of joins and the width of the rows built for every polymorphic query, not missing indexes. Indexes help the filter on the root table, but not the twelve joins.

How do you handle a team that cannot fit the work in this quarter?

The adapters and the gradual switch make that possible: the old path keeps working during the expand phase, so a team can move later without blocking others. What we cannot do is contract until the last team has moved, so the timeline names that date and escalates if it slips.

Would you consider moving the hierarchy to a document store?

Only if the access patterns strongly favour it. It adds a new technology, consistency questions and operational cost for six teams. A read model in the same database usually solves the measured problem with far less risk.

8Conclusion

In this answer, we led the change of a JOINED hierarchy that dominates database CPU. We measured the cost and found the few polymorphic queries responsible, evaluated SINGLE_TABLE, splitting the hierarchy, read models and DTO projections against those queries, and migrated with expand and contract: parallel population, reconciliation, gradual reader switches and removal last. We agreed ownership and a timeline with the six teams, gave them adapters to reduce effort, and recorded the decision and the lesson about inheritance in shared models.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd start with evidence, because with six teams involved everyone needs to see the same numbers. I'd pull the top statements by CPU from pg_stat_statements or the equivalent, and usually you find a few polymorphic list queries, the ones that outer join all twelve subclass tables, account for most of the load, while single-subclass queries are cheap.

Then, if they want more

Then I'd evaluate the options against those specific queries: converting to SINGLE_TABLE removes the joins but means a very wide table and a migration for every team; splitting the hierarchy helps if only some subclasses are used polymorphically; and a read model or summary table for the hot queries fixes them with little change to the entities.

Often there's a quick win too: if list pages only need shared columns, a DTO projection on the root table avoids the joins immediately.

For the actual change I'd use expand and contract: add the new structure, backfill and dual write or use change data capture, reconcile nightly, switch readers query by query behind flags, and remove the old paths last.

With the teams, I'd agree ownership and a timeline in one session, and have a central team provide adapters with the same interfaces so each team mostly flips a flag. Finally I'd write a decision record, including the lesson that inheritance in shared models should follow query patterns.

About this answer

Last updated
12 November 2026
Versions it names
Hibernate 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A core entity hierarchy mapped with JOINED inheritance across 12 subclasses now...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/0970e8c9d1/