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.
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.