For example if I have a table User, I want to store gender or sex, I’ll add a column like sex.
Is it really worth to use an integer and then map it in my favorite programming language?
Like 1 => 'Male' and 2 => 'Female'
Is there any performance reason to do that?
Or could I safely use a varchar which more meaning with ‘female’ or ‘male’ almost like I was using mysql ENUM ?
Edit: I here and there that it is sometimes better, sometimes it doesn’t matter, so I more looking for benchmark or something over a “it is better” answer.
I mean I think using varchar is actually more meaningfull than an integer, and I would use an integer only if performance are more than 0.3% or something.
Ortiginal Answer:
I would suggest storing it in a
CHAR(1)column asMorFIt is expressive enough for the specific purpose AND has the speed benefit of being a single character comparison
Update 4 (fixed benchmark):
All previous benchmarks had a fatal flaw that one (the
CHAR(1)) table wasMyISAMand all other wereInnoDB. So I recreated the database with all tables using theMyISAMand the results make much more sense now.The error creeped in as I used the MySQLWorkbench’s wizard to create the tables and forgot to change the database engine in the other tables and it defaulted to
InnoDB(I have MySQL 5.5)So the corrected results are as follows, (I have removed all my previous benchmarks as they were invalid) :
New Conclusion :
TINYINTIs fastest. But my recommendation would be still yo useCHAR(1)as it would be easier for future developers to understand the database.If you do use
TINYINT, my recommendation would be name the columnismaleinstead ofsexand store0 => Femaleand1 => malethus making it a little more easy to understand in raw database.The table structure for benchmark is this:
Only the type of the gender column is different in the 3 tables, the types are:
All 3 tables have
10000entries.