当前位置:网站首页>key&key_len&ref&filtered(4)—mysql执行计划(五十)
key&key_len&ref&filtered(4)—mysql执行计划(五十)
2022-07-26 12:54:00 【用户9919783】
前面说了system是精确存储引擎和只存一条数据,const是主键和唯一索引才能达到的效率访问,ref是二级索引等值查询,或者联合索引全部等值,如果联合索引单个查询,则是index,ref_not是二级索引等值查询而且有null,range代表分区查询,all代表全表查询。
possible keys & key
possible keys代表可能用到的索引,key代表,mysql优化器成本计算后,实际会用到的索引key,
所以possible keys并不是越多越好,代表扫描的索引越多。
mysql> EXPLAIN SELECT * FROM s1 WHERE key1 > 'z' AND key3 = 'a';
+----+-------------+-------+------------+------+-------------------+----------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+-------------------+----------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | s1 | NULL | ref | idx_key1,idx_key3 | idx_key3 | 303 | const | 6 | 2.75 | Using where |
+----+-------------+-------+------------+------+-------------------+----------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
mysql> EXPLAIN SELECT key_part2 FROM s1 WHERE key_part3 = 'a';
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | s1 | NULL | index | NULL | idx_key_part | 909 | NULL | 9688 | 10.00 | Using where; Using index |
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)有一点特别的是,当type是index时候,possible_kyes是空的。
key_len
表示mysql当决定使用某个索引的时候,使用索引的最大长度,这个长度是由三个部分组成:
1、对于使用固定长度类型的索引,则他实际占用的大小就是他的固定值,若是utf8,varchar(100),则他实际占用长度是100*3 = 300。
2、如果有null,需要加个null值列表。长度为1
3、对于变长字段,都会有两个字节来存储实际长度。
mysql> EXPLAIN SELECT * FROM s1 WHERE id = 5;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | s1 | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)
mysql> EXPLAIN SELECT * FROM s1 WHERE key2 = 5;
+----+-------------+-------+------------+-------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | s1 | NULL | const | idx_key2 | idx_key2 | 5 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)从上可以看到,因为主键没有null值,所以比key2的key_len长度少1,因为key2可以有null。
对于可变长的索引来说:
mysql> EXPLAIN SELECT * FROM s1 WHERE key1 = 'a';
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | s1 | NULL | ref | idx_key1 | idx_key1 | 303 | const | 8 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)因为key1属于varchar(100),所以是100*3,又因为可以null,+1,又因为变长字段+2,所以就是303。当使用联合索引查询的时候,需要注意的是,会根据使用几个索引而现实多长:
mysql> EXPLAIN SELECT * FROM s1 WHERE key_part1 = 'a';
+----+-------------+-------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | s1 | NULL | ref | idx_key_part | idx_key_part | 303 | const | 12 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
mysql> EXPLAIN SELECT * FROM s1 WHERE key_part1 = 'a' AND key_part2 = 'b';
+----+-------------+-------+------------+------+---------------+--------------+---------+-------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+--------------+---------+-------------+------+----------+-------+
| 1 | SIMPLE | s1 | NULL | ref | idx_key_part | idx_key_part | 606 | const,const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+--------------+---------+-------------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)上面可以看到,联合索引里有三个字段,第一个sql用到一个索引,索引是303,第二个sql用到两个索引,索引是606.
ref
当索引在等值匹配查询的时候,在访问,const,ref,ref_or_null,unique sub_query,index sub_query其中之一时,
mysql> EXPLAIN SELECT * FROM s1 WHERE key1 = 'a';
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | s1 | NULL | ref | idx_key1 | idx_key1 | 303 | const | 8 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)这里可以看到ref现实的是const,表名key1做匹配的时候,是个常数。
mysql> EXPLAIN SELECT * FROM s1 INNER JOIN s2 ON s1.id = s2.id;
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------+
| 1 | SIMPLE | s1 | NULL | ALL | PRIMARY | NULL | NULL | NULL | 9688 | 100.00 | NULL |
| 1 | SIMPLE | s2 | NULL | eq_ref | PRIMARY | PRIMARY | 4 | xiaohaizi.s1.id | 1 | 100.00 | NULL |
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)
mysql> EXPLAIN SELECT * FROM s1 INNER JOIN s2 ON s2.key1 = UPPER(s1.key1);
+----+-------------+-------+------------+------+---------------+----------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | s1 | NULL | ALL | NULL | NULL | NULL | NULL | 9688 | 100.00 | NULL |
| 1 | SIMPLE | s2 | NULL | ref | idx_key1 | idx_key1 | 303 | func | 1 | 100.00 | Using index condition |
+----+-------------+-------+------------+------+---------------+----------+---------+------+------+----------+-----------------------+
2 rows in set, 1 warning (0.00 sec)而这里可以看到前面的是数据库加s1的id,后面是一个函数。
row
mysql> EXPLAIN SELECT * FROM s1 WHERE key1 > 'z';
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | s1 | NULL | range | idx_key1 | idx_key1 | 303 | NULL | 266 | 100.00 | Using index condition |
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)如果是全表扫描,则是全表扫描的行数,如果是索引扫描,则是索引需要扫描的行数。比如上面的sql,因为key1是索引,所以是扫描索引满足的数量有266条。
Filtered
我们前面说过连接查询的时候,有一个扇出值的概念,被驱动表查询的次数,取决于驱动表查询的数据有多少行,
1、如果是全表扫描的时候,那么计算驱动表扇出时,估计出满足搜索记录需要多少条。
2、如果使用索引执行单表查询,那么计算驱动表扇出时,估计出满足使用到对应索引的搜索条件外其他搜索记录有多少条。
mysql> EXPLAIN SELECT * FROM s1 WHERE key1 > 'z' AND common_field = 'a';
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+------------------------------------+
| 1 | SIMPLE | s1 | NULL | range | idx_key1 | idx_key1 | 303 | NULL | 266 | 10.00 | Using index condition; Using where |
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+------------------------------------+
1 row in set, 1 warning (0.00 sec)比方这个单表查询,rows可以看到key1索引扫描满足>z的有266条,那么有多少条满足common_filed = 'a' 呢,这就要看filtered了,sql查询出来的数据就是266 * 10%。
接下来我们 看连接查询
mysql> EXPLAIN SELECT * FROM s1 INNER JOIN s2 ON s1.key1 = s2.key1 WHERE s1.common_field = 'a';
+----+-------------+-------+------------+------+---------------+----------+---------+-------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------------------+------+----------+-------------+
| 1 | SIMPLE | s1 | NULL | ALL | idx_key1 | NULL | NULL | NULL | 9688 | 10.00 | Using where |
| 1 | SIMPLE | s2 | NULL | ref | idx_key1 | idx_key1 | 303 | xiaohaizi.s1.key1 | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------------------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)连接查询可以看到s1表全表扫描查出估计值是9688条,然后* 10%,意味着扇出值就是9688 * 10%,也就意味着s2会执行扇出值的次数。
边栏推荐
- Today's sleep quality record 75 points
- If there is a declaration "int x=5, y=1;", Then the expression x < y? The result of x++: y++ is:
- A super easy-to-use artifact apifox, throw swagger a few streets... (glory Collection Edition)
- Azure synapse analytics Performance Optimization Guide (2) -- optimize performance using materialized views (Part 1)
- Shell variables and references
- [typescript] typescript common types (Part 2)
- Interviewer: how to deal with high concurrency?
- Code error reporting and problem solving experience II: test error reporting in yolov5
- LCD notes (4) analyze the LCD driver of the kernel
- C regards type as generic type T and uses it as generic type of method
猜你喜欢

