[MySQL] utf8_general_ci と utf8_unicode_ci の違い

データベースのコレーション(Collation: 適合順序)について。

これまであまり深く考えずにutf8_general_ciを設定することが多かったけど、書籍やネットで見かけるサンプルにはutf8_unicode_ciを使っているものも少なくない。これらの振る舞いの違いは何なのか、一度しっかり押さえておきたい。

とりあえず公式サイトのマニュアルを探したら記述が見つかった。

MySQL :: MySQL 3.23, 4.0, 4.1 Reference Manual :: 9.1.12.1 Unicode Character Sets
http://dev.mysql.com/doc/refman/4.1/en/charset-unicode-sets.html

For any Unicode character set, operations performed using the xxx_general_ci collation are faster than those for the xxx_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.

抜粋拙訳

  • xxx_unicode_ciよりもxxx_general_ciのほうが処理が速い。
  • たとえばutf8_general_ciはutf_8_unicode_ciよりも速いけど精度は落ちる。
  • その理由はutf8_unciode_ciは拡張したマッピングをするから (1つの文字が他の文字の組み合わせと等しいか比較する)。

もっと具体的に言ってくれないとわからない・・・

参考サイト

ネットを探すと解りやすく説明したサイトが見つかった。

utf8_general_ci
– 英字の大文字小文字は区別しない。
– 全角半角は区別する。
utf8_unicode_ci
– 英字の大文字小文字は区別しない。
– 全角半角も区別しない。
– さらに濁音、半濁音、ひらがな、カタカナも区別しない。
– 「は」で検索すると「は」「ば」「ぱ」「ハ」「バ」「パ」「ハ」がヒットする。

utf8_general_ciとutf8_unicode_ci – 技術メモ
http://seesaawiki.jp/notepad/d/utf8_general_ci%A4%C8utf8_unicode_ci

問題
select * from member where namae like ‘%サトウ%’;
こんなSQLで、namaeがサトウ、サトウ、さとう、サトウ(一部半角)何でもマッチさせたい!

【MySQL】大文字小文字、全角半角区別しないでマッチする検索をしたい at softelメモhttp://www.softel.co.jp/blogs/tech/archives/1877