Why aggregation matters in PostgreSQL - Performance Analysis
When we use aggregation in databases, we combine many rows into summary results. Understanding how long this takes helps us know how well our queries will perform as data grows.
We want to see how the work needed changes when we have more data to aggregate.
Analyze the time complexity of the following code snippet.
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query counts how many employees are in each department by grouping rows with the same department.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the employees table once.
- How many times: Once for every employee row (n times).
As the number of employees grows, the database must look at each one 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; doubling rows doubles the work.
Time Complexity: O(n)
This means the time to run the aggregation grows in a straight line with the number of rows.
[X] Wrong: "Aggregation runs instantly no matter how much data there is."
[OK] Correct: The database must still look at every row to count or summarize, so more data means more work and more time.
Knowing how aggregation scales helps you explain query performance clearly and shows you understand how databases handle large data sets.
"What if we added an index on the department column? How would the time complexity change?"
Practice
COUNT or SUM in SQL?Solution
Step 1: Understand aggregation functions
Aggregation functions like COUNT and SUM take many rows and produce one summary value.Step 2: Identify their purpose
They help to reduce data by summarizing, not by deleting or joining tables.Final Answer:
To summarize multiple rows into a single value -> Option AQuick Check:
Aggregation = Summary [OK]
- Thinking aggregation deletes rows
- Confusing aggregation with JOIN
- Believing aggregation creates new tables
sales?Solution
Step 1: Recall correct COUNT syntax
The correct syntax to count all rows isSELECT COUNT(*) FROM table_name;.Step 2: Check each option
SELECT COUNT(*) FROM sales; matches the correct syntax. Others have syntax errors or wrong order.Final Answer:
SELECT COUNT(*) FROM sales; -> Option BQuick Check:
COUNT(*) syntax = SELECT COUNT(*) FROM sales; [OK]
- Omitting parentheses after COUNT
- Using COUNT without * or column
- Wrong order of keywords
orders with columns customer_id and amount, what does this query return?SELECT customer_id, SUM(amount) FROM orders GROUP BY customer_id;
Solution
Step 1: Understand GROUP BY usage
GROUP BY customer_id groups rows by each customer.Step 2: Understand SUM(amount)
SUM(amount) adds all amounts per customer group.Final Answer:
Total amount spent by each customer -> Option AQuick Check:
GROUP BY + SUM = total per group [OK]
- Thinking SUM returns total for all rows without grouping
- Confusing SUM with COUNT
- Ignoring GROUP BY effect
SELECT customer_id, COUNT(*) FROM orders;
Solution
Step 1: Check aggregation with non-aggregated column
customer_id is selected but not aggregated or grouped.Step 2: Identify missing GROUP BY
To use COUNT(*) with customer_id, GROUP BY customer_id is required.Final Answer:
Missing GROUP BY clause for customer_id -> Option CQuick Check:
Non-aggregated columns need GROUP BY [OK]
- Forgetting GROUP BY when selecting non-aggregated columns
- Thinking COUNT needs WHERE clause
- Assuming ORDER BY is mandatory with aggregation
Solution
Step 1: Understand filtering groups with HAVING
HAVING filters groups after aggregation; WHERE filters rows before aggregation.Step 2: Check query order and clauses
SELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3; correctly uses GROUP BY then HAVING COUNT(*) > 3 to filter customers with more than 3 orders.Final Answer:
SELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3; -> Option DQuick Check:
Use HAVING to filter groups after GROUP BY [OK]
- Using WHERE to filter aggregated counts
- Placing HAVING before GROUP BY
- Syntax errors mixing WHERE and HAVING
Ready to prove it?
Pass the test and this pattern moves unknown
