开发者

SQL Server Performance: GROUP BY int vs GROUP BY VARCHAR

Is there a performance difference when grouping by different data t开发者_开发问答ypes? For instance, if I group by INT, will I get better performance than if I group by varchar?


I would say GROUP BY INT is faster, as only 4 bytes are checked verses n bytes in a varchar field.


Are you deciding on a data type based solely on how the data type performs in a GROUP BY? Is it the same data, you're just deciding how to store 123456, as an INT or a VARCHAR? Have you given consideration to other factors, such as the CPU cost of converting between numeric and string types when it might not otherwise be necessary? The extra memory required to hold the whole table in cache? The row overhead for VARCHAR indicating length? What about storage costs (e.g. 1234567890 takes 4 bytes as an INT, but '1234567890' takes 10 bytes + row overhead as a VARCHAR)? How about compression? How will the index on this column be aligned with the clustered index on the table, which can affect how useful "already grouped" will be?

In other words, I wouldn't consider GROUP BY performance in a bubble.


Grouping on an int would be slightly faster than grouping on a varchar, but what really makes a difference is if there is an index on the field that the database can use for the grouping.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