Why aggregation summarizes data in MySQL - Performance Analysis
When we use aggregation in SQL, we combine many rows into fewer summary results.
We want to understand how the work grows as the data size grows.
Analyze the time complexity of the following aggregation query.
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query counts how many employees are in each department by grouping rows.
Look for repeated work done by the query.
- Primary operation: Scanning each employee row once to assign it to a group.
- How many times: Once per row in the employees table.
The query looks at every employee once to count them by department.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to summarize grows in a straight line with the number of rows.
[X] Wrong: "Aggregation is instant no matter how many rows there are."
[OK] Correct: The database must still look at each row to count or summarize it, so more rows mean more work.
Understanding how aggregation scales helps you explain query performance clearly and confidently.
"What if we added a WHERE clause to filter rows before aggregation? How would the time complexity change?"