Mysql索引长度限制

1、myisam存储引擎mysql

1. 数据库版本:阿里云RDS MySQL5.1sql

mysql> select @@version;
+-------------------------------+
| @@version |
+-------------------------------+
| 5.1.61-Alibaba-rds-201404-log |
+-------------------------------+
1 row in set (0.00 sec)数据库

 

2. 测试的表结构信息服务器

mysql> show create table tb2\G
*************************** 1. row ***************************
Table: tb2
Create Table: CREATE TABLE `tb2` (
`a` varchar(255) DEFAULT NULL,
`b` varchar(255) DEFAULT NULL,
`c` varchar(255) DEFAULT NULL,
`d` varchar(1000) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)测试

 

3. 测试加索引阿里云

(1)添加单列索引,可以添加成功(报出warning),但实际添加的是前缀索引。.net

mysql> alter table tb2 add index idx1 (d);
Query OK, 0 rows affected, 2 warnings (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0code

mysql> show warnings;
+---------+------+----------------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------------+
| Warning | 1071 | Specified key was too long; max key length is 1000 bytes |
| Warning | 1071 | Specified key was too long; max key length is 1000 bytes |
+---------+------+----------------------------------------------------------+
2 rows in set (0.00 sec)orm

表结构信息:排序

mysql> show create table tb2\G
*************************** 1. row ***************************
Table: tb2
Create Table: CREATE TABLE `tb2` (
`a` varchar(255) DEFAULT NULL,
`b` varchar(255) DEFAULT NULL,
`c` varchar(255) DEFAULT NULL,
`d` varchar(1000) DEFAULT NULL,
KEY `idx1` (`d`(333))
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

 

(2)添加组合索引,会执行失败。

mysql> alter table tb2 add index idx1 (a,b);
ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes

4. 分析

myisam存储引擎在建立索引的时候,索引键长度是有一个较为严格的长度限制的,全部索引键最大长度总和不能超过1000,并且不是实际数据长度的总和,而是索引键字段定义长度的总和。

主要字符集的计算方式:
latin1 = 1 byte = 1 character
uft8 = 3 byte = 1 character
gbk = 2 byte = 1 character

 

2、innodb存储引擎

1. 表结构信息:

mysql> create table tb1 (a varchar(255), b varchar(255), c varchar(255), d varchar(1000));
Query OK, 0 rows affected (0.01 sec)

 

2. 添加组合索引,报出waring,实际在某个单列上添加的是前缀索引

mysql> alter table tb1 add index idx1(a,b,c,d);

Query OK, 0 rows affected, 2 warnings (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> show warnings;
+---------+------+---------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------+
| Warning | 1071 | Specified key was too long; max key length is 767 bytes |
| Warning | 1071 | Specified key was too long; max key length is 767 bytes |
+---------+------+---------------------------------------------------------+
2 rows in set (0.00 sec)

 

3. 添加单列索引,报出waring,实际添加的是前缀索引

mysql> alter table tb1 add index idx2(d);
Query OK, 0 rows affected, 2 warnings (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> show warnings;
+---------+------+---------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------+
| Warning | 1071 | Specified key was too long; max key length is 767 bytes |
| Warning | 1071 | Specified key was too long; max key length is 767 bytes |
+---------+------+---------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show create table tb1\G
*************************** 1. row ***************************
Table: tb1
Create Table: CREATE TABLE `tb1` (
`a` varchar(255) DEFAULT NULL,
`b` varchar(255) DEFAULT NULL,
`c` varchar(255) DEFAULT NULL,
`d` varchar(1000) DEFAULT NULL,
KEY `idx1` (`a`,`b`,`c`,`d`(255)),
KEY `idx2` (`d`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

 

4. 分析:

默认状况下,InnoDB 引擎单一字段索引的长度最大为 767 字节,一样的,前缀索引也有一样的限制。当使用 UTF-8 字符集,每个字符使用 3 字节来存储,在 TEXT 或者 VARCHAR 类型的字段上创建一个超过 255 字符数的前缀索引时就会遇到问题。能够启用服务器选项 innodb_large_prefix 使得这个限制增长到 3072 字节,并且表的 row_format 须要使用 compressed 或者 dynamic。

 

3、使用前缀索引带来的风险:

INNODB的索引会限制单独Key的最大长度为767字节,超过这个长度必须创建小于等于767字节的前缀索引。

此外,BLOB和TEXT类型的列只能建立前缀索引。

前缀索引能提升索引创建速度和检索速度,可是下面状况是没法使用前缀索引的:

  • 索引覆盖扫描
  • 经过索引的排序(order by, group by)

仍是在上面的测试表上:

mysql> explain select * from tb1 order by d;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | tb1 | ALL | NULL | NULL | NULL | NULL | 5 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

mysql> explain select * from tb1 group by d; +----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+ | 1 | SIMPLE | tb1 | ALL | NULL | NULL | NULL | NULL | 5 | Using temporary; Using filesort | +----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+ 1 row in set (0.00 sec)

相关文章
相关标签/搜索