An application uses a compound index on `{ 'status': 1, 'priority': -1 }`. Which query will efficiently use this index?
Since the query filters by the leading field in the compound index ('status'), it can perform an index scan. This allows the database to quickly narrow down the result set without scanning every document in the collection, leading to significantly better performance for the application's read operations.
Why this answer
Compound indexes are ordered. A query will only use the index if the query filter matches the prefix of the indexed fields. Here, the index starts with 'status'.
Therefore, queries filtering on 'status' and optionally 'priority' will be efficient. Understanding index prefixing is crucial for DBAs to ensure that developers write efficient queries that fully leverage the index structures, avoiding unnecessary collection scans and keeping performance stable as the data grows.
Exam trap
Candidates often think an index on fields B and C can be used when querying only field C, ignoring the strict left-to-right prefix rule of compound indexes.