两种情况:
使用默认排序规则:
CREATE TABLE IF NOT EXISTS `table` (
`name` varchar(255) collate utf8_general_ci NOT NULL,
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SELECT `name` FROM `table` ORDER BY `name`;
使用COLLATE
:
CREATE TABLE IF NOT EXISTS `table` (
`name` varchar(255) collate utf8_bin NOT NULL,
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SELECT `name` FROM `table` ORDER BY `name` COLLATE utf8_general_ci;
我需要从第一种情况更改为第二种情况,因为索引不区分大小写。订购仍然很重要。有实验性的排序规则,utf8_general_cs
但它需要特殊的编译。
这会对性能产生影响吗?
在我看来,如果MySQL
在 utf8 内部独立于排序规则存储文本字段,它不应该影响性能。
编辑:
使用情况下的解释输出与COLLATE
不使用时相同。
mysql> EXPLAIN SELECT *
-> FROM `table`
-> ORDER BY `name`
-> COLLATE utf8_general_ci;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | table | ALL | NULL | NULL | NULL | NULL | 5 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT *
-> FROM `table`
-> ORDER BY `name`;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | table | ALL | NULL | NULL | NULL | NULL | 5 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)