Window functions (ROW_NUMBER) in MySQL - Time & Space Complexity
We want to understand how the time needed to run a query with ROW_NUMBER grows as the data gets bigger.
How does the number of rows affect the work done by the database?
Analyze the time complexity of the following code snippet.
SELECT
employee_id,
department_id,
salary,
ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
FROM employees;
This query assigns a rank number to each employee within their department based on salary.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database groups rows by department and sorts each group by salary.
- How many times: It processes every row once, but sorting happens within each department group.
As the number of rows grows, the database must sort more data within each department.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 rows sorted in small groups |
| 100 | More rows sorted, groups get bigger |
| 1000 | Much more sorting work inside groups |
Pattern observation: The sorting work grows a bit faster than the number of rows because sorting is more than just looking at each row once.
Time Complexity: O(n log n)
This means the time to run the query grows a little faster than the number of rows because sorting takes extra steps.
[X] Wrong: "ROW_NUMBER just looks at each row once, so it must be O(n)."
[OK] Correct: The function needs to sort rows within each group, and sorting takes more work than just one pass.
Understanding how window functions like ROW_NUMBER scale helps you explain query performance clearly and shows you know how databases handle sorting and grouping.
What if we removed the PARTITION BY clause? How would the time complexity change?