Friday, March 5, 2010

Char Vs Varchar

The char is a fixed-length[Storage Size] character data type, the varchar is a variable-length character data type.

Because char is a fixed-length data type, the storage size of the char value is equal to the maximum size for this column. Because varchar is a variable-length data type, the storage size of the varchar value is the actual length of the data entered, not the maximum size for this column.

You can use char when the data entries in a column are expected to be the same size.
You can use varchar when the data entries in a column are expected to vary considerably in size.

1. Storage wise: char columns have fixed length. If the user supplied value for the column is less than the fixed length defined in the schema, the column is padded with 0 at end to make the total length fixed. varchar doesn't have a fixed length thus no padding is needed. But as the result varchar columns have to store the size of the data together with the column
data, which takes an extra 2 bytes per varchar column.

2. Performance wise locating char is a little faster than varchar. Since char columns have fixed length, they are stored in fixed location in a row. This means locating a char column can directly jump to the fixed location in a row to read. For varchar column since the size of the data is variable, they can't be stored in fixed location in a row and rather there is some kind of lookup table in the row format to store the location of each varchar column. This means locating a varchar column has to lookup the location of the column in the lookup table stored in the row first before jumping to the location to read. Referencing the lokup table introduces some perofrmance overhead, especially ifthe lookup table reference causes cache line miss.

No comments: