Scrivio Interview library
LeadershipPrincipal8 min read · updated 12 November 2026

Database performance folklore (avoid subqueries, avoid joins, always denormalise) spreads through your engineering organisation and drives poor designs. How would you replace folklore with evidence at company scale?

I would replace folklore with a small, repeatable system of evidence: published guidelines backed by benchmarks that anyone can rerun on the databases and versions we actually run, EXPLAIN ANALYZE and pg_stat_statements as a normal part of reviewing data-heavy changes, and a small database guild that reviews hot queries and teaches. Normalised designs are the default, denormalisation needs a written decision with measurements, and the guidance is revisited after every major database upgrade.

Why this question is asked

Rules like 'never use subqueries' or 'joins are slow, always denormalise' sound like experience, and they spread because they are short and easy to repeat. The trouble is that many of them were true for one engine, one version or one data shape years ago, and they quietly push teams into designs that are harder to change and often slower. Interviewers ask this at a senior level because fixing it is not a technical trick; it is changing how a whole organisation decides things. In this answer, we will see where the folklore comes from, how to build a benchmark practice people trust, how to put plans and query statistics into everyday reviews, how a guild spreads the skill, and how to keep guidance honest as databases change.

1Why Database Folklore Spreads

Most folklore started as a true observation. In older MySQL versions, an IN (SELECT ...) subquery was often executed as a dependent subquery, run once per outer row, so 'rewrite subqueries as joins' was good advice. MySQL 5.6 added semi-join transformations and the advice became mostly obsolete, but it kept travelling. In PostgreSQL before version 12, every CTE was an optimisation fence, so 'CTEs are slow' was true there and is mostly false now.

The second source is a single bad experience. A team once had a join on an unindexed foreign key take minutes, and the lesson they took away was 'joins are slow' rather than 'index your foreign keys'. The lesson is then repeated in design reviews by people who never saw the original case.

So the problem is not that engineers are careless. The problem is that a rule without its context, its version and its numbers cannot be checked, and a rule that cannot be checked cannot be retired. Our job is to make every performance claim checkable.

Common claims and what is usually true
ClaimWhere it came fromUsually true today
Avoid subqueriesMySQL 5.5 ran IN subqueries per rowOptimisers turn most into semi-joins
CTEs are slowPostgreSQL 11 and earlier fenced every CTESince 12, single-use CTEs are inlined
Joins are slowJoins on unindexed foreign keysIndexed joins are cheap; check the plan
Always denormaliseRead-heavy pages on one hot queryOnly with a measured need and an owner
Each claim had a real origin. Recording the origin is what lets us retire it.

2Publish Guidelines Backed by Reproducible Benchmarks

The first step is a small, public set of guidelines, each one tied to a benchmark that anyone can rerun. A benchmark here is a script in a shared repository that creates a realistic schema, loads data with a realistic shape (for example 50 million orders skewed so that 1 percent of customers place 30 percent of them), runs the competing query forms, and records EXPLAIN (ANALYZE, BUFFERS) output and timings.

Notice the words 'the databases and versions you run'. A benchmark on a laptop with PostgreSQL 18 and an empty cache says little about production on PostgreSQL 16 with 64 GB of shared buffers. So the harness pins the engine version in a container image (Testcontainers works well for this in Java teams), states the hardware class, and runs in CI on a schedule so results stay current.

SQL
-- benchmarks/subquery_vs_join/README: PostgreSQL 16.4, 50M orders, warm cache
-- Form A: IN subquery
EXPLAIN (ANALYZE, BUFFERS)
SELECT c.id FROM customers c
WHERE c.id IN (SELECT o.customer_id FROM orders o WHERE o.total > 1000);
-- Hash Semi Join ... Execution Time: 412 ms
-- Form B: join with DISTINCT
EXPLAIN (ANALYZE, BUFFERS)
SELECT DISTINCT c.id FROM customers c JOIN orders o ON o.customer_id = c.id
WHERE o.total > 1000;
-- Hash Join + HashAggregate ... Execution Time: 655 ms

The numbers above are an illustration of what the harness records, not a universal result. The point is the shape of the evidence: two forms, one plan each, real timings, a version and a data size. A guideline written from this reads 'IN subqueries are fine on PostgreSQL 16; the planner uses a semi-join. See benchmark subquery_vs_join.' Anyone who disagrees can change the data shape and rerun it, which turns an argument into an experiment.

3Make Plans and Query Statistics Part of Reviews

Guidelines only help if they reach the moment a decision is made, which is the code review. For changes that add or alter heavy queries, the review template asks for the EXPLAIN ANALYZE output on a realistic dataset and a note on which index the query relies on. This takes a few minutes and catches most surprises before they ship.

In production, pg_stat_statements is the evidence for what actually costs money. It is a PostgreSQL extension that records, for every normalised query, how many times it ran, its total and mean execution time, and the rows and buffers it touched. Sorting by total time shows which queries deserve attention, which is often not the one people argue about.

