Scrivio Interview library
ScenarioAdvanced8 min read · updated 12 November 2026

A dashboard query joins orders, customers and a 3,000 row region table. The planner starts from orders (200M rows) and the query takes minutes. MySQL's EXPLAIN shows the region filter applied last. How do you approach it, and when would you force join order?

First we find out why the optimizer starts from the 200 million row orders table: compare its estimates with reality, check for missing indexes on the join keys, and make sure the region filter is sargable. Then we refresh statistics with ANALYZE TABLE and add histograms with ANALYZE TABLE ... UPDATE HISTOGRAM so the optimizer sees how selective the region filter is, and validate with EXPLAIN ANALYZE on production-like data. Forcing the order with STRAIGHT_JOIN or a JOIN_ORDER hint is a last resort for one documented query, and for a dashboard a summary table maintained by the write path may be the better design.

Why this question is asked

Dashboards are where bad join orders hurt most: they run often, users wait for them, and they tend to join the biggest fact table with small dimension tables. When the optimizer starts from the wrong end, a query that should read a few thousand rows reads hundreds of millions. Interviewers ask this scenario to see whether we diagnose before forcing, know MySQL's tools for statistics and hints, and can step back to the design question. In this answer, we will see why join order matters here, diagnose the optimizer's choice, fix the statistics, look at forcing order as a last resort, consider a summary table, and validate the result.

1Why Starting From orders Is So Expensive

MySQL executes joins as nested loops: it reads rows from the first table in the join order and, for each one, looks up matching rows in the next table, usually through an index. So the first table decides how many lookups follow. If the query wants orders for customers in one region, starting from the 3,000 row region table, filtering to one region, then finding its customers and then their orders through indexes touches only the rows that matter.

Starting from orders means reading 200 million rows, looking up each customer, and only at the end discovering that most rows belong to other regions. The EXPLAIN output in the question, with the region filter applied last, shows exactly that: the filter that removes 99% of the rows runs after almost all the work is done.

Two join orders for the same dashboard
OrderRows read firstLookups after thatTypical time
orders -> customers -> region200,000,000 orders200M customer lookups, filter at the endminutes
region -> customers -> orders1 region of 3,000its customers, then their orders by indexwell under a second
Illustrative numbers: the selective filter should run first, not last.

2Diagnosing the Optimizer's Choice

The optimizer chose this order because, by its estimates, it was cheapest. So we look at what it believed. EXPLAIN FORMAT=TREE, or EXPLAIN with its rows and filtered columns, shows the estimated rows and the fraction expected to pass each condition.

SQL
EXPLAIN FORMAT=TREE
SELECT r.name, count(*), sum(o.total)
FROM orders o
JOIN customers c ON c.id = o.customer_id
JOIN region r ON r.id = c.region_id
WHERE r.code = 'EU-NORTH' AND o.created_at >= '2026-10-01'
GROUP BY r.name;
SHOW INDEX FROM customers; -- is there an index on customers(region_id)?
SHOW INDEX FROM orders; -- and on orders(customer_id, created_at)?

Three things are worth checking. Are there indexes on the join keys in the direction we want, customers(region_id) and orders(customer_id) or better orders(customer_id, created_at)? Without them, starting from region gives no cheap way to reach orders, and the optimizer is right to avoid it. Is the region filter sargable, a plain equality on a column rather than something like UPPER(r.code) = ?, so its selectivity can be estimated and used? And how many rows does the optimizer expect from the region filter and the date filter; if it thinks the date filter on orders is very selective, it may prefer starting there.

3Fixing the Statistics

MySQL's InnoDB statistics are estimated from sampled index pages, and for columns without an index the optimizer may have no distribution information at all, so it guesses a default selectivity. ANALYZE TABLE refreshes the index statistics. Since MySQL 8.0, histograms add distribution data for columns, which is especially useful for non-indexed columns used in filters.

Java
ANALYZE TABLE orders, customers, region; -- refresh index statistics
ANALYZE TABLE region UPDATE HISTOGRAM ON code WITH 64 BUCKETS;
ANALYZE TABLE orders UPDATE HISTOGRAM ON created_at WITH 256 BUCKETS;
SELECT * FROM information_schema.COLUMN_STATISTICS WHERE TABLE_NAME = 'region';

Notice that histograms help most for columns without an index. For indexed columns, MySQL often estimates ranges by index dives, which sample the index directly. With better information, the optimizer can see that r.code = 'EU-NORTH' selects 1 of 3,000 rows and that the date range covers a large share of orders, and starting from region becomes the cheapest order on its own.

4Forcing the Join Order as a Last Resort

If statistics and indexes are right and the optimizer still chooses badly, MySQL lets us force the order. STRAIGHT_JOIN makes MySQL join tables in the order they appear in the FROM clause. Optimizer hints, written as special comments, are more precise: JOIN_ORDER fixes the order of the named tables, and JOIN_PREFIX fixes only which tables come first.

