CS FundamentalsHardAmazon · Microsoft MCQ pattern
A query planner chooses a full table scan over using an available index on a WHERE column. Which scenario makes this the RIGHT choice, not a missed optimization?
- A. The indexed column is the primary key
- B. The query's WHERE condition matches a very large fraction of the table's rows (low selectivity), so the extra cost of random-access lookups via the index outweighs just scanning sequentially
- C. The table has no rows at all
- D. An index always makes every query faster, so this would never be correct
Correct answer: B. The query's WHERE condition matches a very large fraction of the table's rows (low selectivity), so the extra cost of random-access lookups via the index outweighs just scanning sequentially
Indexes help most for highly selective conditions (few matching rows), where seeking directly to matches beats scanning everything. If a condition matches most of the table, a sequential scan reading pages in order can actually be cheaper than a flood of (often random) index-driven lookups, which is exactly the kind of case-by-case decision a cost-based query optimizer is meant to make.
More DBMS and SQL questions
- Which statement about a query execution plan using a hash join versus a nested-loop join is correct?
- A sort-merge join requires both input relations to be sorted on the join key (or sorted as part of the plan). When is sort-merge join…
- A transaction executes BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE…
- A relation R(A, B, C, D, E) has candidate key AB and no other candidate keys. Which of the following dependencies, if it existed, would…
- A relation R(A, B, C, D) has candidate key AB. If C and D are both non-prime, and B → C holds (B alone, part of the key, determines C)…
- A query optimizer estimates the selectivity of WHERE age = 30 using a histogram on the age column rather than assuming a uniform…