ParatuTech
Database journalMySQL

MySQL · Composite indexes

MySQL composite indexes across different workloads

See why a tenant-first index can suit tenant queries while leaving cross-tenant reporting or retention work with a different access problem.

A tenant activity page and an overnight retention job can touch the same table but need different access paths. Review both before treating an existing composite index as coverage for the whole workload.

01 Tenant-led range
(tenant_id, created_at)tenant_idcreated_atAOct 01AOct 03AOct 08BOct 01BOct 04BOct 09tenant = A; date ≥ Oct 03One tenant’s dates sit together
02 Date-only range
No tenant conditiontenant_idcreated_atAOct 01AOct 03AOct 08BOct 01BOct 04BOct 09date ≥ Oct 03 onlyMatches span different tenant groups
Illustrative key order and data only. An index on (tenant_id, created_at) supports its leftmost prefix. A cross-tenant date-only query needs its own plan and workload evaluation; eligible Skip Scan and other optimizer strategies may still matter. Inspect the plan; no index choice is guaranteed.

The short version

What to take away

  • Check the leading index columns against each workload, including background jobs.
  • A missing leftmost filter changes the available access paths; it does not prove the index is never used.
  • Evaluate a time-first candidate against tenant reads, cross-tenant work and write cost.

Start with two query shapes

Consider a hypothetical MySQL 8.4 events table with an index on (tenant_id, created_at). A tenant report filters one tenant and a bounded creation-time window. The index groups entries by tenant, then by time within each tenant. That layout can support a range inside the selected tenant.

A cross-tenant report filters the same time window without specifying a tenant. Matching dates are spread across tenant groups. The second query therefore has a different access problem, even though its filter column appears in the index. See MySQL's multiple-column index rules.

Read the exception carefully

Avoid translating the leftmost-prefix rule into “the index cannot be used.” An index scan may still be selected. MySQL also supports Skip Scan for eligible queries, which can visit separate ranges under different leading-key values.

Skip Scan has restrictions: for example, the query must reference one table, use only indexed columns, and have no GROUP BY or DISTINCT. A report selecting an unindexed payload does not satisfy those conditions. Inspect the actual plan for each query rather than assuming this optimization will rescue every cross-tenant workload. The range optimization documentation explains the eligibility rules.

Compare a candidate against the whole workload

A time-first index is a candidate to test for cross-tenant time filtering. It changes how rows are grouped, so do not replace the tenant-first index based on one report.

  • Compare tenant sizes, narrow and wide time windows, selected columns and sorting requirements.
  • Check existing plans, then measure comparable runs in a controlled environment. EXPLAIN ANALYZE executes its statement; use it only where that execution is safe. See EXPLAIN.
  • Include write throughput, storage and regression limits in the decision. For a retention job, evaluate the actual deletion workflow separately; a fast preview SELECT does not establish safe delete behavior.

Record which workload each index serves and the evidence needed before any approved schema change. The diagram illustrates ordering only; it contains no measured performance claim.

Discuss your MySQL workload with ParatuTech.

A considered next step

Bring the workload, the symptoms and the evidence. We’ll help you frame the next decision.

Discuss your database