In my user_accounts table, I have a field called source which is an ENUM. It indicates how the user was referred to the website. Possible values are via Facebook, via Email, or via regular website signup.
There are 3 possible options for how I can store these values in the database:
-
As a full string, i.e
facebook, email , website -
As a 1 letter code, e.g
F, E, W -
As a 1 number code, e.g
1, 2, 3
Which approach is the best approach from a database performance / maintainence point of view? Is there going to be any impact (such as faster queries) if I stored the values as 1 letter / digit codes rather than as the full strings? This column will be used in WHERE statements.
Performance wise, ENUMS really tend to the most optimized (As the system knows the possible set of values, it uses different algos in searching etc etc). You can store the full string with ENUMS (‘FACEBOOK’, etc etc) and they will take only 1 byte of space per row! (provided its less than 256 enums in total). However, use enums only if you know for sure that F,E,W are the only options you will see. Adding new ENUMS is a pain as you will have to alter the table to update the enum values, and you will have to ensure the enum order.
A close second is digits. The issue with that is readability. You may end up having to keep another table to understand what each digit stands for. (Either in the database or in the application)
Varchar is the worst when it comes to speed of query, but is the most readable and managable.