Ue5 official case Lyra full feature explanation 7. resource management

维度灾难 维数灾难 暂记

【TypeScript】TypeScript常用类型(上篇)

Kubelet CRI 容器运行时

Kubernetes----高级存储之PV和PVC简介

笔记。。。。

最好的工程师,就是这样被你“逼”走的!

0 basic programming resources (collect first ~ read slowly ~)
![[applet] why can't the onreachbottom event be triggered? (one second)](/img/da/3641040c63f6db4d227dcf2ff89919.png)
[applet] why can't the onreachbottom event be triggered? (one second)

Food safety | what food can be heated in a microwave oven? You should know these potential safety hazards
随机推荐
Paper reading MLPD: multi label pedestrian detector in multispectral domain (Internship Program of Hikvision Research Institute)
jvm:类加载子系统干什么的?由什么组成?需要记住哪些八股文?
(int argc, char** argv) command line parameters in visual stdio (VS)
Does Flink CDC only support SQL client to submit SQL scripts
The best engineer was "forced" away by you like this!
The database consists of stored procedures and functions
手机上买股票,在哪里开户比较安全?
A super easy-to-use artifact apifox, throw swagger a few streets... (glory Collection Edition)
LCD笔记(7)LCD驱动程序框架_配置时钟
Bitwise and how to write SQL
Shutter cachednetworkimage fillet
Reflection, an implementation of automatic repeated call interface
概要设计说明书
Redisson distributed lock usage example (I)
食品安全 | 微波炉什么食品都能加热?这些安全隐患要知道
Knowledge points of C language documents
PXE principle and configuration
Kubelet CRI 容器运行时
Shutter background graying effect, how transparency, gray mask
一款超好用的神器Apifox,甩 Swagger 几条街...(荣耀典藏版)