SQL
SELECT /*+ JOIN_ORDER(r, c, o) */ r.name, count(*), sum(o.total)
FROM orders o
JOIN customers c ON c.id = o.customer_id
JOIN region r ON r.id = c.region_id
WHERE r.code = 'EU-NORTH' AND o.created_at >= '2026-10-01'
GROUP BY r.name;

A forced order is a decision that does not adapt. If one region later holds half of all customers, or the dashboard gains a filter that makes another table more selective, the hint keeps forcing the old order. So we use it for one query, with a comment explaining why, an owner, and a reminder to review it when the data changes significantly. It is not something to add to every slow query.

5Would a Summary Table Fit Better?

It is worth asking whether a dashboard should join 200 million rows on every view at all. If it shows totals per region per day, a summary table such as region_daily_sales(region_id, day, order_count, revenue) can be maintained when orders are written, through an event consumer or a scheduled job that aggregates new orders every few minutes. The dashboard then reads a few hundred rows instead of joining the raw tables.

The cost is freshness and complexity: the summary may lag by minutes, and it needs a process that keeps it correct when orders are updated or cancelled. For a dashboard that refreshes every minute and serves many users, that trade is usually worth it, and it removes the join order question entirely.

6Validating the Fix

Whatever we change, we validate on production-like data, because estimates and join orders depend on volume and distribution. Since MySQL 8.0.18, EXPLAIN ANALYZE runs the query and reports actual rows and time per step, so we can confirm that the region lookup comes first, that the actual rows at each step are small, and that the total time dropped. We compare before and after on the same copy and keep the result with the change, so the next person can see why the order is what it is.

Java
EXPLAIN ANALYZE SELECT ...;
-- -> Nested loop inner join (actual time=0.4..85.2 rows=41000 loops=1)
-- -> Filter: (r.code = 'EU-NORTH') (actual rows=1 loops=1)
-- -> Index lookup on c using idx_region_id (region_id=r.id) (actual rows=1200 loops=1)
-- -> Index range scan on o using idx_customer_created (actual rows=34 loops=1200)

7Follow-up questions

What is the difference between STRAIGHT_JOIN and the JOIN_ORDER hint?

STRAIGHT_JOIN forces the whole join to follow the textual order of tables in the FROM clause, so the query text itself becomes the plan, and reordering the SQL changes behaviour. JOIN_ORDER names the tables and their order explicitly in a hint comment, can cover only some of the tables, and is easier to read and review. Both freeze the decision, so both need the same documentation and review.

Why might MySQL still prefer orders first after ANALYZE TABLE?

Usually because an index is missing for the other direction, for example no index on customers(region_id) or orders(customer_id), so starting from region would need a scan of customers or orders anyway. It can also be that the optimizer has no distribution data for a non-indexed filter column, which a histogram fixes, or that the date filter is estimated as far more selective than it is.

Can a covering index help here?

Yes. An index on orders(customer_id, created_at, total) lets MySQL find a customer's recent orders and read the total from the index itself, without visiting the table rows. For a dashboard that sums totals, that can cut the work on the orders side by a large factor.

8Conclusion

In this answer, we saw that starting a nested loop join from the 200 million row orders table and applying the region filter last does nearly all the work for nothing. We diagnosed the choice by checking estimates, join key indexes and whether the filter is sargable, refreshed statistics with ANALYZE TABLE and histograms, and treated STRAIGHT_JOIN or JOIN_ORDER as a documented last resort for one query. We also considered a summary table maintained by the write path, which suits a dashboard better than joining raw data on every view, and validated the result with EXPLAIN ANALYZE on production-like data.

What to say in the interviewAbout 100 seconds aloud

Open with

MySQL runs joins as nested loops, so the first table decides how much work follows. Starting from 200 million orders and applying the region filter last means reading almost everything and throwing most of it away, when starting from one region out of 3,000 and following indexes would read a tiny fraction.

Then, if they want more

So first I would find out why the optimizer chose that order. I would check its estimates with EXPLAIN, check that the join keys have indexes in the useful direction, customers on region_id and orders on customer_id, ideally with created_at, and check that the region filter is a plain sargable equality.

Next, statistics: ANALYZE TABLE to refresh index statistics, and since MySQL 8 histograms with ANALYZE TABLE UPDATE HISTOGRAM on filter columns that have no index, so the optimizer knows that region code selects one row out of 3,000. Usually that is enough for it to start from region on its own.

If it still gets it wrong, I would force the order with a JOIN_ORDER hint or STRAIGHT_JOIN, but only for that query, documented, with an owner, because it will not adapt when the data changes. I would also ask whether a dashboard should join 200 million rows on every view at all.

A summary table per region and day, maintained from the write path or a frequent job, often fits better, at the cost of some lag. And I would validate any change with EXPLAIN ANALYZE, available since 8.0.18, on production-like data.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8.0
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 dashboard query joins orders, customers and a 3,000 row region table. The planner...": https://scrivio-interview-library.netlify.app/databases/query-optimization/b3a6269609/