There is no detailed description of key_length in the MySQL manual, and the calculation method of Key_length is verified by experiments.
- When the index field is a fixed-length data type, such as Char,int,datetime, if there is a null tag, this tag takes up to 1 bytes. For variable-length data types, such as varchar, there is a need for length information in addition to a null tag, which takes up to 2 bytes. (When the field is defined as NOT NULL, the null tag will not consume bytes).
- Different character sets, latin1 encodes one character at a byte, GBK encodes a character of 2 bytes, and UTF8 encodes a character 3 bytes.
- You can specify the length of the index when you create the index, for example:
ALTER TABLE test add index URI (URI (30));
Length 30 refers to the number of characters, if UTF8 is encoded as varchar (255), key_length=30*3+2=92 bytes.
How to calculate Key_len when MySQL is explain