How can I alter an indexed varchar(255) from utf8 to utf8mb4 and still stay under the 767 max key length?

时光毁灭记忆、已成空白 提交于 2019-12-04 04:37:23

问题


I have an mysql column that needs to support emoji, and that means converting a utf8 column into a utf8mb4. But my varchar(255) won't fit, so long as the column is indexed (not unique).

How can I keep the index, and get the utf8mb4 collation?

I've tried to just reduce the length to 191 but unfortunately some of my rows are longer and I get this error: #1406 - Data too long for column 'column_name' at row 33565 (which isn't terribly helpful since I don't have an auto-increment column and have no idea how to fine row 33565).


回答1:


I think it is connected with maximum data length of the row, there is such limitation, at least for string data types as I know. To avoid this try to separate table's data, e.g. split table into two tables using one-to-one relation.

About the maximum key length: I have tried to create table with indexed utf8mb4 field, it was successfully created with key length 191, but when I set it to 192, it threw an error - Specified key was too long; max key length is 767 bytes.




回答2:


I ended up removing the index.

If performance is negatively impacted I may add a second indexed column that only contains the first n characters (up to 191, but likely just 10-20 or so) of the current column.

The 191 character limit is due to the maximum key length of 767 bytes. For a 4 byte character, this means a max of 191 characters (floor(767/4) = 191).



来源:https://stackoverflow.com/questions/20123824/how-can-i-alter-an-indexed-varchar255-from-utf8-to-utf8mb4-and-still-stay-unde

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!