295

I'm trying to figure out what collation I should be using for various types of data. 100% of the content I will be storing is user-submitted.

My understanding is that I should be using UTF-8 General CI (Case-Insensitive) instead of UTF-8 Binary. However, I can't find a clear a distinction between UTF-8 General CI and UTF-8 Unicode CI.

  1. Should I be storing user-submitted content in UTF-8 General or UTF-8 Unicode CI columns?
  2. What type of data would UTF-8 Binary be applicable to?
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Dolph
  • 49,714
  • 13
  • 63
  • 88
  • 20
    Side note but instead of `utf8`, use `utf8mb4` instead for full UTF-8 support. Commenting here because the answers on this popular question do not address this. https://mathiasbynens.be/notes/mysql-utf8mb4 – Steven R. Loomis Jan 06 '16 at 19:33
  • If you want case folding, but accent sensitivity, please file a request at http://bugs.mysql.com . – Rick James Mar 14 '17 at 22:47
  • Or click "Affects Me" on https://bugs.mysql.com/bug.php?id=58797 and add a comment. – Rick James Jun 06 '17 at 20:48
  • _Now that 8.0 is common, much of this Question and the Answers are out of date_. (Feel free to start a new Question to get a more targeted answer.) – Rick James Sep 02 '21 at 17:49

5 Answers5

306

In general, utf8_general_ci is faster than utf8_unicode_ci, but less correct.

Here is the difference:

For any Unicode character set, operations performed using the _general_ci collation are faster than those for the _unicode_ci collation. For example, comparisons for the utf8_general_ci collation are faster, but slightly less correct, than comparisons for utf8_unicode_ci. The reason for this is that utf8_unicode_ci supports mappings such as expansions; that is, when one character compares as equal to combinations of other characters. For example, in German and some other languages “ß” is equal to “ss”. utf8_unicode_ci also supports contractions and ignorable characters. utf8_general_ci is a legacy collation that does not support expansions, contractions, or ignorable characters. It can make only one-to-one comparisons between characters.

Quoted from: http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html

For more detailed explanation, please read the following post from MySQL forums: http://forums.mysql.com/read.php?103,187048,188748

As for utf8_bin: Both utf8_general_ci and utf8_unicode_ci perform case-insensitive comparison. In constrast, utf8_bin is case-sensitive (among other differences), because it compares the binary values of the characters.

Sagi
  • 8,009
  • 3
  • 26
  • 25
  • 2
    I think that if you don't have a good reason to use _unicode_ci, then use _general_ci. – Sagi Feb 26 '10 at 19:12
  • 4
    This doesn't really answer the question in depth though. *What is the difference between these collations exactly?* – Pekka Apr 02 '11 at 22:34
  • 4
    You are right, the exact difference is not provided here for sake of simplicity. I've added a link to a post with the *exact* difference. – Sagi Sep 16 '11 at 16:43
  • NB `show collation;` allows you to see the default collation for each character set. 5.1 shows `utf8_general_ci` as default for `utf8`. – David Carboni Jul 16 '12 at 14:53
  • 9
    Are there any resources that would go more in-depth in the actual speed difference between the two collations? Are we talking about a 0.1% drop in performance or a 10% drop? – Emphram Stavanger Mar 04 '13 at 18:11
  • Does utf8-bin collation means exact binary match? – Calmarius Mar 11 '14 at 11:54
93

You should also be aware of the fact, that with utf8_general_ci when using a varchar field as unique or primary index inserting 2 values like 'a' and 'á' would give a duplicate key error.

Alex Hepp
  • 931
  • 6
  • 2
  • 3
    Thanks, this is useful to avoid similar usernames (e.g. if "jose" exists, I wouldn't want someone else to create a "josé" user) NB: this also holds true for most of the utf8 collations (except utf8_bin). The surest/safest/most comprehensive is `utf8_unicode_ci` – Costa Apr 10 '13 at 03:12
  • 2
    I use utf8_bin where I want jose and josé to be distinguished in the index. For example, a column that records search/replace operations, where the user might have decided to search for josé, and replace it with jose. (I'm writing a spreadsheet program) – Buttle Butkus May 09 '13 at 19:56
47
  • utf8_bin compares the bits blindly. No case folding, no accent stripping.
  • utf8_general_ci compares one codepoint with one codepoint. It does case folding and accent stripping, but no 2-character comparisons; for example: ij is not equal ij in this collation.
  • utf8_*_ci is a set of language-specific rules, but otherwise like unicode_ci. Some special cases: Ç, Č, ch, ll
  • utf8_unicode_ci follows an old Unicode standard for comparisons. ij=ij, but ae != æ
  • utf8_unicode_520_ci follows an newer Unicode standard. ae = æ

See collation chart for details on what is equal to what in various utf8 collations.

utf8, as defined by MySQL is limited to the 1- to 3-byte utf8 codes. This leaves out Emoji and some of Chinese. So you should really switch to utf8mb4 if you want to go much beyond Europe.

The above points apply to utf8mb4, after suitable spelling change. Going forward, utf8mb4 and utf8mb4_unicode_520_ci are preferred. Or (in 8.0) utf8mb4_0900_ai_ci

  • utf16 and utf32 are variants on utf8; there is virtually no use for them.
  • ucs2 is closer to "Unicode" than "utf8"; there is virtually no use for it.
Rick James
  • 135,179
  • 13
  • 127
  • 222
  • 1
    Re "stay tuned": [_8.0 collations_](http://mysql.rjweb.org/utf8mb4_collations.html) shows how various characters, diphthongs, etc, compare in the 8.0 utf8mb4 collations; utf8 is mostly the same. – Rick James Feb 15 '17 at 22:55
  • And 8.0 collations are clocked at being _significantly_ faster than 5.x. – Rick James Jun 06 '17 at 20:49
  • it would be nice if that page lists utf8mb4_bin at the top. I know it does no character matching at all, but it's good for newbies. – Henk Poley Jul 19 '19 at 08:58
  • @TobySpeight - Thanks. Now I am worried that I messed up on other answers; I have said things like that many times in the last decade. Now that 8.0 is the current version, many questions like this are not asked -- general_ci is not the default anymore. – Rick James Mar 31 '21 at 21:02
7

Accepted answer is outdated.

If you use MySQL 5.5.3+, use utf8mb4_unicode_ci instead of utf8_unicode_ci to ensure the characters typed by your users won't give you errors.

utf8mb4 supports emojis for example, whereas utf8 might give you hundreds of encoding-related bugs like:

Incorrect string value: ‘\xF0\x9F\x98\x81…’ for column ‘data’ at row 1

Marwann
  • 163
  • 3
  • 14
  • This Answer (correctly) addresses issues with encoding of Emoji (and some of Chinese). But the Question seems to be focused on Collation. `utf8mb4_unicode_ci` treats (I think) all Emoji as equal. `utf8mb4_unicode_520_ci` gives an ordering to Emoji. – Rick James Jul 19 '19 at 16:18
6

Really, I tested saving values like 'é' and 'e' in column with unique index and they cause duplicate error on both 'utf8_unicode_ci' and 'utf8_general_ci'. You can save them only in 'utf8_bin' collated column.

And mysql docs (in http://dev.mysql.com/doc/refman/5.7/en/charset-applications.html) suggest into its examples set 'utf8_general_ci' collation.

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
vitalii
  • 443
  • 6
  • 10
  • 1
    I did a quick test on this, and it appears to be accurate. Both collations behave the same when it comes to a unique key on a column and values with tildes and the like. – MirroredFate Jun 30 '15 at 00:19
  • @MirroredFate OK, I should add there that column should have unique index for causing this error. It implies in my answer. – vitalii Jul 01 '15 at 07:09