MySQL MyIsam 存储引擎索引长度限制测试记录

来源:互联网 发布:mac 照片 储存 编辑:程序博客网 时间:2024/05/29 09:43

 

MySQL MyIsam 存储引擎在创建索引的时候,索引键长度是有一个较为严格的长度限制的,所有索引键最大长度总和不能超过1000,而且不是实际数据长度的总和,而是索引键字段定义长度的总和。下面做个简单的测试,记录一下。

root@sky:~# mysql -u sky -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 44
Server version: 5.0.51a-log MySQL Community Server (GPL)

Type ‘help;’ or ‘/h’ for help. Type ‘/c’ to clear the buffer.

sky@127.0.0.1 : (none) 05:23:08> use test;
Database changed
sky@127.0.0.1 : test 05:23:11>
sky@127.0.0.1 : test 05:23:12>

先创建一个MyIsam表,字符集选择latin1,三个字段均设置为varchar 255,:
sky@127.0.0.1 : test 05:23:12> create table test_ind
-> (a varchar(255),
-> b varchar(255),
-> c varchar(255)
-> ) engine=myisam charset=latin1;
Query OK, 0 rows affected (0.01 sec)

创建效果:
sky@127.0.0.1 : test 05:23:32> show create table test_ind/G
*************************** 1. row ***************************
Table: test_ind
Create Table: CREATE TABLE `test_ind` (
`a` varchar(255) default NULL,
`b` varchar(255) default NULL,
`c` varchar(255) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

三个字段联合索引(长度应该在1000以内)

sky@127.0.0.1 : test 05:23:41> create index test_a_b_c_ind on test_ind(a,b,c);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

我们看到,创建成功了。

下面我们做一次字符集转换,将字符集转换成utf8

sky@127.0.0.1 : test 05:25:54> alter table test_ind convert to charset utf8;
ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes
sky@127.0.0.1 : test 05:26:24>
sky@127.0.0.1 : test 05:28:03> show create table test_ind/G
*************************** 1. row ***************************
Table: test_ind
Create Table: CREATE TABLE `test_ind` (
`a` varchar(255) default NULL,
`b` varchar(255) default NULL,
`c` varchar(255) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

结果报错了,查看表实际情况也确实没有成功,仍然是latin1的字符集。

我们现drop掉索引,再转换字符集。

sky@127.0.0.1 : test 05:28:10> drop index test_a_b_c_ind on test_ind;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

sky@127.0.0.1 : test 05:28:15> alter table test_ind convert to charset utf8;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
sky@127.0.0.1 : test 05:28:20> show create table test_ind/G
*************************** 1. row ***************************
Table: test_ind
Create Table: CREATE TABLE `test_ind` (
`a` varchar(255) default NULL,
`b` varchar(255) default NULL,
`c` varchar(255) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
转换很顺利,成功了。

现在再创建索引看看效果怎样:

sky@127.0.0.1 : test 05:28:36> create index test_a_b_c_ind on test_ind(a,b,c);
ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes

失败,减少索引键字段
sky@127.0.0.1 : test 05:28:54> create index test_a_b_c_ind on test_ind(a,b);
ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes

还是失败,继续减少
sky@127.0.0.1 : test 05:29:00> create index test_a_b_c_ind on test_ind(a);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

ok,总算成功了。

最后再看看其他存储引擎有没有这个限制呢?就看当前用的最广泛的存储引擎之一Innodb吧:

sky@127.0.0.1 : test 05:29:03> drop index test_a_b_c_ind on test_ind;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

sky@127.0.0.1 : test 05:29:54>
sky@127.0.0.1 : test 05:29:58>
sky@127.0.0.1 : test 05:30:11> alter table test_ind engine=innodb;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
sky@127.0.0.1 : test 05:31:15> show create table test_ind/G
*************************** 1. row ***************************
Table: test_ind
Create Table: CREATE TABLE `test_ind` (
`a` varchar(255) default NULL,
`b` varchar(255) default NULL,
`c` varchar(255) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

sky@127.0.0.1 : test 05:31:23> create index test_a_b_c_ind on test_ind(a,b,c);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

很顺利,创建成功,Innodb是没有这个限制的。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝商家收款不发货怎么办 淘宝申请退款商家拒绝怎么办 一件代发找不到供货商怎么办 淘宝京东兼职上当怎么办 退货时快递丢件怎么办 淘宝店铺代销1688有订单怎么办 供应商已解除合作无法代销怎么办 被代运营骗了怎么办 被淘宝运营骗了怎么办 淘宝运营公司骗了怎么办 被金融公司骗了怎么办 天猫品牌方投诉怎么办 淘宝卖家售假被扣了12分怎么办? 淘宝店被投诉了怎么办 淘宝商品被投诉侵权怎么办 淘宝小二胡乱判怎么办 淘宝卖家不干了怎么办 花呗剩下的钱怎么办 蚂蚁花呗无法使用怎么办 淘宝不能用花呗怎么办 淘宝号给冻结了怎么办 淘宝买家号封了怎么办 拼多多商家盗图怎么办 被拼多多盗图了怎么办 淘宝盗用图片被投诉怎么办 淘宝别人盗用我的图片怎么办 淘宝盗图申诉原图过大怎么办 淘宝别人举报我盗用图片怎么办 淘宝卖家被投诉盗图怎么办 淘宝卖家被投诉卖假货怎么办 淘宝买到假货卖家不承认怎么办 被投诉盗图扣2分怎么办 拼多多盗淘宝图怎么办 微信视频清理了怎么办 牛仔短裤买大了怎么办 淘宝图片打开变大了怎么办 同城换公司社保怎么办 劳务不给交社保怎么办 好多工厂外包工不交社保怎么办 外包公司没有交社保怎么办 外包公司不给交社保怎么办