当前位置:网站首页>MySQL back to table query optimization
MySQL back to table query optimization
2022-06-30 14:09:00 【Zhen Jie.】
1. What is return table query
Premise : This test uses mysql 5.6 edition .
1. 1 mysql Storage engine for
mysql There are many categories of storage engines , The more common one is MyISAM and InnoDb Two kinds of , Specific storage The engine is unknown .
1. 2 InnoDb Storage engine
InnoDb The storage engine has two categories of indexes
Clustered index (clustered index)
General index (secondary index)
1. 3 The difference between a clustered index and a normal index
InnoDb The leaf node of the clustered index stores row records , therefore InnoDb There must be a clustered index and only one clustered index .
Clustered indexes are also conditional , It must be orderly , There is another point to be noted here , Order is not continuous , Such as :1,3,5,7 Is ordered , But it's not continuous , Can still form a clustered index . If it's through uuid The generated primary key cannot form a clustered index .
InnoDb Leaf nodes store row records , Is a complete piece of data ,MyISAM Leaf nodes store record pointers .
InnoDB Clustered index The leaf node of the tree stores row records , therefore ,InnoDB There must be and only one clustered index
- A、 If you define PK, that PK Clustering index .
- B、 If there is no definition PK, that NOT NULL UNIQUE A column is a clustered index
- C、 otherwise InnoDb Will create a hidden row-id Columns as clustered indexes
Voice over : therefore PK The inquiry is very fast , Locate row records directly .
InnoDB General index The leaf node stores the primary key value .
Voice over : Be careful , Not store row record header pointer ,MyISAM The index leaf node stores the record pointer .
After a general understanding of the above concepts , So what is a watch ? Suppose there is a table t(id PK, name KEY, sex, flag), Table has 4 Bar record . (id Is clustered index ,name It's a general index .)
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`sex` varchar(5),
`flag` varchar(5),
PRIMARY KEY (`id`) USING BTREE)
ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTE
SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

2. The difference between two different indexes storing the above data
Clustered index storage ( Store row records ) id by PK, Clustered index , Leaf nodes store row records 
Nonclustered index ( Storage PK) name by KEY, General index , Leaf node storage PK value , namely id
Suppose we execute the following query statement ,mysql How is it implemented ?
select * from user where name='ls';

summary : The pink path is the process of query execution , First through the general index name Navigate to the primary key value id=5, Then locate the row record through the cluster index . This is called back table query , First locate the primary key value , Repositioning row records , Its performance is lower than scanning the index tree .
You need to scan the index tree twice :
- First locate the primary key value through the normal index id=5;
- To locate row records through a clustered index ;
3、 So we need to build a composite index to cover the queried fields ;
In this way, you can get... From an index tree SQL All required column data , There is no need to return the form , Faster .
No coverage index is used ( Note the single valued index )
create table user (
id int primary key,
name varchar(20),
sex varchar(5),
index(name)
)engine=innodb;
Query statement
explain select id, name, sex from `user` where name = "ls"
explain The results of the analysis 
analysis :
keys The value of this column is name, Description hit index name, however sex The value of this column can only be queried by going back to the table
Use overlay index ( Note that the composite index )
create table user (
id int primary key,
name varchar(20),
sex varchar(5),
index(name, sex)
)engine=innodb;
Query statement
explain select id, name, sex from `user` where name = "ls"
explain The results of the analysis 
analysis :
Can hit index coverage , There is no need to return the form .
4、 Which scenarios can be optimized with index coverage SQL? How to avoid querying data in the back table ?
scene 1: Full table count Query optimization 
The original table is :
user(PK id, name, sex);
direct :
select count(name) from user;
Cannot use index overlay .
Add index :
alter table user add key(name);
We can use index coverage to improve efficiency .
scene 2: Column query return table optimization
select id,name,sex … where name=‘shenjian’;
This example will not be repeated , Index a single column (name) Upgrade to Federated index (name, sex), To avoid returning to the table .
scene 3: Paging query
select id,name,sex … order by name limit 500,100;
Index a single column (name) Upgrade to Federated index (name, sex), You can also avoid going back to the table .
边栏推荐
- get请求与post提交区别的简易理解
- The programming competition is coming! B station surrounding, senior members and other good gifts to you!
- Publicity of the fourth batch of shortlisted Enterprises - annual Top100 smart Internet supplier selection
- Multi terminal collaboration of Huawei accounts to create a better internet life
- 一文讲清楚什么是类型化数组、ArrayBuffer、TypedArray、DataView等概念
- Inexplicable error occurred in unity's frequent switching branch result model
- Unity 频繁切换分支 结果模型出现莫名其妙的错误
- [the path of system analyst] Chapter V software engineering (software process improvement)
- Lifting scanning tool
- golang模板(text/template)
猜你喜欢

Google Earth Engine(GEE)——将字符串的转化为数字并且应用于时间搜索( ee.Date.fromYMD)

IM即时通讯应用开发中无法解决的“顽疾”

半导体动态杂谈
![[kali] Kali system, software update (with image source)](/img/ac/43a3f81d50ab6866271b500b142252.png)
[kali] Kali system, software update (with image source)

Rpm2rpm packaging steps

navicat数据库建表是没有utf8选项。

用Unity实现Flat Shading

QQ 居然被盗了?原因在这......

Introduction to reverse commissioning - VA and RVA conversion in PE 04/07

Optimization of unit test efficiency: why test programs? What are the benefits of testing?
随机推荐
Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
[observation] as the intelligent industry accelerates, why should AI computing power take the lead?
Lucky hash quiz system development (source code deployment) fun investment hash game play development (case requirements)
Pytoch viewing model parameter quantity and calculation quantity
数字化转型道阻且长,如何迈好关键的第一步
Dart 扩展特性
Je suis à Foshan, où puis - je ouvrir un compte? L'ouverture d'un compte par téléphone mobile est - elle sécurisée?
Simple understanding of the difference between get request and post submission
Jetpack compose for perfect screen fit
【科学文献计量】外文文献及中文文献关键词的挖掘与可视化
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)
How to execute a query SQL
[the path of system analyst] Chapter V software engineering (software process improvement)
golang模板(text/template)
Comprehensively analyze the basic features and summary of free and paid SSH tools
科普达人丨漫画图解什么是eRDMA?
Complete TCP forwarding server (kernel linked list + mutex)
DeFi“钱从哪来”?一个大多数人都没搞清楚的问题
幸运哈希竞猜系统开发(源码部署)趣投哈希游戏玩法开发(案例需求)
Apache Doris Compaction優化百科全書