← All Exercises
Find Queries Without a Usable Index
intermediatejavascript
Background
Indexes only help when the leading column matches the filter. Reviewing queries against existing index prefixes finds scans that adding "an index on everything" would not fix.
Problem
For each query, decide whether an existing index can serve its leading WHERE column and, when not, propose the column order for a new index.
Input
JSON object: { queries: [{ id, table, where: string[], orderBy?: string[] }], indexes: [{ table, columns: string[] }] }Output
JSON array of { query, covered: boolean, candidateIndex: object | null }Sample tests
Sample 1
Input: {"queries":[{"id":"q1","table":"orders","where":["customer_id"],"orderBy":["created_at"]},{"id":"q2","table":"orders","where":["status"],"orderBy":["created_at"]}],"indexes":[{"table":"orders","columns":["customer_id","created_at"]}]}Expected: [{"query":"q1","covered":true,"candidateIndex":null},{"query":"q2","covered":false,"candidateIndex":{"table":"orders","columns":["status","created_at"]}}]Sample 2
Input: {"queries":[{"id":"q3","table":"users","where":[]}],"indexes":[]}Expected: [{"query":"q3","covered":true,"candidateIndex":null}]Your solution
Learning objective
Review query/index fit and propose the smallest sufficient index.