Table aliases in MySQL - Time & Space Complexity
We want to understand how using table aliases affects the time it takes for a database query to run.
Specifically, does giving tables short names change how long the query takes as data grows?
Analyze the time complexity of the following code snippet.
SELECT a.name, b.salary
FROM employees AS a
JOIN salaries AS b ON a.id = b.emp_id
WHERE b.salary > 50000;
This query joins two tables using aliases to shorten table names and filters salaries above 50000.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning rows in both tables to find matching employee IDs.
- How many times: Once for each row in the employees table and once for each row in the salaries table during the join.
As the number of employees and salaries grows, the database must check more rows to join and filter.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 to 20 row checks |
| 100 | About 100 to 200 row checks |
| 1000 | About 1000 to 2000 row checks |
Pattern observation: The work grows roughly in direct proportion to the number of rows in the tables.
Time Complexity: O(n)
This means the time to run the query grows roughly in a straight line as the data size grows.
[X] Wrong: "Using table aliases makes the query run faster because the names are shorter."
[OK] Correct: Aliases only make the query easier to write and read; they do not change how many rows the database processes.
Understanding how query time grows helps you explain database performance clearly and confidently.
"What if we added an index on the join column? How would the time complexity change?"