NTILE for distribution in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to run NTILE changes as the data grows.
How does dividing rows into groups affect the work done?
Analyze the time complexity of the following code snippet.
SELECT id, value, NTILE(4) OVER (ORDER BY value) AS quartile
FROM sales;
This query divides the sales rows into 4 groups based on the value column.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting all rows by the value column.
- How many times: Once over all rows before assigning groups.
As the number of rows grows, sorting takes more time, and then grouping is done in one pass.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 operations (sorting + grouping) |
| 100 | About 700 operations |
| 1000 | About 10,000 operations |
Pattern observation: The work grows faster than the number of rows because sorting is the main cost.
Time Complexity: O(n log n)
This means the time grows a bit faster than the number of rows because sorting is needed before grouping.
[X] Wrong: "NTILE just splits rows quickly, so it runs in linear time."
[OK] Correct: NTILE needs the rows sorted first, and sorting takes more time than just splitting.
Understanding how NTILE works helps you explain how databases handle grouping and sorting efficiently, a useful skill in many data tasks.
"What if we removed the ORDER BY clause inside NTILE? How would the time complexity change?"