Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is converting database string enums to integers worth it?

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.

like image 929
randomguy Avatar asked Jul 18 '11 11:07

randomguy


1 Answers

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).

like image 153
Oded Avatar answered Nov 06 '22 21:11

Oded