Migrating MySQL UTF8 to UTF8MB4 problems and questions

前端 未结 3 1136
-上瘾入骨i
-上瘾入骨i 2021-01-02 14:03

Im trying to convert my UTF8 MySQL 5.5.30 database to UTF8MB4. I have looked at this article https://mathiasbynens.be/notes/mysql-utf8mb4 but have some questions.

I

3条回答
  •  滥情空心
    2021-01-02 14:52

    1. There are limits on the size of an INDEX. You bumped into the limit because utf8mb4 needs up to 4 bytes per character, where as utf8 needs only 3. Meanwhile the INDEX size limit is in bytes.

    The 'solution' is to decide what to do about the over-sized index. (more below)

    2.

    ALTER TABLE t CHANGE col col ...
    

    is the same as the more logical

    ALTER TABLE t MODIFY col ...
    

    The former allows you to change the name of the column, hence two copies of the column name when you don't need to change the name.

    1. Quite likely you had VARCHAR(255) which takes 767 bytes in utf8 (3*255+2; the "2" is the size of the length field). The equivalent in the 4-byte utf8mb4 would be (191) (4*191+2=766; not room for more than 191).

    2. I have not seen an article about it. I suspect that what I just said is most of what needs to be said.

    So...

    Plan A: Do you have foo VARCHAR(255) and it was utf8? Is the data in it always (now and in the future) shorter than 191 characters? If so, then simply do the ALTER.

    Plan B: If you need more than 191, do you really need the INDEX? DROP INDEX may be an alternative.

    Plan C: Or, you could use a "prefix" index: INDEX(foo(191)), while leaving it VARCHAR(255). Usually "prefix" indexes are useless, but you might have a use case for which it works.

    To discuss this further, please provide SHOW CREATE TABLE for the table in question, and discuss the meaning of that particular field and its INDEX.

提交回复
热议问题