When creating a table, I encountered an interesting problem. The message "Specified key was too long; max key length is 767 bytes" appeared. From the description, it seems that the key is too long and exceeds the specified limit of 767 bytes. Below is the table structure that causes the problem CREATE TABLE `test_table` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(1000) NOT NULL DEFAULT '', `link` varchar(1000) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; We can see that for name, we set the length to 1000 variable characters. Because utf8mb4 encoding is used, its size becomes 1000 * 4 > 767 Interested students can test it Solution 1
my.cnf configuration set global innodb_large_prefix=on; set global innodb_file_per_table=on; set global innodb_file_format=BARRACUDA; set global innodb_file_format_max=BARRACUDA; The reason for the above 3072 bytes is as follows We know that the default size of an InnoDB page is 16k. Because it is a Btree organization, a page on a leaf node is required to contain at least two records (otherwise it will degenerate into a linked list). Solution 2 When creating a table, add row_format=DYNAMIC CREATE TABLE `test_table` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '', `link` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `name` (`name`) )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 row_format=DYNAMIC; The function of this parameter is as follows MySQL indexes only support 767 bytes, and each character in utf8mb4 takes up 4 bytes, so the maximum length of the index can only be 191 characters, that is, varchar(191). If you want to use a larger field, MySQL needs to be set to support data compression and modify the table attribute row_format ={DYNAMIC|COMPRESSED} This is the end of this article about the solution to the problem of MySQL index being too long. For more information about MySQL index being too long, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A collection of common uses of HTML meta tags
>>: Introduction to the use of em in elastic layout in CSS3: How many pixels is 1em?
Table of contents 1. Introduction to MySQL Index ...
This article shares the specific code of jQuery t...
Previously, I introduced the use of the charAt() ...
Install GeoIP on Linux yum install nginx-module-g...
Installing MySQL 5.7 from TAR.GZ on Mac OS X Comp...
An absolute URL is used to represent all the conte...
Uninstall tomcat9 1. Since the installation of To...
When compiling and installing Nginx, some modules...
The MySQL version used in this example is mysql-8...
Syntax format: row_number() over(partition by gro...
First of all, this post is dedicated to Docker no...
Table of contents Overview Install Gulp.js Create...
A large part of data management is searching, and...
Lots of links You’ve no doubt seen a lot of sites ...
How to find slow SQL statements in MySQL? This ma...