I have an aggregate query that has a lot of columns and generate a huge data set:
SELECT column1,
column2,
...
column20,
sum(column21),
...
sum(column40)
INTO #Output
FROM #Ledger
GROUP BY column1,
...
column20
The input table (#Ledger
) has 18m rows and the result table (#Output
) has 600k rows. The query took 9 minutes. Is there anything I can do to make it faster? Here's the execution plan:
There's no index on the #Ledger
table and the exclamation point in Sort gives the following warning:
Operator used tempdb to spill data during execution with spill level 1 and 4 spilled thread(s), Sort wrote 1541592 pages to and read 1541592 pages from tempdb with granted memory 3752160KB and used memory 3681824KB