CS FundamentalsHardProduct-company MCQ pattern

A query optimizer must decide between two equivalent plans for `A JOIN B`: (1) nested-loop join using an index on B, with A as the outer (driving) table, or (2) the same but with B as the outer table. If A has 10 rows and B has 1,000,000 rows, which choice is almost always better, and why?

  1. A. Neither option can use an index, so this choice is irrelevant
  2. B. B as the outer table, because indexing on the larger table is always faster
  3. C. A as the outer table (only 10 iterations), each doing one indexed lookup into B, versus B as the outer table requiring 1,000,000 iterations each needing a lookup into A — far more total work
  4. D. It makes no difference since the total number of rows compared is the same either way

Correct answer: C. A as the outer table (only 10 iterations), each doing one indexed lookup into B, versus B as the outer table requiring 1,000,000 iterations each needing a lookup into A — far more total work

Nested-loop join cost scales with (outer row count) × (cost per inner lookup). Driving with the smaller table (A, 10 rows) means only 10 indexed lookups into B; driving with B as the outer table means 1,000,000 indexed lookups into A — even though each individual lookup might be similarly cheap, the total work differs by 5 orders of magnitude, which is why optimizers strongly prefer the smaller input as the outer/driving table in nested-loop joins.

More DBMS and SQL questions

Exams that ask this topic