NATURAL join and its risks in PostgreSQL - Time & Space Complexity
We want to understand how the time needed to run a NATURAL JOIN grows as the tables get bigger.
How does the size of the tables affect the work the database must do?
Analyze the time complexity of the following code snippet.
SELECT *
FROM employees
NATURAL JOIN departments;
This query joins two tables by automatically matching columns with the same names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing rows from both tables to find matching values in the shared columns.
- How many times: For each row in the first table, the database checks rows in the second table to find matches.
As the number of rows in each table grows, the work to find matching rows grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons |
| 100 | About 10,000 comparisons |
| 1000 | About 1,000,000 comparisons |
Pattern observation: The work grows roughly by the product of the number of rows in both tables.
Time Complexity: O(n * m)
This means the time grows roughly by multiplying the number of rows in both tables.
[X] Wrong: "NATURAL JOIN is always fast because it automatically matches columns."
[OK] Correct: The automatic matching can cause the database to compare many rows, which can be slow if tables are large or if unexpected columns match.
Understanding how joins work and their time cost helps you explain database performance clearly and shows you know how to handle real data efficiently.
"What if we added indexes on the matching columns? How would the time complexity change?"