In MySQL/MariaDB, both utf8 and utf8mb4 are character sets that support the UTF-8 encoding. However, there is a difference between them in terms of the range of characters they can support.
The utf8 character set in MySQL is limited to a maximum of three bytes per character, which means it can only support characters in the Basic Multilingual Plane (BMP), including most commonly used Unicode characters.
On the other hand, the utf8mb4 character set is a superset of utf8 and can support characters outside the BMP, including emojis, certain Asian characters, and other less commonly used Unicode characters. It uses a maximum of four bytes per character.
If you need to store or handle characters outside the BMP, such as emojis, or if you want to future-proof your database to support a wider range of characters, it is recommended to use the utf8mb4 character set to avoid any further “utf8mb3 to utf8mb4” conversion issues
Many modern applications, including popular ones like Moodle, have opted for utf8mb4 as the default character set. WordPress also implemented a strategy to upgrade from utf8 to utf8mb4.
Another difference is the index & prefix index size restrictions. As the number of possible characters in an index varchar for example will be slightly reduced with utf8mb4 because of the additional bit. You may have met in the past errors like:
Index column size too large. The maximum column size is 767 bytes
InnoDB storage engine used to allow 767 bytes for index prefixes; and with the default innodb_large_prefix (after MySQL 5.7) it has reached a limit of 3072 bytes considering a default innodb_page_size of 16KB.
- With utf8: 3072 byte / 4 byte = 1024 possible char per index
- With utf8mb4: 3072 byte / 4 byte = 768 possible char per index
While it was a varchar of 255 for utf8 & 191 for utf8mb4.
Note that the limits that apply to index key prefixes also apply to full-column index keys. [1][2]
This has little impact for most users, as increasing the size of the index keys would be meaningless if the speed & performance gain are lost.
Comments