How the join engine matches rows in SQL - Performance & Efficiency
When databases join tables, they match rows from each table to combine data.
We want to know how the time to do this matching grows as tables get bigger.
Analyze the time complexity of the following SQL join operation.
SELECT *
FROM Employees e
JOIN Departments d ON e.DepartmentID = d.ID;
This query matches each employee to their department by comparing IDs.
Look for repeated steps in the join process.
- Primary operation: Comparing each employee row to department rows to find matches.
- How many times: For each employee, the database checks department rows until it finds a match.
As the number of employees and departments grows, the matching work changes.
| Input Size (Employees x Departments) | Approx. Operations |
|---|---|
| 10 x 5 | About 50 comparisons |
| 100 x 20 | About 2,000 comparisons |
| 1000 x 100 | About 100,000 comparisons |
Pattern observation: The number of comparisons grows roughly by multiplying the sizes of both tables.
Time Complexity: O(n * m)
This means the time to join grows roughly by multiplying the number of rows in each table.
[X] Wrong: "Joining two tables always takes time proportional to just one table's size."
[OK] Correct: The join compares rows from both tables, so both sizes affect the total work.
Understanding how joins scale helps you explain database performance clearly and confidently.
"What if one table has an index on the join column? How would the time complexity change?"