How to calculate Key_len when MySQL is explain

Source: Internet
Author: User
Tags mysql manual

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

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.