Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

A Customer table has grown to 120 columns because every feature added fields; some are large and rarely read. A team proposes splitting into several one-to-one tables (customer_core, customer_preferences, customer_kyc). How would you evaluate and do this safely?

I would weigh the benefits, smaller hot rows, less I/O, separate ownership and access control for sensitive data such as KYC, against the costs, joins for full views, more mapping and the one-to-one lazy loading traps in JPA. If the split pays off, each extension table shares the customer's primary key with @MapsId and is mapped from its own side, and the migration follows expand and contract: create the tables, backfill and dual write, switch reads, then drop the old columns, measuring before and after.

Why this question is asked

A table that grows a column for every feature is a familiar sight in systems older than five years. At 120 columns, every read of a customer drags along kilobytes of data that most screens never show, sensitive identity documents sit next to the marketing opt-in, and every team needs to change the same table. Splitting it sounds obviously right, but done carelessly it adds joins everywhere and triggers Hibernate's one-to-one pitfalls. Interviewers ask this design question to see whether we evaluate before we act and migrate without downtime. In this answer, we will look at what the split gains and costs, decide which columns move, design the mapping, and plan the migration step by step.

1What the Split Gains

The first gain is smaller hot rows. In PostgreSQL and MySQL, rows are stored in pages, typically 8 KB and 16 KB. If the average customer row is 3 KB because of large text columns and rarely used fields, only a few rows fit per page, so a list of 50 customers reads many pages. If the hot columns, name, status, email and a few more, make up 300 bytes, ten times as many rows fit per page, the cache holds more customers, and the common queries do less I/O. Very large values may already be stored out of line by the database, so the real gain depends on the data, which is why we measure.

The second gain is ownership and security. KYC data, the identity checks required by financial regulation, often needs stricter access, its own retention rules and its own audit. In a separate table it can have its own grants, be owned by the compliance team's service, and even be encrypted differently. The third gain is clearer modules: preferences belong to the notifications team, KYC to compliance, and each team changes its own table without coordinating a migration of the shared one.

2What the Split Costs

Any screen that needs the full customer now joins three or four tables, and any report that did select * from customer must change. In the mapping, we gain several entities and the question of how they relate. The biggest JPA trap is the one-to-one: if Customer gets a field @OneToOne(mappedBy = "customer") CustomerKyc kyc, Hibernate cannot make that field a lazy proxy, because the customer row has no column telling it whether a KYC row exists, so it queries the KYC table for every customer it loads. A careless split can therefore turn one query into four on every list page, the opposite of what we wanted.

So before deciding, I would collect facts: which queries hit the table most, which columns they read, the average and largest row sizes, and which columns are sensitive. If the hot queries already select only a few columns through projections, the I/O gain may be small, and the main reason to split becomes ownership.

Deciding which columns move
GroupExample columnsRead byDestination
Coreid, name, email, status, created_atalmost every querycustomer_core
Preferenceslanguage, marketing_opt_in, channelsnotifications servicecustomer_preferences
KYCdocument_type, document_number, verified_atcompliance onlycustomer_kyc, restricted grants
Large and rarenotes, legacy_import_blobsupport screenscustomer_extra or archive

3Designing the Mapping

Each extension table uses the customer id as its own primary key, which @MapsId expresses in JPA. The primary key doubles as the foreign key, there is no extra unique index, and one row per customer is guaranteed. Crucially, the association is mapped from the extension's side only, so loading a customer never touches the extension tables.

Java
@Entity @Table(name = "customer_core")
class Customer {
@Id @GeneratedValue Long id;
String name;
String email;
@Enumerated(EnumType.STRING) CustomerStatus status;
// no fields for preferences or KYC: no hidden one-to-one queries
}
@Entity @Table(name = "customer_kyc")
class CustomerKyc {
@Id Long customerId; // equals the customer's id
@OneToOne(fetch = FetchType.LAZY) @MapsId
@JoinColumn(name = "customer_id") Customer customer;
String documentType;
String documentNumber;
Instant verifiedAt;
}
Customer c = customerRepository.findById(42L).orElseThrow(); // select ... from customer_core where id = 42
Optional<CustomerKyc> kyc = kycRepository.findById(42L); // only when the use case needs it

