MySQL 5.7.12 开始

https://dev.mysql.com/doc/refman/5.7/en/create-index.html

测试结果

create table  booboo_db1.t20211018 (id int primary key, a varchar(5000));
alter table booboo_db1.t20211018 add index idx_a768 (a(768));
0 row(s) affected Records: 0 Duplicates: 0 Warnings: 0

alter table booboo_db1.t20211018 add index idx_a769 (a(769));
0 row(s) affected, 1 warning(s): 1071 Specified key was too long; max key length is 3072 bytes Records: 0 Duplicates: 0 Warnings: 1

官方说明

Column Prefix Key Parts

For string 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:

As of MySQL 5.7.17, if a specified index prefix exceeds the maximum column data type size, CREATE INDEX handles the index as follows:

  • For a nonunique index, either an error occurs (if strict SQL mode is enabled), or the index length is reduced to lie within the maximum column data type size and a warning is produced (if strict SQL mode is not enabled).
  • For a unique index, an error occurs regardless of SQL mode because reducing the index length might enable insertion of nonunique entries that do not meet the specified uniqueness requirement.

The statement shown here creates an index using the first 10 characters of the name column (assuming that name has a nonbinary string type):

CREATE INDEX part_of_name ON customer (name(10));

If names in the column usually differ in the first 10 characters, lookups performed using this index should not be much slower than using an index created from the entire name column. Also, using column prefixes for indexes can make the index file much smaller, which could save a lot of disk space and might also speed up INSERT operations.