EXPLAIN
可以查看 select, delete, insert, replace, update 语句的执行计划(MySQL怎么执行SQL语句的)。
explain 为select 语句中使用到的每一个table 返回一行信息。
它按照MySQL在处理语句时读取它们的顺序列出了输出中的表。
输出的表解析
Column | JSON Name | Meaning |
---|---|---|
id | select_id | The SELECT identifier |
select_type | None | The SELECT type |
table | table_name | The table for the output row |
partitions | partitions | The matching partitions |
type | access_type | The join type |
possible_keys | possible_keys | The possible indexes to choose |
key | key | The index actually chosen |
key_len | key_length | The length of the chosen key |
ref | ref | The columns compared to the index |
rows | rows | Estimate of rows to be examined |
filtered | filtered | Percentage of rows filtered by table condition |
Extra | None | Additional information |
type (join type)值的解释
const 全主键等于查询,
select * from tb1 where primary_key=1;
select * from tb1 where primary_key1 = 1 and primary_key2=2;
通常情况下, key_len= 字段字符数 * 字符集每个字符所占字节数
default null ,会使 key_len+1
变长的字段如varchar,会使key_len+2
int ,key_len=4
bigint, key_len=8
如果可以为null 长度加1
对于 DEFAULT CHARSET=utf8
varchar 3*length + 2 [+ 1] // 多出来的两个字节是为了记录长度的, 最长 65536
对于 DEFAULT CHARSET=utf8mb4
varchar 4 * length + 2 [+ 1]