I have a large temp table (~160 million rows) #itemsTemp
itemId | style | styleWeight
--------------------------------
int | smallint | float(53)
and the following query on it:
select
itemId,
style,
SUM(styleWeight) itemCount
from
#itemsTemp
group by itemId,style
Currently #itemsTemp has no indexes. I’m a little confused about what would be best here:
- A composite index on
itemIdandstyle(and probablyincludestyleWeight) - Separate indexes on
itemIdandstyle
Which way should I go? Why? Any other options?
Composite index on
itemIdandstylewithstyleWeightincluded would be the best option.This will allow
Stream Aggregatewithout sorting and/or clustered seek/RID lookup overhead.