utf8_general_ci和utf8_unicode_ci有什么区别? [英] What's the difference between utf8_general_ci and utf8_unicode_ci?

查看:108
本文介绍了utf8_general_ci和utf8_unicode_ci有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

utf8_general_ciutf8_unicode_ci之间,在性能方面有什么区别吗?

Between utf8_general_ci and utf8_unicode_ci, are there any differences in terms of performance?

推荐答案

这两个归类均适用于UTF-8字符编码.区别在于文本的排序和比较方式.

These two collations are both for the UTF-8 character encoding. The differences are in how text is sorted and compared.

注意:在MySQL中,您必须使用 utf8mb4 而不是utf8.令人困惑的是,utf8是早期MySQL版本中有缺陷的UTF-8实现,仅用于向后兼容.固定版本的名称为utf8mb4.

Note: In MySQL you have to use utf8mb4 rather than utf8. Confusingly, utf8 is a flawed UTF-8 implementation from early MySQL versions which remains only for backward compatibility. The fixed version was given the name utf8mb4.

注意:较新版本的MySQL已更新了Unicode排序规则,这些名称可在以下名称下使用: utf8mb4_0900_ai_ci 适用于基于Unicode 9.0的等效规则-无等效 _general 变体.现在阅读此书的人可能应该使用这些较新的排序规则中的一种,而不是 _unicode _general. 如果您可以改用一种较新的排序规则,则下面不再写很多内容.

Note: Newer versions of MySQL have updated Unicode sorting rules, available under names such as utf8mb4_0900_ai_ci for equivalent rules based on Unicode 9.0 - and with no equivalent _general variant. People reading this now should probably use one of these newer collations instead of either _unicode or _general. Much of what's written below is not of much interest anymore if you can use one of the newer collations instead.

主要区别

  • utf8mb4_unicode_ci基于用于通用排序和比较的官方Unicode规则,可以在多种语言中进行精确排序.

  • utf8mb4_unicode_ci is based on the official Unicode rules for universal sorting and comparison, which sorts accurately in a wide range of languages.

utf8mb4_general_ci是一组简化的排序规则,旨在尽力而为,同时采用许多旨在提高速度的捷径.它不遵循Unicode规则,在某些情况下(例如,使用特定语言或字符时)会导致不希望的排序或比较.

utf8mb4_general_ci is a simplified set of sorting rules which aims to do as well as it can while taking many short-cuts designed to improve speed. It does not follow the Unicode rules and will result in undesirable sorting or comparison in some situations, such as when using particular languages or characters.

在现代服务器上,这种性能提升几乎可以忽略不计.它是在服务器仅占当今计算机CPU性能的一小部分的时候设计的.

On modern servers, this performance boost will be all but negligible. It was devised in a time when servers had a tiny fraction of the CPU performance of today's computers.

utf8mb4_unicode_ci优于utf8mb4_general_ci

Benefits of utf8mb4_unicode_ci over utf8mb4_general_ci

utf8mb4_unicode_ci使用Unicode规则进行排序和比较,它采用相当复杂的算法来对多种语言和使用多种特殊字符进行正确排序.这些规则需要考虑到特定于语言的约定.并不是每个人都按照我们称为字母顺序"的顺序对字符进行排序.

utf8mb4_unicode_ci, which uses the Unicode rules for sorting and comparison, employs a fairly complex algorithm for correct sorting in a wide range of languages and when using a wide range of special characters. These rules need to take into account language-specific conventions; not everybody sorts their characters in what we would call 'alphabetical order'.

