MY PLATFORM:
PHP & mySQL
MY SITUATION:
I came across a situation where I need to store a value for user selection in one of my columns of a table. Now my options would be to:
MY QUESTIONS:
So I wanted to know, which of the above two types:
Leads to faster query speed when that column is accessed (for the sake of simplicity, let's leave out mixing other queries or accessing other columns, please).
Is the most efficient way of storing and accessing data and why?
How does the access speed vary if the columns are indexed and when they are not?
My understanding is that since char(1) and tinyint(1) take up only 1 byte space, storage space will not be an issue in this case. Then what would remain is the access speed. As far as I know, numeric indexing is faster and more efficient than anything else. But the case here is tough one to decide, I think. Would definitely like to hear your experience on this one.
Thank you in advance.
I think you should create column with ENUM('n','y')
. Mysql stores this type in optimal way. It also will help you to store only allowed values in the field.
You can also make it more human friendly ENUM('no','yes')
without affect to performance. Because strings 'no'
and 'yes'
are stored only once per ENUM
definition. Mysql stores only index of the value per row.
Also note about sorting by ENUM
column:
ENUM values are sorted according to the order in which the enumeration members were listed in the column specification. (In other words, ENUM values are sorted according to their index numbers.) For example, 'a' sorts before 'b' for ENUM('a', 'b'), but 'b' sorts before 'a' for ENUM('b', 'a').