What are the general parameters for determining whether the key_len for a mysql index is ‘too long’?
For example, for longer VARCHAR columns, having a key would be longer. In one case I have an index with key_len=767. In which case should I do something like:
ADD INDEX (title(50));
If your table holds many rows and/or if you execute lots of
SELECT ... ORDER BYqueries, having index(es) on the field(s) involved will speed up the SELECT queries.However, it will slow down inserts, writes and updates on these fields, because the indexes need to be updated as well.
Every index on a table uses disk space: if you’re really short on disk space, use a smaller key_len. But your queries will execute slower.
Imagine a table with only one column, and only the first 3 characters are indexed. The table contains:
Timo
Timothée
Tim
Timothy
Timmo
This query will use the index, but since its content is partially indexed, it will need extra processing to do a complete sort, and the execution will take longer.
MySQL indexes can be up to 1000 bytes long (767 bytes for InnoDB tables). (link)
The Innodb documentation says:
In InnoDB, having a long PRIMARY KEY (either a single column with a lengthy value, or several columns that form a long composite value) wastes a lot of disk space. The primary key value for a row is duplicated in all the secondary index records that point to the same row. (See Section 14.3.11, “InnoDB Table and Index Structures”.) Create an AUTO_INCREMENT column as the primary key if your primary key is long, or index a prefix of a long VARCHAR column instead of the entire column.