就拉丁(即欧洲")语言而言,Unicode排序与MySQL中简化的utf8mb4_general_ci排序之间没有太大差异,但仍然存在一些差异:

As far as Latin (ie "European") languages go, there is not much difference between the Unicode sorting and the simplified utf8mb4_general_ci sorting in MySQL, but there are still a few differences:

  • 例如,Unicode排序规则将ß"(如"ss")和Œ"(如"OE")进行排序,因为使用这些字符的人们通常会希望这样做,而utf8mb4_general_ci将它们排序为单个字符(大概是分别是"s"和"e".

  • For examples, the Unicode collation sorts "ß" like "ss", and "Œ" like "OE" as people using those characters would normally want, whereas utf8mb4_general_ci sorts them as single characters (presumably like "s" and "e" respectively).

某些Unicode字符被定义为可忽略,这意味着它们不应计入排序顺序,而比较应继续到下一个字符. utf8mb4_unicode_ci可以正确处理这些问题.

Some Unicode characters are defined as ignorable, which means they shouldn't count toward the sort order and the comparison should move on to the next character instead. utf8mb4_unicode_ci handles these properly.

在非拉丁语言(例如亚洲语言或具有不同字母的语言)中,Unicode排序和简化的utf8mb4_general_ci排序之间可能会有很多 more 差异. utf8mb4_general_ci的适用性在很大程度上取决于所使用的语言.对于某些语言来说,这是远远不够的.

In non-latin languages, such as Asian languages or languages with different alphabets, there may be a lot more differences between Unicode sorting and the simplified utf8mb4_general_ci sorting. The suitability of utf8mb4_general_ci will depend heavily on the language used. For some languages, it'll be quite inadequate.

您应该使用什么?

几乎没有理由再使用utf8mb4_general_ci了,因为我们已经抛弃了CPU速度足够低而性能差异很重要的观点.您的数据库几乎肯定会受到除此以外的其他瓶颈的限制.

There is almost certainly no reason to use utf8mb4_general_ci anymore, as we have left behind the point where CPU speed is low enough that the performance difference would be important. Your database will almost certainly be limited by other bottlenecks than this.

过去,有些人建议使用utf8mb4_general_ci,除非精确的排序对于证明性能成本合理重要.如今,这种性能成本几乎消失了,开发人员正在更加认真地对待国际化.

In the past, some people recommended to use utf8mb4_general_ci except when accurate sorting was going to be important enough to justify the performance cost. Today, that performance cost has all but disappeared, and developers are treating internationalization more seriously.

有一个论点是,如果速度对您而言比准确性更重要,那么您可能根本不做任何排序.如果不需要精确的算法,则可以使算法更快.因此,utf8mb4_general_ci是一种折衷,出于速度原因可能不需要,而且出于准确性原因也可能不适合.

There's an argument to be made that if speed is more important to you than accuracy, you may as well not do any sorting at all. It's trivial to make an algorithm faster if you do not need it to be accurate. So, utf8mb4_general_ci is a compromise that's probably not needed for speed reasons and probably also not suitable for accuracy reasons.

我要补充的另一件事是,即使您知道您的应用程序仅支持英语,它仍可能需要处理人们的名字,该名字通常可以包含在其他语言中同样重要的字符.正确排序.对所有内容使用Unicode规则有助于让您放心,非常聪明的Unicode人员为使排序正常工作而非常努力.

One other thing I'll add is that even if you know your application only supports the English language, it may still need to deal with people's names, which can often contain characters used in other languages in which it is just as important to sort correctly. Using the Unicode rules for everything helps add peace of mind that the very smart Unicode people have worked very hard to make sorting work properly.

部件的含义

首先,ci用于不区分大小写的排序和比较.这意味着它适用于文本数据,并且大小写并不重要.其他类型的排序规则是cs(区分大小写),用于区分大小写的文本数据,和bin,用于区分编码的点对点匹配,适用于真正编码二进制数据的字段(例如,包括Base64).区分大小写的排序会导致一些奇怪的结果,并且区分大小写的比较可能导致重复值仅在字母大小写上有所不同,因此区分大小写的排序规则对文本数据不受欢迎-如果大小写对您而言很重要,则否则会出现标点符号可忽略的情况等等也可能很重要,而二进制排序规则可能更合适.

Firstly, ci is for case-insensitive sorting and comparison. This means it's suitable for textual data, and case is not important. The other types of collation are cs (case-sensitive) for textual data where case is important, and bin, for where the encoding needs to match, bit for bit, which is suitable for fields which are really encoded binary data (including, for example, Base64). Case-sensitive sorting leads to some weird results and case-sensitive comparison can result in duplicate values differing only in letter case, so case-sensitive collations are falling out of favor for textual data - if case is significant to you, then otherwise ignorable punctuation and so on is probably also significant, and a binary collation might be more appropriate.

接下来,unicodegeneral指的是特定的排序和比较规则-尤其是规范化或比较文本的方式. utf8mb4字符编码有很多不同的规则集,其中unicodegeneral是两个试图在所有可能的语言中都能正常工作的规则,而不是一种特定的语言.这两组规则之间的差异是此答案的主题.请注意,unicode使用Unicode 4.0中的规则. MySQL的最新版本使用Unicode 5.2中的规则添加规则集unicode_520,并使用Unicode 9.0中的规则添加0900(除去"unicode_"部分).

Next, unicode or general refers to the specific sorting and comparison rules - in particular, the way text is normalized or compared. There are many different sets of rules for the utf8mb4 character encoding, with unicode and general being two that attempt to work well in all possible languages rather than one specific one. The differences between these two sets of rules are the subject of this answer. Note that unicode uses rules from Unicode 4.0. Recent versions of MySQL add the rulesets unicode_520 using rules from Unicode 5.2, and 0900 (dropping the "unicode_" part) using rules from Unicode 9.0.

最后,utf8mb4当然是内部使用的字符编码.在这个答案中,我只谈论基于Unicode的编码.

And lastly, utf8mb4 is of course the character encoding used internally. In this answer I'm talking only about Unicode based encodings.

这篇关于utf8_general_ci和utf8_unicode_ci有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