在utf8_general_ci和utf8_unicode_ci之间,在性能方面有什么不同吗?
当前回答
我想知道使用utf8_general_ci和utf8_unicode_ci之间的性能差异是什么,但我没有在互联网上找到任何基准测试,所以我决定自己创建基准测试。
我创建了一个非常简单的50万行表:
CREATE TABLE test(
ID INT(11) DEFAULT NULL,
Description VARCHAR(20) DEFAULT NULL
)
ENGINE = INNODB
CHARACTER SET utf8
COLLATE utf8_general_ci;
然后我通过运行这个存储过程,用随机数据填充它:
CREATE PROCEDURE randomizer()
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE random CHAR(20) ;
theloop: loop
SET random = CONV(FLOOR(RAND() * 99999999999999), 20, 36);
INSERT INTO test VALUES (i+1, random);
SET i=i+1;
IF i = 500000 THEN
LEAVE theloop;
END IF;
END LOOP theloop;
END
然后我创建了以下存储过程来测试简单的SELECT, SELECT with LIKE和排序(SELECT with ORDER BY):
CREATE PROCEDURE benchmark_simple_select()
BEGIN
DECLARE i INT DEFAULT 0;
theloop: loop
SELECT *
FROM test
WHERE Description = 'test' COLLATE utf8_general_ci;
SET i = i + 1;
IF i = 30 THEN
LEAVE theloop;
END IF;
END LOOP theloop;
END;
CREATE PROCEDURE benchmark_select_like()
BEGIN
DECLARE i INT DEFAULT 0;
theloop: loop
SELECT *
FROM test
WHERE Description LIKE '%test' COLLATE utf8_general_ci;
SET i = i + 1;
IF i = 30 THEN
LEAVE theloop;
END IF;
END LOOP theloop;
END;
CREATE PROCEDURE benchmark_order_by()
BEGIN
DECLARE i INT DEFAULT 0;
theloop: loop
SELECT *
FROM test
WHERE ID > FLOOR(1 + RAND() * (400000 - 1))
ORDER BY Description COLLATE utf8_general_ci LIMIT 1000;
SET i = i + 1;
IF i = 10 THEN
LEAVE theloop;
END IF;
END LOOP theloop;
END;
在上面的存储过程中使用了utf8_general_ci排序,但当然,在测试期间,我同时使用了utf8_general_ci和utf8_unicode_ci。
对于每种排序,我调用每个存储过程5次(utf8_general_ci调用5次,utf8_unicode_ci调用5次),然后计算平均值。
我的结果是:
benchmark_simple_select ()
utf8_general_ci: 9,957 ms utf8_unicode_ci: 10,271 ms
在这个基准测试中,使用utf8_unicode_ci比使用utf8_general_ci慢3.2%。
benchmark_select_like ()
使用utf8_general_ci: 11,441 ms 使用utf8_unicode_ci: 12,811 ms
在这个基准测试中,使用utf8_unicode_ci比使用utf8_general_ci慢12%。
benchmark_order_by ()
使用utf8_general_ci: 11,944 ms 使用utf8_unicode_ci: 12,887 ms
在这个基准测试中,使用utf8_unicode_ci比使用utf8_general_ci慢7.9%。
其他回答
请参阅mysql手册,Unicode字符集部分:
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.
因此,总的来说,utf_general_ci使用的比较集比utf_unicode_ci更小,更不正确(根据标准),后者应该实现整个标准。general_ci集将更快,因为要做的计算更少。
这篇文章描述得很好。
简而言之:utf8_unicode_ci使用Unicode标准中定义的Unicode排序算法,而utf8_general_ci是一种更简单的排序顺序,会导致“不太准确”的排序结果。
根据这篇文章,在MySQL 5.7上使用utf8mb4_general_ci而不是utf8mb4_unicode_ci有相当大的性能优势: https://www.percona.com/blog/2019/02/27/charset-and-collation-settings-impact-on-mysql-performance/
上面的注释表明没有理由使用utf8_general*。然而,对于日本人来说,这可能不是真的。
在MariaDB中,utf8mb4_ja_0900_as_cs不可用,因此必须使用unicode或通用选项之一。然而,unicode对待浊音和浊音等是一样的。例如,びよういん(美容师)被视为等同于びょういん(医院)。这显然是不正确的行为。
> select strcmp('が', 'か' collate utf8mb4_unicode_ci); #0
> strcmp('びよういん', 'びょういん' collate utf8mb4_unicode_520_ci); #0
而将军给出
> select strcmp('が', 'か' collate utf8mb4_general_ci); #1
换句话说,unicode对浊音假名和浊音假名一视同仁。在我看来,这并不可取。
编辑:使用uca1400_ai_cs可能更好,它在MariaDB的新版本中可用,并使上述排序正确。
一些细节(PL)
正如我们可以在这里(Peter Gulutzan)读到的,排序/比较波兰字母“Ł”(L与笔画- html esc: Ł)(小写:“ova”- html esc: ł) -我们有以下假设:
utf8_polish_ci Ł greater than L and less than M
utf8_unicode_ci Ł greater than L and less than M
utf8_unicode_520_ci Ł equal to L
utf8_general_ci Ł greater than Z
在波兰语中,字母Ł在字母L之后,在字母m之前,没有哪个编码更好或更差——这取决于你的需要。
推荐文章
- 警告用户/local/mysql/data目录不属于mysql用户
- 添加一个复合主键
- 无法添加或更新子行:外键约束失败
- 如何从本地机器mysqldump远程数据库
- 如何正确地创建复合主键- MYSQL
- 仅在Datetime列上按日期分组
- 在MySQL数据库中存储货币值的最佳数据类型
- 如何自定义'显示进程列表'在mysql?
- MySQL删除一些外键
- 用MySQL LEFT JOIN删除行
- MySQL - length() vs char_length()
- 使用PDO进行行计数
- 在Mac上安装MySQL后,使用ALTER USER语句重置MySQL root密码
- my.cnf文件在macOS上的位置
- 如何从Mac OS X上卸载MySQL ?