Calculation of Key_len in MySQL explain

Source: Internet
Author: User

Ken_len represents the number of bytes used by the index, based on this value, it is possible to determine the index usage, especially when combining indexes, to determine whether all the indexed fields are queried.

Key_len shows the index length required by the conditional retrieval clause, but the index used by the ORDER by and the GROUP by clause does not count toward the key_len statistic value;

About the calculation rules for Key_len:

• When the index field is a fixed-length data type, such as: char,int,datetime, whether it needs to have an empty tag, this tag needs to occupy 1 bytes;
• When the index field is a variable-length data type, such as: varchar, besides whether it is an empty tag, it also needs to have length information, which takes up to 2 bytes;
• If the field is defined as non-empty, the mark will not occupy bytes;
• Also need to consider the differences in the character set used in the table, Latin1 encodes a character of 1 bytes, GBK encodes a character 2 bytes, UTF8 encodes a character 3 bytes;

Calculation formula of Key_len length

VARCHR (10) Variable length field and allow null:10* (Character set:utf8=3,gbk=2,latin1=1) +1 (null tag bit) +2 (variable-length field)
VARCHR (10) Variable-length field and do not allow null:10* (Character set:utf8=3,gbk=2,latin1=1) +2 (variable-length field)
char (10) Fixed field and allow null:10* (Character set:utf8=3,gbk=2,latin1=1) +1 (null tag bit)
char (10) Fixed field and does not allow null:10* (Character set:utf8=3,gbk=2,latin1=1)

Calculation of Key_len in MySQL explain

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.