Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index for ENUM datatypes

Tags:

mysql

The text book tells me that it is not recommended to use index for enumerated datatypes. But it didn't tell me why. Should I use index for ENUM? The book also tells me that we should index column which we use in WHERE clause. I always use ENUM in WHERE part of my query and it should be indexed according to the book. And it also says not to index enumerated datatypes. Now what should I do?

Edit:

I think I made a mistake while asking, I just read the same book again and I think I got a misunderstanding while reading, the book didn't explicitly said we should not use index for ENUM but it said that we should not use index for columns that have very limited range of values such as yes/no, 0/1 etc. And the thing I grabbed from the book is that such columns are of ENUM types.

like image 440
user15 Avatar asked Oct 04 '17 13:10

user15


People also ask

Can enum be indexed?

Use the Object. values() method to get an array containing the enum's values. Use square brackets to access the array at the specific index and get the value.

What type of data can be stored by enum data type?

The ENUM data type, different from standard data types, is an enumerated list of 1 to 65,535 strings indicating the allowed values for the field. When defining an ENUM, you are creating a list of items from which the value must be selected (or it can be NULL).

What is enum data type in SQL?

An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time. See Section 11.3. 1, “String Data Type Syntax” for ENUM type syntax and length limits.

How enum values are stored in MySQL?

The ENUM data type is stored in two locations: the set of values is stored in the table metadata; in each row, only the set index is stored, as integer, which requires one byte for enums up to 255 entries large, then two for up to 65535 entries (see MySQL reference)


2 Answers

I just want to share my personal experience with an index on enums. I had a really slow query and found this while googling, which kind of discouraged me. But eventually I tried adding an index to my enum column anyhow.

My query was this:

SELECT * FROM my_table
WHERE my_enum IN ('a', 'b')
ORDER BY id DESC
LIMIT 0, 100;

The id column is the primary key. I have 25.000 rows in my_table. There are 4 possible values for my_enum.

Without an index on my_enum, the query took around 50 seconds to complete. With an index it takes 0.015.

This was on a 12 core Xeon Gold MySQL 8.0 server.

like image 104
Bjørn Stenfeldt Avatar answered Oct 12 '22 00:10

Bjørn Stenfeldt


The enum data type is simply stored as a number (the position of the list item value within the list):

The strings you specify as input values are automatically encoded as numbers.

Thus, an enum field can be indexed just as any other numeric fields.

like image 45
Shadow Avatar answered Oct 11 '22 23:10

Shadow