4

When creating a table which column type is better from a searching / speed standpoint?

For certain tables I run across a need for a column called Status. I typically store this an int32 in the database and then use a class to reference it in my code:

 public static int Published = 1;
 public static int Draft = 0;
 public static int Deleted = -1;

Is there much of a speed difference in using this column as a varchar instead?

 public static string Published = "Published";
 public static string Draft = "Draft";
 public static string Deleted = "Deleted";

I like the second varchar method better because it helps with readability in the database and then I don't have to constantly convert the int status code to a string for human use.

Any help would be appreciated

3
  • What RDBMS. If you're using MySQL, you could use an ENUM. It's stored as an integer, but displayed as a string.
    – Barmar
    Sep 8, 2014 at 5:54
  • MS SQL and Entity Framework
    – oledu.com
    Sep 8, 2014 at 6:03
  • 2
    Typically, int is better for performance, because (1) it's only 4 bytes long, and (2) it's a fixed-width data type that doesn't incur the overhead of a variable-length datatype (like varchar). Varchar(10) is between 2 and 12 bytes long, and variable-length, which is slightly less optimal for performance - it carries a bit of overhead with it. These points really only come into play if you have a substantial number of entries, though - if you have several hundreds, the performance difference is neglible
    – marc_s
    Sep 8, 2014 at 6:27

1 Answer 1

6

As a best practice you should choose the narrowest column. The narrower the column, the less amount of data SQL Server has to store, and the faster SQL Server is able to read and write data. In addition, if any indexes are created on the column, index size will be narrower and hence the overall performance will be impacted on a positive side. As you have column that is designed to hold only numbers, use a numeric data type, such as INTEGER, instead of a VARCHAR or CHAR data type. Numeric data types require less space to hold the same numeric value as does a character data type. This helps to reduce the size of the columns, and can boost performance when the columns is searched (WHERE clause), joined to another column, or sorted.

Also if you are going to store values in columns as numbers -1, 1 through 10, then the TINYINT data type is more appropriate that the INT data type.

Hope this helps!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.