Screens that need several parts load them deliberately, either with a second repository call or with a query that joins and returns a DTO. For reporting, a database view customer_full that joins the tables gives analysts the old shape without the application having to provide it.

4Migrating With Expand and Contract

The table is in production and read by many services, so we change it in small, reversible steps instead of one big migration. In the expand phase we create customer_kyc and the other tables next to the wide table, and change the application to write both: every update to KYC fields goes to the old columns and the new table in the same transaction. A backfill job copies existing rows in batches of, say, 10,000 so it does not lock the table, and a comparison job checks that old and new data match.

Next we switch reads to the new tables, behind a feature flag so we can switch back. When reads have run cleanly for a while, we stop writing the old columns. In the contract phase, after checking that no query, report or other service still reads them, using the database's statistics or query logs, we drop the old columns. Each step can be released and rolled back on its own.

Step throughExpand and contract for one extension table
  1. Create customer_kycnew table, old columns untouched
  2. Dual writeapplication writes old columns and new table
  3. Backfill in batchescopy existing rows, compare old and new
  4. Switch reads behind a flagcan switch back if numbers look wrong
  5. Stop old writesafter a quiet period
  6. Check no remaining readersquery logs, reports, other services
  7. Drop old columnscontract phase complete

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

5Measuring Before and After

We record the baseline before the first step: the latency of the ten most frequent customer queries, the statement count of the main screens, buffer cache hit ratio for the table, and row size. After switching reads, we compare the same numbers. A successful split shows smaller rows for customer_core, unchanged or fewer statements per screen, and lower latency on list queries. If statement counts went up, the usual cause is a one-to-one mapped from the customer side or a screen that now loads every part separately, and we fix that before continuing with the next table.

Doing one table at a time, KYC first if security is the main driver, keeps the risk small and lets each step prove its value before the next.

6Follow-up questions

Would you ever keep the wide table instead?

Yes, if measurements show the hot queries already use projections, the large columns are stored out of line anyway, and no team needs separate ownership. Then the cost of joins and migration buys little, and documenting the column groups may be enough.

Why not use @SecondaryTable to map several tables into one Customer entity?

@SecondaryTable maps one entity across tables joined by primary key, so the Java model stays the same. But Hibernate then joins the secondary tables on every load of the entity, which brings back the cost we wanted to remove, and it does not give the separate ownership that a separate entity does. It can be useful as a temporary step during migration.

How do other services that read the wide table directly fit in?

They are the main reason to keep a compatibility view during the migration. A view with the old name and columns, built on the new tables, lets them keep working while they move to the owning service's API. Database query logs show which ones still read it before we drop it.

7Conclusion

In this answer, we evaluated splitting a 120-column customer table by weighing smaller hot rows and separate ownership of sensitive data against joins, mapping complexity and one-to-one lazy loading traps. We mapped each extension table with a shared primary key through @MapsId from its own side only, migrated with expand and contract, kept a view for readers of the old shape, and measured before and after.

What to say in the interviewAbout 80 seconds aloud

Open with

I would first evaluate with data. The benefits of splitting are smaller hot rows, so more customers fit per page and the common queries do less I/O, separate access control and ownership for sensitive data like KYC, and clearer modules where each team owns its table.

Then, if they want more

The costs are joins for any full view, changes to reports, more mapping, and in JPA the one-to-one trap: a mappedBy one-to-one from Customer to an extension cannot be lazy, so careless mapping turns one query into four. So I would look at the most frequent queries, the columns they read, row sizes and which columns are sensitive.

If it pays off, each extension table uses the customer id as its primary key with @MapsId, and I would map the association only from the extension side, so loading a customer never touches the others, and fetch extensions by id when a use case needs them.

The migration would be expand and contract, one table at a time: create the new table, dual write, backfill in batches with a comparison job, switch reads behind a flag, stop old writes, confirm no readers remain, then drop the old columns. A compatibility view helps other readers. And I would measure latency, statement counts and row sizes before and after.

About this answer

Last updated
12 November 2026
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 Customer table has grown to 120 columns because every feature added fields; some are...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/da181ef449/