了解Explain语句中的MySQL key_len [英] Understanding MySQL key_len in Explain Statement

查看:103
本文介绍了了解Explain语句中的MySQL key_len的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MySQL网站key_len列指示MySQL决定使用的密钥的长度.如果键列显示NULL,则长度为NULL.请注意,key_len的值使您能够确定MySQL实际使用的多部分键的多少部分.

According to MySQL website, the key_len column indicates the length of the key that MySQL decided to use. The length is NULL if the key column says NULL. Note that the value of key_len enables you to determine how many parts of a multiple-part key MySQL actually uses.

使用我的

Using an example from my previous question, I have an EXPLAIN SELECT statement that shows MySQL using an Index with key_len: 6. Below shows the composition of the index and columns used.

`Type` char(1) NOT NULL,
`tn` char(1) NOT NULL DEFAULT 'l',
`act` tinyint(1) unsigned NOT NULL DEFAULT '0',
`flA` mediumint(6) unsigned NOT NULL DEFAULT '0',
KEY `Index` (`Type`, `tn`, `act`, `flA`)

那么key_len的值如何使我确定查询使用的是多部分键的前三个部分?

So how does the value of key_len allows me to determine that my query uses the first three parts of a multiple-part key?

推荐答案

key_len指定MySQL从密钥中使用的字节数.
索引始终使用left_to_right.即仅使用最左侧的部分.

The key_len specifies the number of bytes that MySQL uses from the key.
Indexes are always used left_to_right. i.e. only the left-most part is used.

您的字段长度如下:

1 byte             `Type` char(1) NOT NULL,
1 byte             tn char(1) NOT NULL DEFAULT 'l',
1 byte             act tinyint(1) unsigned NOT NULL DEFAULT '0',
3 bytes            flA mediumint(6) unsigned NOT NULL DEFAULT '0',
1+1+1+3 = 6 bytes  KEY `Index` (`Type`, `tn`, `act`, `flA`)
 key usage always starts here ---^^^^^

如果key_len = 3,则说明它使用的是type+tn+act.
请注意,在这种配置下,Key_len = 4是不可能的.

If the key_len = 3 then it's using type+tn+act.
Note that Key_len = 4 is impossible in this configuration.

这篇关于了解Explain语句中的MySQL key_len的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