There are two ways to store enum types in database: as a string or as an integer.
Saving the enumeration ( sex = {male,female}
, account_type = {regular,pro,admin}
, etc. ) as strings makes things more readable but requires more space than integers.
On the other hand, integers require mapping the enums in and out of the database. As a benefit, case-sensitivity is handled outside of the database with integers.
Assuming both are indexed, is doing the integer conversion generally worth it? How much faster is the lookup with integers?
Example
Perhaps a concrete example could help to visualize things. Lets take the above account_type with a database of 100,000 users.
String enum
Assuming 8-bit fixed length CHAR type
7*100000*8/8 = 700000 bytes
Integer enum
Assuming 8-bit TINYINT integers
100000*8/8 = 400000 bytes
Seems like the size is almost half with integer enums. Also need to concider the indexes.
The answer is, as you would expect, it depends.
The larger the database the more significant the space savings - not only on disk but also in network IO and computation.
Personally, I would store integers instead of textual values, unless there is direct DB supprt for enumerations (as MySQL does).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With