SQL
SELECT query, calls, round(total_exec_time) AS total_ms,
round(mean_exec_time, 1) AS mean_ms, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;
-- calls 8,200,000 mean_ms 0.9 (a cheap query called very often)
-- calls 1,440 mean_ms 2,300 (a slow report)

So each team reviews its top ten queries monthly, and the platform team publishes the company-wide top list. MySQL teams use the performance_schema statement digests for the same purpose. The habit we want is 'show me the plan and the numbers', not 'I have heard that'.

4Run a Small Database Guild

Tools are not enough without people who can read the output. A guild is a small group, perhaps four to six engineers with strong database skills drawn from several teams, who spend part of their time reviewing hot queries, answering design questions and teaching. They own the benchmark repository and the guidelines.

Their most useful work is teaching through real cases. A monthly session that takes one slow production query, reads its plan together and fixes it teaches more than any document. Over a year, a team that has seen five such sessions stops repeating folklore because it can check the claim itself. The guild should be consultative, not a gate: if every schema change waits for them, teams will route around them.

Step throughFrom a claim to a guideline
  1. Someone states a performance rulein a review or design doc
  2. Is there a benchmark for it?
    YesLink it and apply the guidelineNoGuild writes one on our versions
  3. Run on realistic data and versionrecord plans and timings
  4. Does the claim hold?
    YesPublish guideline with contextNoPublish the correction
  5. Re-run after each major upgradeCI schedule

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

5Normalised by Default, Denormalised With Measurements

The default design is normalised: each fact stored once, with foreign keys and indexes. This is the design that is easiest to keep correct, because an update touches one row. Denormalisation, storing a copy of data to avoid a join, is sometimes right, for example a product listing page that runs 3,000 times a second and joins five tables.

But a copy has to be kept in sync, and every copy is a future bug. So we ask for a short decision record: the query, its current plan and latency, the target latency, the measured result of the denormalised design, and who owns the synchronisation (a trigger, a materialised view, or an event consumer). If the record cannot show a measured gain, the team keeps the normalised design.

6Revisit Guidance After Every Upgrade

Optimisers improve with every major release, so a guideline is a claim about a version. When we move from PostgreSQL 16 to 18, or from MySQL 8.0 to 8.4, the guild reruns the whole benchmark suite and updates or retires rules. Each guideline carries the version it was measured on and the date, so a reader can see when it is stale.

This also builds trust. When engineers see that the guild retired 'avoid CTEs' after the PostgreSQL 12 change, they learn that the guidance follows evidence, and they are more willing to follow the rules that remain.

7Follow-up questions

What if a team insists their denormalisation is needed but has no numbers?

I would help them get the numbers rather than refuse. We take the slow query, capture its plan and latency, try the obvious fixes first (a missing index, a covering index, a rewritten predicate), and only then compare against the denormalised design. Often the index alone gets the page under the target. If it does not, the measurement becomes the decision record and the team proceeds with an owner for the synchronisation.

How do you measure whether the programme is working?

I would track a few outcomes: the share of database time spent in the top 20 queries, the number of incidents caused by slow queries per quarter, how many reviews include a plan for heavy queries, and how many guidelines were updated after the last upgrade. Anecdotes matter too: if design reviews start saying 'the benchmark shows' instead of 'I heard', the culture is changing.

Is EXPLAIN ANALYZE safe to run in production?

EXPLAIN ANALYZE really executes the statement, so for an UPDATE or DELETE we wrap it in a transaction and roll back, and for heavy reads we prefer a replica or a production-sized staging copy. Plain EXPLAIN without ANALYZE only plans and is always safe, but it shows estimates rather than actual rows and times.

8Conclusion

In this answer, we saw that database folklore usually begins as a true observation about one engine or version and outlives its context. We replace it with checkable evidence: guidelines tied to reproducible benchmarks on our own versions, plans and pg_stat_statements in everyday reviews, a small guild that reviews hot queries and teaches, normalised designs by default with measured decision records for denormalisation, and a rerun of the guidance after every major upgrade.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start by accepting that most folklore was once true somewhere. Avoid subqueries came from old MySQL, avoid CTEs from PostgreSQL before 12. So my aim is to make every performance claim checkable.

Then, if they want more

First, a shared benchmark repository: scripts that build realistic data on the exact engine versions we run, compare query forms, and record EXPLAIN ANALYZE and timings, run in CI. Each published guideline links to its benchmark and states its version.

Second, I would put evidence into reviews: for data-heavy changes the author attaches the plan on realistic data, and teams look at pg_stat_statements monthly to see which queries really cost time.

Third, a small guild of engineers with strong database skills who review hot queries, own the guidelines, and teach with real production cases, acting as consultants rather than a gate. Fourth, normalised design is the default; denormalisation is allowed with a short decision record showing the measured gain and who owns keeping the copy in sync.

And after every major upgrade we rerun the suite and retire rules that no longer hold, which is also what makes people trust the rules that remain.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 16, MySQL 5.6, MySQL 8.0, PostgreSQL 12
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 "Database performance folklore (avoid subqueries, avoid joins, always denormalise)...": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/0471a3d0fd/