In this lesson, you will learn how the database engine estimates the resource burden of sorting data during GROUP BY and DISTINCT operations.
The Hidden Work of Sorting
When you write a query using GROUP BY or DISTINCT, the database must group identical values together so it can aggregate them or filter out duplicates. If there is no pre-existing index to help, the database engine must perform an explicit Sort Operation.
Sorting is computationally expensive because it requires the engine to hold your data in memory (specifically in TempDB or a memory buffer) to rearrange it. The cost isn't just the time spent shuffling rows; it is the resource impact on your system’s memory and disk I/O.
How the Optimizer Estimates Cost
The Query Optimizer uses the statistics you've learned about previously (histograms and density) to estimate the number of rows it needs to sort. The formula usually follows an O(N log N) complexity. This means as the number of rows (N) increases, the cost of the sort doesn't just grow linearly—it accelerates.
Consider this query on an unindexed column:
SELECT DISTINCT CategoryID
FROM LargeSalesTable;
To execute this, the engine must:
- Scan: Read every row from
LargeSalesTable. - Sort: Arrange the rows in memory. If the result set exceeds the "Sort Memory Grant" allocated by the optimizer, the engine is forced to "spill" this data to
TempDBon your disk. - Deduplicate: Walk through the sorted list to keep only unique values.
The Role of Memory Grants
The optimizer looks at your table statistics to request a "Memory Grant" before the query starts. If your statistics are outdated or skewed, the engine might underestimate the number of rows. If it allocates too little memory, the sort operation will throttle performance as it waits for disk-based TempDB operations. Conversely, if it allocates too much, you waste precious RAM that other concurrent queries could have used, leading to system-wide contention.
Key Takeaway: Because sorting is an O(N log N) operation that consumes memory grants and risks TempDB spillover, the optimizer prefers using indexes to provide pre-sorted data whenever possible.