Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

A multi-tenant SaaS table has indexes on (tenant_id), (tenant_id, created_at), (created_at, tenant_id) and (tenant_id, user_id, created_at). Writes are slow. How do you decide which to keep?

(tenant_id) is a prefix of the other tenant-leading indexes and is almost always redundant, so it goes first. For the rest, we map the frequent queries from pg_stat_statements or performance_schema to the index each one needs: (tenant_id, created_at) and (tenant_id, user_id, created_at) serve different queries and both stay only if user-level queries are frequent, while (created_at, tenant_id) serves only cross-tenant time queries and may be replaced by BRIN or moved to a replica. We confirm with usage counters over a full business cycle and drop in stages, watching latency after each drop.

Why this question is asked

When writes slow down on a busy table, extra indexes are one of the first suspects, because every insert must update every index. But removing an index blindly can turn a fast query into a table scan at the worst moment. Interviewers ask this to see whether we can reason about prefixes and query patterns and then remove indexes safely with evidence. In this answer, we will see how each index is used, find the redundant one, map queries to indexes, look at the cross-tenant index and BRIN, and then plan the drops.

1Why Four Indexes Slow Writes

Each INSERT into this table writes the row and one entry in each index, so five structures change per row, plus WAL for all of them. Updates that change created_at or user_id must also move entries in the indexes that contain those columns, and in PostgreSQL they lose the cheap HOT update path. On a table taking thousands of writes per second, removing even one index can visibly reduce write latency and replica lag.

So the goal is the smallest set of indexes that still serves every important query well.

2The Redundant Prefix: (tenant_id)

A composite index can serve any query on its leading columns. (tenant_id, created_at) is sorted by tenant_id first, so WHERE tenant_id = ? can descend to the tenant's entries and read them, exactly like an index on (tenant_id) alone. The same is true for (tenant_id, user_id, created_at). So the single-column index adds a full set of writes and buys almost nothing.

The only small difference is size: a scan of all of one tenant's entries reads a slightly smaller index. That rarely matters enough to pay for an extra index on every write. Unless it backs a constraint, which a plain non-unique index does not, it is the first to drop.

SQL
SELECT indexrelid::regclass AS index, idx_scan, pg_size_pretty(pg_relation_size(indexrelid))
FROM pg_stat_user_indexes WHERE relname = 'events';
-- events_tenant_idx | 1840221 | 21 GB -- used, but a prefix of the next two
-- events_tenant_created_idx | 98312004 | 38 GB
-- events_created_tenant_idx | 4120 | 38 GB -- rarely used
-- events_tenant_user_created_idx | 12500877 | 52 GB

Notice that the (tenant_id) index shows scans. That does not mean it is needed; it means the planner picked it because it was there. Once it is gone, the same queries will use the (tenant_id, created_at) index with practically the same cost.

3Mapping Queries to Indexes

Next we collect the real query patterns. pg_stat_statements in PostgreSQL, or the statement digests in MySQL's performance_schema, list the normalised queries with their call counts and total time. For each frequent query we write down which index serves it best. This is the step that turns an opinion into a decision.

Frequent queries and the index each needs
Query patternCalls per dayBest index
tenant's recent events, ORDER BY created_at DESC40 million(tenant_id, created_at)
one user's recent events in a tenant8 million(tenant_id, user_id, created_at)
count of a tenant's events1 millioneither tenant-leading index
all tenants, events in the last hour (internal report)24(created_at, tenant_id) or BRIN
Illustrative numbers; the real ones come from query statistics over a full cycle.

4The Two Tenant-Leading Indexes Serve Different Queries

(tenant_id, created_at) answers 'the tenant's latest events', reading one contiguous run of the tenant's entries in time order. (tenant_id, user_id, created_at) answers 'this user's latest events', reading one contiguous run for that user. The second index cannot serve the first query well, because inside a tenant its entries are grouped by user, so the tenant's events are not in time order; the database would have to read all of them and sort. And the first index cannot serve the user query well unless it reads the whole tenant's history and filters by user.

So both are justified only if both query types are frequent and latency-sensitive. In our example, 8 million user-level queries a day is plenty, so both stay. If user-level queries were rare, an admin screen used a few times a day, we could drop the three-column index and let those queries filter the tenant's recent entries.

5The Cross-Tenant Index and BRIN

(created_at, tenant_id) leads with time, so it serves queries across all tenants for a time range, such as an internal report of the last hour's events. In a tenant-isolated application those queries are rare, 24 times a day here, but the index costs writes on every one of millions of inserts.

