Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indexes for group by on two columns

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:

  1. A composite index on itemId and style (and probably include styleWeight)
  2. Separate indexes on itemId and style

Which way should I go? Why? Any other options?

like image 967
spender Avatar asked Feb 24 '23 13:02

spender


1 Answers

Composite index on itemId and style with styleWeight included would be the best option.

This will allow Stream Aggregate without sorting and/or clustered seek/RID lookup overhead.

like image 109
Quassnoi Avatar answered Mar 05 '23 18:03

Quassnoi