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?
- A. Neither option can use an index, so this choice is irrelevant
- B. B as the outer table, because indexing on the larger table is always faster
- 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
- 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
- A relation R(A, B, C, D, E) has FDs AB → C, C → DE. Which of the following is the candidate key, and how many non-prime attributes does R…
- Which pair of properties must every primary key satisfy?
- Which ACID property guarantees that a transaction's changes are applied completely or not at all?
- Which of these SQL commands is a DDL (Data Definition Language) command?
- In an ER model, a weak entity set is identified by:
- Which SQL clause filters groups AFTER aggregation?