If the table is append-mostly, rows arrive roughly in created_at order, so the physical position in the table correlates with time. A BRIN index, block range index, stores just the minimum and maximum created_at for each range of table pages, by default 128 pages. It is often a few megabytes instead of tens of gigabytes, costs almost nothing to maintain, and lets a time-range query skip the page ranges that cannot match. Alternatively, the report can run on a read replica or in an analytics store, where heavier indexes do not slow the primary's writes.

SQL
CREATE INDEX CONCURRENTLY events_created_brin ON events USING brin (created_at);
SELECT pg_size_pretty(pg_relation_size('events_created_brin')); -- 3 MB (illustrative)
-- then, after checking the report's plan and timing:
DROP INDEX CONCURRENTLY events_created_tenant_idx; -- frees 38 GB

6Dropping Safely, in Stages

Index usage counters only mean something over a full business cycle. A monthly billing job or a quarter-end report may run once and need an index the rest of the month never uses. So we read idx_scan before and after a complete cycle, on the primary and on every replica, because counters are kept per server.

Then we drop one index at a time, with DROP INDEX CONCURRENTLY in PostgreSQL so writes are not blocked, keep its CREATE statement ready, and watch query latency and error dashboards for a few days before the next drop. In MySQL 8, we can first make an index invisible, so the optimizer ignores it while it is still maintained, and make it visible again instantly if something regresses.

Step throughRemoving indexes from the events table
  1. Drop (tenant_id)prefix of two others
  2. Watch latency for a few daystenant queries use (tenant_id, created_at)
  3. Add BRIN on created_at, test the reportor move the report to a replica
  4. Drop (created_at, tenant_id)after a full month of usage data
  5. Are user-level queries frequent?
    YesKeep (tenant_id, user_id, created_at)NoDrop it and watch again

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

7Follow-up questions

What is the risk of dropping an index that a monthly job uses?

The job silently changes from an index scan to a full table scan. On a large table it may run for hours instead of minutes, read most of the table, push hot pages out of the cache, slow every tenant while it runs, and miss the deadline it exists for, such as producing invoices on the first of the month. Because it runs once a month, the problem appears weeks after the drop, when nobody connects the two. We avoid it by checking usage over a full cycle, asking job owners, and keeping the index definition ready to recreate concurrently.

Would partitioning change the answer?

If the table were partitioned by created_at, old partitions could be dropped or kept on cheaper storage, and time-range queries would skip partitions without any time-leading index. Each partition still needs the tenant-leading indexes for application queries.

How do we find redundant indexes across many tables?

A catalog query that compares index column lists finds indexes whose columns are a leading prefix of another index on the same table, ignoring unique and constraint-backing indexes. Running it weekly and sending results to owning teams keeps the problem from returning.

8Conclusion

In this answer, we saw how to prune four indexes on a multi-tenant table: (tenant_id) is a prefix of the other tenant-leading indexes and is redundant, frequent queries from pg_stat_statements or performance_schema decide whether both (tenant_id, created_at) and (tenant_id, user_id, created_at) earn their write cost, and (created_at, tenant_id) serves only cross-tenant time queries that BRIN or a replica can handle. Usage counters over a full business cycle and staged drops with DROP INDEX CONCURRENTLY keep the change safe.

What to say in the interviewAbout 90 seconds aloud

Open with

First, the single-column tenant_id index is a prefix of two other indexes, so any query on tenant_id alone can use those instead. It is redundant in almost every case and I would drop it first.

Then, if they want more

Then I would collect the real query patterns from pg_stat_statements or performance_schema and map each frequent query to the index that serves it. The tenant_id plus created_at index serves the tenant's recent activity.

The tenant_id, user_id, created_at index serves a single user's recent activity, which the first index cannot do well, and vice versa, so I keep both only if user-level queries are frequent enough to pay for the extra writes. The created_at, tenant_id index only helps queries across all tenants by time, like an internal report.

If rows arrive roughly in time order, a BRIN index on created_at is tiny and can serve that report, or the report can move to a replica, so I would test that and then drop the big index. Before every drop I check the usage counters over a full business cycle, including month-end jobs, on the primary and replicas.

I drop one index at a time with DROP INDEX CONCURRENTLY, keep the definition ready to recreate, and watch latency after each step. In MySQL I would make the index invisible first.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8
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 multi-tenant SaaS table has indexes on (tenant_id), (tenant_id, created_at)...": https://scrivio-interview-library.netlify.app/databases/indexing/cf35b206fd/