Databases · Senior · Question 63 of 75
For WHERE tenant_id = $1 AND created_at > $2 ORDER BY created_at, which composite index order is best?
- 1Two separate single-column indexes are always better
- 2
(created_at, tenant_id) - ✓Correct answer:
(tenant_id, created_at): equality columns first, then the range or sort column
Explanation
With the equality column first, all rows for the tenant are adjacent and already sorted by time. A range column first would scatter each tenant across the index.