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
ENUM
. It's stored as an integer, but displayed as a string.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 (likevarchar
).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