0
0
MySQLquery~5 mins

Table aliases in MySQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Table aliases
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of employees and salaries grows, the database must check more rows to join and filter.

Input Size (n)Approx. Operations
10About 10 to 20 row checks
100About 100 to 200 row checks
1000About 1000 to 2000 row checks

Pattern observation: The work grows roughly in direct proportion to the number of rows in the tables.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the query grows roughly in a straight line as the data size grows.

Common Mistake

[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.

Interview Connect

Understanding how query time grows helps you explain database performance clearly and confidently.

Self-Check

"What if we added an index on the join column? How would the time complexity change?"