SQL Index Eligibility Analysis
Analyze how several SQL query shapes use one composite B-tree index and validate the resulting plans.
Problem statement
For this exercise, assume a multi-tenant events table has tenant_id, event_type, created_at, actor_id, and payload columns, plus one composite B-tree index on `(tenant_id, event_type, created_at)` in that order.
Analyze four query shapes: (A) equality on tenant_id and event_type plus a created_at range, ordered by created_at descending; (B) equality on tenant_id plus a created_at range, ordered by created_at descending, with no event_type predicate; (C) equality on event_type plus a created_at range, with no tenant_id predicate; and (D) equality on tenant_id, two event_type values, and a created_at range, ordered by created_at descending.
Classify each as a targeted seek or range scan, a partial-prefix scan, a full index scan, or not useful enough to choose over the table. Explain filtering and ordering work, validate the plans, and recommend at most one complementary index for the highest-priority unsupported query.
Use portable lexicographic B-tree behavior rather than a vendor-specific optimizer rule.
What to build
- Classify every supplied query shape by how much of the composite B-tree can bound its search.
- Explain the contiguous key ranges, residual filtering, merge or sort work, and likely rows examined for each query.
- Validate each classification with production-like statistics, explain output, and measured execution behavior.
- Recommend at most one additional index for the highest-priority unsupported query and justify its read benefit, storage, and write amplification.
At scale
- Assume the table contains 500 million event rows and receives 2,000 reads and 5,000 writes per second at peak.
- Assume one tenant owns at most 1 percent of rows and event_type values are moderately selective within a tenant.
- Assume eligible first-page queries return at most 100 rows and should complete within 150 milliseconds at p95.
- Assume the conventional B-tree is lexicographically ordered: leading equality predicates preserve later-key narrowing, the first range ends further ordered narrowing, and a skipped leading key prevents a targeted seek unless a broader scan is accepted.