0

我创建了一个具有 2 种不同字符集类型的表:utf8 和 latin1。

1)

CREATE TABLE `aaa` (
  `id` int(11) NOT NULL DEFAULT '0',
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2)

CREATE TABLE `aaa` (
  `id` int(11) NOT NULL DEFAULT '0',
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

然后我将值插入几行。当使用“解释”选择两个表时,我得到了不同的 key_len:

1)

mysql> explain select count(*) from aaa where name = "haha";

| 编号 | 选择类型 | 表| 类型 | 可能的键 | 关键 | key_len | 参考 | 行 | 额外 |

| 1 | 简单 | 啊!参考 | 姓名 | 姓名 | 258 | 常量 | 2 | 使用哪里;使用索引 |

一组中的 1 行(0.00 秒)

2)

mysql> explain select count(*) from aaa where name = "haha";

| 编号 | 选择类型 | 表| 类型 | 可能的键 | 关键 | key_len | 参考 | 行 | 额外 |

| 1 | 简单 | 啊!参考 | 姓名 | 姓名 | 第768章 常量 | 2 | 使用哪里;使用索引 |

一组中的 1 行(0.01 秒)

我不知道系统如何为使用不同字符集定义的类似表分配 key_len?

4

1 回答 1

2

由于 utf8 是一个多字节字符集,MySQL 需要为每个字符预留三个字节:

utf8, a UTF-8 encoding of the Unicode character set using one to three bytes per character. 

http://dev.mysql.com/doc/refman/5.6/en/charset-unicode.html

除此之外,似乎还有三个字节的开销。

于 2013-04-18T16:32:22.007 回答