博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql索引长度的一些限制
阅读量:4626 次
发布时间:2019-06-09

本文共 5282 字,大约阅读时间需要 17 分钟。

一、myisam存储引擎

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

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),但实际添加的是前缀索引。

mysql> alter table tb2 add index idx1 (d);

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

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)

表结构信息:

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

 

二、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。

 

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

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)

转载于:https://www.cnblogs.com/yuyue2014/p/4339090.html

你可能感兴趣的文章
内容安全策略(CSP)_防御_XSS_攻击的好助手
查看>>
获取URL中的参数
查看>>
宝塔面板安装swoole扩展
查看>>
HDOJ_1061_Rightmost Digit
查看>>
【小笨鸟看JDK1.7集合源码之三】LinkedList源码剖析
查看>>
bfs,dfs区别
查看>>
Javascript端加密java服务端解密
查看>>
xml文件中引号如何处理
查看>>
Centos 下 Jenkins2.6 + Git + Maven Shell一件部署与备份
查看>>
MVC原理
查看>>
Java中堆内存和栈内存详解
查看>>
网络编程
查看>>
C# 访问USB(HID)设备方法 (转)
查看>>
linux 配置svn服务器+使用+注意事项
查看>>
关于JAVA并发编程你需要知道的——语言篇
查看>>
Spring MVC 中 HandlerInterceptorAdapter的使用
查看>>
druid 数据源 使用属性文件的一个坑
查看>>
[na]数据包由于isp不稳定丢包-seq&ack
查看>>
浅谈Web前端浏览器兼容问题
查看>>
用命令行在github新建一个项目
查看>>