Mysql::Error: 指定的键太长;最大密钥长度为 1000 字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3489041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:52:44  来源:igfitidea点击:

Mysql::Error: Specified key was too long; max key length is 1000 bytes

mysqlsqlruby-on-railsindexingmysql-error-1071

提问by amaseuk

script/generate acts_as_taggable_on_migration
rake db:migrate

causes

原因

Mysql::Error: Specified key was too long; max key length is 1000 bytes: CREATE  INDEX `index_taggings_on_taggable_id_and_taggable_type_and_context` ON `taggings` (`taggable_id`, `taggable_type`, `context`)

What should I do?

我该怎么办?

Here is my database encoding:

这是我的数据库编码:

mysql> SHOW VARIABLES LIKE 'character\_set\_%';
+--------------------------+--------+
| Variable_name            | Value  |
+--------------------------+--------+
| character_set_client     | latin1 | 
| character_set_connection | latin1 | 
| character_set_database   | utf8   | 
| character_set_filesystem | binary | 
| character_set_results    | latin1 | 
| character_set_server     | latin1 | 
| character_set_system     | utf8   | 
+--------------------------+--------+
7 rows in set (0.00 sec)

回答by OMG Ponies

This is solely a MySQL issue -

这仅仅是一个 MySQL 问题 -

MySQL has different engines - MyISAM, InnoDB, Memory...

MySQL 有不同的引擎——MyISAM、InnoDB、Memory...

MySQL has different limits on the amount of space you can use to define indexes on column(s) - for MyISAM it's 1,000 bytes; it's 767 for InnoDB. And the data type of those columns matters - for VARCHAR, it's 3x so an index on a VARCHAR(100)will take 300 of those bytes (because 100 characters * 3 = 300).

MySQL 对可用于定义列索引的空间量有不同的限制——对于 MyISAM,它是 1,000 字节;InnoDB 是 767。并且这些列的数据类型很重要 - 对于VARCHAR,它是 3 倍,因此 a 上的索引VARCHAR(100)将占用 300 个字节(因为 100 个字符 * 3 = 300)。

To accommodate some indexing when you hit the ceiling value, you can define the index with regard to portions of the column data type:

为了在达到上限时容纳一些索引,您可以定义关于列数据类型部分的索引:

CREATE INDEX example_idx ON YOUR_TABLE(your_column(50))

Assuming that your_columnis VARCHAR(100), the index in the example above will only be on the first 50 characters. Searching for data beyond the 50th character will not be able to use the index.

假设your_columnVARCHAR(100),上面例子中的索引只会出现在前 50 个字符上。搜索超过第 50 个字符的数据将无法使用索引。

回答by Pabinator

This seems to be a bug that was reported here: http://bugs.mysql.com/bug.php?id=4541

这似乎是这里报告的错误:http: //bugs.mysql.com/bug.php?id=4541

If you have tried all the answers on this post and still getting the error, you may want to try to run this command on your SQL query window.

如果您已经尝试了这篇文章中的所有答案,但仍然出现错误,您可能需要尝试在 SQL 查询窗口上运行此命令。

set GLOBAL storage_engine='InnoDb';

回答by user3410311

if this error occur in some proccess like migration, it could be solved by changing config file of MySql (*.ini)

如果在迁移等过程中出现此错误,可以通过更改MySql的配置文件(*.ini)来解决

default-storage-engine=InnoDB

回答by BitKFu

I think one of your fields is a varchar with more than 1000 chars. e.g. context?

我认为你的一个字段是一个超过 1000 个字符的 varchar。例如上下文?

Think about the meaning of an index. It's quick access to a row when all your indexed fields are within the where clause. If an index is to long (in case of mysql more than 1000 bytes), it makes no sense to use an index, because it's probably slower than accessing the complete table with a full table scan.

想想索引的含义。当所有索引字段都在 where 子句中时,可以快速访问一行。如果索引很长(如果 mysql 超过 1000 字节),使用索引是没有意义的,因为它可能比使用全表扫描访问完整表慢。

I would suggest to shorten the index, e.g to taggable_id and taggable_type, if those both are the shorter once.

我建议缩短索引,例如,taggable_id 和taggable_type,如果它们都较短一次。

Cheers - Gerhard

干杯 - 格哈德