Your query may run slowly if the following conditions are true:
- The query uses aggregates such as group by.
- SQL Server uses hash aggregate in the query plan.
- The estimated number of distinct values is much lower than the actual number of distinct values for the aggregate.
This issue occurs because SQL Server underestimates the amount of memory grant needed. This results in intermiedate results spilling to tempdb.