Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Performance: GROUP BY int vs GROUP BY VARCHAR

Is there a performance difference when grouping by different data types? For instance, if I group by INT, will I get better performance than if I group by varchar?

like image 314
richard Avatar asked Aug 25 '11 21:08

richard


People also ask

Is int faster than varchar?

Int comparisons are faster than varchar comparisons, for the simple fact that ints take up much less space than varchars. This holds true both for unindexed and indexed access. The fastest way to go is an indexed int column.

What is the difference between varchar and integer?

Integer is for numbers, and varchar is for numbers, letters and other characters (Short text). So for age you can use a int type, for genders you can use the enum() type if there are only two options. Varchar is text and integer is number.

How do I make a GROUP BY query faster?

GROUP BY performs better if you keep the number of grouping columns small. Avoid grouping redundant columns by using set functions. When you're grouping joined tables, reduce before you expand. You can make a join happen late by replacing it with a set operator.

Does GROUP BY reduce rows?

The GROUP BY clause restricts the rows of the result; only one row appears for each distinct value in the grouping column or columns.


1 Answers

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

like image 134
Simon Hughes Avatar answered Oct 10 '22 19:10

Simon Hughes