当前位置:网站首页>MySQL index hit level analysis
MySQL index hit level analysis
2022-07-07 17:39:00 【Ugly beyond the 38th parallel】
Mysql Index hit level
The result value is from good to bad :
- system/const, system stay mysql8 There seems to be no more . Generally, the unique result of primary key query will be const.
eq_ref
: similar ref, The difference is that the index used is the only index , For each index key value , Only one record in the table matches , Simply speaking , It is used in multi table connection primary key perhaps unique key As a condition of Association- ref: The lookup criteria column uses an index and is not primary key and unique. Actually , It means that although index is used , But the value of the index column is not unique , Repeat . In this way, even if the index is used to quickly find the first data , Still can't stop , To scan a small area near the target value . But the advantage of it is that it doesn't need to scan the whole table , Because the index is ordered , Even if there are duplicate values , It's also scanning in a very small area .
- range: Index scan with range , be relative to index Full scan table , He has limits , So it's better than index
- index: Another form of full table scanning , But his scanning method is in the order of index
- all: Full table scan
preparation
Create a teacher list
CREATE TABLE teacher (
id INT ( 5 ),
NAME VARCHAR ( 20 ),
card_id INT ( 5 )
);
Create a teacher card information table
CREATE TABLE teacher_card (
id INT ( 5 ),
remark VARCHAR ( 20 )
);
Insert teacher information
insert into teacher values(1, ' Zhang San ', 1);
insert into teacher values(2, ' Li Si ', 2);
insert into teacher values(3, ' Wang Wu ', 3);
Insert teacher card information
insert into teacher_card values(1, ' Teacher Zhang's card ');
insert into teacher_card values(2, ' Miss Li's card ');
insert into teacher_card values(3, ' Miss Wang's card ');
const
Type testing
Add the primary key index before the test
alter table teacher add constraint pk_teacher_id primary key(id);
The derived table has only one main query of data ; The derived table queries only one piece of data through the primary key , Then use this data to make the main query .
EXPLAIN select * from teacher where id=1
Get the results of the implementation plan :
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | teacher | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
- Only one piece of data can be found SQL, be used for primary key or unique The index of ( Other index types do not belong to ).
- When there is only one piece of data in the primary key query , The type is const
eq_ref
Type testing
to teacher_card Add primary key
alter table teacher_card add constraint pk_teacher_card_id primary key(id);
Analysis of teacher Table to index unique queries
explain select t.card_id from teacher t, teacher_card tc where t.card_id = tc.id;
explain select t.card_id from teacher t left join teacher_card tc on t.card_id = tc.id;
Get the results of the implementation plan
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------------------------+------+----------+-------------+
| 1 | SIMPLE | t | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
| 1 | SIMPLE | tc | NULL | eq_ref | PRIMARY | PRIMARY | 4 | mysql_ref_index_test.t.card_id | 1 | 100.00 | Using index |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------------------------+------+----------+-------------+
Uniqueness index , The table index is associated with the primary key of another surface , Each data between the two tables should correspond one by one ( Each one should correspond one by one , One cannot correspond to multiple , There must be no correspondence ), The queried data is unique in the table , There can be no repetition .
ref
Type testing
Non unique linear index , The data returned from the query of each index key is 0 Or more .
to teacher
Tabular name
Add an index to the field
alter table teacher add index index_teacher_name (name);
- according to name = Zhang San found two pieces of data
- according to name The value directly queried by the index is ref type .
sqlexplain select * from teacher where name = ' Zhang San ';
Implementation plan results :
+----+-------------+---------+------------+------+--------------------+--------------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+--------------------+--------------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | teacher | NULL | ref | index_teacher_name | index_teacher_name | 83 | const | 1 | 100.00 | NULL |
+----+-------------+---------+------------+------+--------------------+--------------------+---------+-------+------+----------+-------+
range
Type testing
- Check the specified range line ,where After that is a range query (between、in、>、)
- see range Index of type
in
The situation of
explain select * from teacher t where t.id in (1, 2);
Implementation plan results
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | t | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 2 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
between
The situation of
explain select * from teacher where id between 1 and 2;
Implementation plan results
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | teacher | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 2 | 100.00 | Using where |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
Comparison
explain select * from teacher where id < 3;
Implementation plan results
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | teacher | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 2 | 100.00 | Using where |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
Range query the specified index , Its index type is range:
index
Type testing
- Query all data in the index .
- Query all the data in the index , among
name
It's index. - Go straight to the index , There's no need to go back
explain select name from teacher;
Implementation plan
+----+-------------+---------+------------+-------+---------------+--------------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+--------------------+---------+------+------+----------+-------------+
| 1 | SIMPLE | teacher | NULL | index | NULL | index_teacher_name | 83 | NULL | 3 | 100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+--------------------+---------+------+------+----------+-------------+
all
Type testing
- Query all the data in the table , Or query according to fields that are not indexes .
- That is to go through the whole table scanning , Can't go to the index .
EXPLAIN select * from teacher;
Implementation plan results
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | teacher | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
type Type summary :
system
/const
: The result is just one piece of data .eq_ref
: There are many results , But every data is unique .ref
: There are many results , But the data queried can be multiple , And the data can be repeated .range
: Search the index fields for range queries .
边栏推荐
- 本周小贴士#136:无序容器
- 鲲鹏开发者峰会2022 | 麒麟信安携手鲲鹏共筑计算产业新生态
- 麒麟信安中标国网新一代调度项目!
- Jenkins发布uniapp开发的H5遇到的问题
- 《世界粮食安全和营养状况》报告发布:2021年全球饥饿人口增至8.28亿
- Ratingbar的功能和用法
- With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore
- 麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
- 赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
- calendarview日历视图组件的功能和用法
猜你喜欢
麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
责任链模式 - Unity
Lex & yacc of Pisa proxy SQL parsing
MRS离线数据分析:通过Flink作业处理OBS数据
状态模式 - Unity(有限状态机)
【OKR目标管理】价值分析
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
麒麟信安云平台全新升级!
第3章业务功能开发(用户登录)
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
随机推荐
阿富汗临时政府安全部队对极端组织“伊斯兰国”一处藏匿点展开军事行动
LeetCode1051(C#)
【TPM2.0原理及应用指南】 9、10、11章
Repair method of firewall system crash and file loss, material cost 0 yuan
命令模式 - Unity
Devops' operational and commercial benefits Guide
本周小贴士#136:无序容器
Functions and usage of viewflipper
Dragging the custom style of Baidu map to the right makes the global map longitude 0 unable to be displayed normally
【分布式理论】(一)分布式事务
【网络攻防原理与技术】第1章:绪论
Ansible 学习总结(9)—— Ansible 循环、条件判断、触发器、处理失败等任务控制使用总结
[re understand the communication model] the application of reactor mode in redis and Kafka
LeetCode 515(C#)
百度地图自定义样式向右拖拽导致全球地图经度0度无法正常显示
【OKR目标管理】价值分析
【可信计算】第十次课:TPM密码资源管理(二)
【网络攻防原理与技术】第4章:网络扫描技术
麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
calendarview日历视图组件的功能和用法