加入收藏 | 设为首页 | 会员中心 | 我要投稿 商洛站长网 (https://www.0914zz.com/)- AI应用、CDN、边缘计算、云计算、物联网!
当前位置: 首页 > 数据库 > MySql > 正文

索引长度过长 ERROR 1071 (42000): Specified key was too long; max k

发布时间:2020-09-01 05:10:37 所属栏目:MySql 来源:互联网
导读:divid=article_contentclass=article_contentcsdn-tracking-statisticstracking-clickstyle=overflow:hidden;data-dsm=postdata-mod=popu_519

<div id="article_content" class="article_content csdn-tracking-statistics tracking-click" style="overflow: hidden;" data-dsm="post" data-mod="popu_519">
<div class="htmledit_views">
<h1 style="color: #333333; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 14pt;">1.<span style="font-size: 18pt;">发现问题
<p style="color: #333333; font-family: Arial; font-size: 14px; line-height: 26px;"> 今天在修改innodb表的某个列的长度时,报如下错误:


<p style="color: #333333; font-family: Arial; font-size: 14px; line-height: 26px;">


<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">[html] <a class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="#" target="_self">view plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"> <a class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="#" target="_self">copy
<div style="position: absolute; left: 540px; top: 879px; width: 16px; height: 16px; z-index: 99;">
<span class="tracking-ad" data-mod="popu_169"> <a class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="#" target="_self">print<a class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;" href="#" target="_self">?

  1. ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes

2.分析问题

2.1 问题原因分析

[html]

  1. ***************************1.row***************************
  2. Non_unique:1
  3. Seq_in_index:1
  4. Collation:A
  5. Sub_part:NULL
  6. Null:YES
  7. Comment:
  8. 1rowinset(0.00sec)
 show index from test2G;
*************************** 1. row ***************************
        Table: test2
   Non_unique: 1
     Key_name: id
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
1 row in set (0.00 sec)
##删除id列的索引后, alter table test2 modify操作可以正常执行

2.2 关于mysql索引长度限制

[html]

  1. "ForCHAR,VARCHAR,BINARY,andVARBINARYcolumns,indexescanbecreatedthatuSEOnlytheleadingpartofcolumnvalues,usingcol_name(length)syntaxtospecifyanindexprefixlength.
  2. Prefixescanbeupto1000byteslong(767bytesforInnoDBtables).Notethatprefixlimitsaremeasuredinbytes,whereastheprefixlengthinCREATETABLEstatementsisinterpretedasnumberofcharacters..."对于myisam和innodb存储引擎,prefixes的长度限制分别为1000bytes和767bytes。注意prefix的单位是bytes,但是建表时我们指定的长度单位是字符。
  3. Autf8charactercanuseupto3bytes.Henceyoucannotindexcolumnsorprefixesofcolumnslongerthan333(MyISAM)or255(InnoDB)utf8characters.以utf8字符集为例,一个字符占3个bytes。因此在utf8字符集下,对myisam和innodb存储引擎创建索引的单列长度不能超过333个字符和255个字符
>从5.6的官方文档中我们能找到如下双引号中解释
"For CHAR,and VARBINARY columns,indexes can be created that use only the leading part of column values,using col_name(length) syntax to specify an index prefix length.
...
Prefixes can be up to 1000 bytes long (767 bytes for InnoDB tables). Note that prefix limits are measured in bytes,whereas the prefix length in CREATE TABLE statements is interpreted as number of characters ...">>>对于myisam和innodb存储引擎,prefixes的长度限制分别为1000 bytes和767 bytes。注意prefix的单位是bytes,但是建表时我们指定的长度单位是字符。

A utf8 character can use up to 3 bytes. Hence you cannot index columns or prefixes of columns longer than 333 (MyISAM) or 255 (InnoDB) utf8 characters. >>以utf8字符集为例,一个字符占3个bytes。因此在utf8字符集下,对myisam和innodb存储引擎创建索引的单列长度不能超过333个字符和255个字符


<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">[html] <a class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="#" target="_self">copy
<div style="position: absolute; left: 540px; top: 1860px; width: 16px; height: 16px; z-index: 99;">
<span class="tracking-ad" data-mod="popu_169"> <a class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="#" target="_self">?

  1. ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes
##对于innodb表,索引列长度大于255时,会报错。

<p style="font-family: Arial; font-size: 14px; line-height: 26px;"> 从上面可以看出,mysql 在创建单列索引的时候对列的长度是有限制的 myisam和innodb存储引擎下长度限制分别为1000 bytes和767 bytes。(注意bytes和character的区别)


2) 组合索引长度限制
<p style="font-family: Arial; font-size: 14px; line-height: 26px;"> 对于innodb存储引擎,多列索引的长度限制如下:


<p style="font-family: Arial; font-size: 14px; line-height: 26px;">每个列的长度不能大于767 bytes;所有组成索引列的长度和不能大于3072 bytes


<p style="font-family: Arial; font-size: 14px; line-height: 26px;">参考如下例子(下面默认使用的使用innodb存储引擎,smallint 占2个bytes,timestamp占4个bytes,utf8字符集。utf8字符集下,一个character占三个byte)


<p style="font-family: Arial; font-size: 14px; line-height: 26px;">


<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">[html] <a class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="#" target="_self">copy
<div style="position: absolute; left: 540px; top: 2279px; width: 16px; height: 16px; z-index: 99;">
<span class="tracking-ad" data-mod="popu_169"> <a class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="#" target="_self">?

  1. QueryOK,0rowsaffected(0.11sec)=
  2. mysqldroptabletest3;
  3. ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes
  4. ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis3072bytes
  5. QueryOK,0rowsaffected(0.09sec)
 create table test3(id varchar(255),key(id));
Query OK,0 rows affected (0.11 sec)  >>
mysql> drop table test3;
Query OK,0 rows affected (0.03 sec)

(编辑:商洛站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读