简体   繁体   中英

MySQL Enum performance advantage?

Is there a performance advantage to using enum in situations where there are only 5-10 different possible values for a field? if not what is the advantage?

There is a huge performance penalty to using ENUM for operations such as:

  • Query the list of permitted values in the ENUM , for instance to populate a drop-down menu. You have to query the data type from INFORMATION_SCHEMA , and parse the list out of a BLOB field returned.

  • Alter the set of permitted values. It requires an ALTER TABLE statement, which locks the table and may do a restructure.

I'm not a fan of MySQL's ENUM . I prefer to use lookup tables. See also my answer to " How to handle enumerations without enum fields in a database? "

ENUMs are represented internally by 1 or 2 bytes, depending on the number of values. If the strings you're storing are larger than 2 bytes and rarely change, then an ENUM is the way to go. Comparison will be faster with an enum and they take up less space on disk, which in turn can lead to faster seek times.

The downside is that enums are less flexible when it comes to adding/removing values.

In this article http://fernandoipar.com/2009/03/09/using-the-enum-data-type-to-increase-performance/ Fernando looks into performances of Enum type for queries.

The result is that while using ENUM might seem a little less elegant from a design point of view (if your ENUM value are changing sometimes), the performance gain is obvious for large datasets. See his article for details. Do you agree?

No, see a comparison here

The advantage lays in code readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM