当前位置:网站首页>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 .
边栏推荐
- Realize a simple LAN communication (similar to feiqiu)
- 【系统分析师之路】第五章 复盘软件工程(软件过程改进)
- 数字时代,XDR(扩展检测与响应)的无限可能
- Unity Animator 参数
- Lifting scanning tool
- In the digital age, XDR (extended detection and response) has unlimited possibilities
- MySQL如何将列合并?
- Google Earth engine (GEE) - ghsl: global human settlements layer, built grid 1975-1990-2000-2015 (p2016) data set
- Pytorch查看模型参数量和计算量
- Jetpack compose for perfect screen fit
猜你喜欢

编程实战赛来啦!B站周边、高级会员等好礼送你啦!

MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video

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

Pytoch viewing model parameter quantity and calculation quantity

"Persistent diseases" that cannot be solved in IM application development

How to execute a query SQL

我如何才能保护我的私钥?

Apache Doris Compaction優化百科全書

Unity Animator 参数

用Unity实现Flat Shading
随机推荐
Je suis à Foshan, où puis - je ouvrir un compte? L'ouverture d'un compte par téléphone mobile est - elle sécurisée?
"Persistent diseases" that cannot be solved in IM application development
I'd like to ask you, where can I open an account in Foshan? Is it safe to open a mobile account?
【科研数据处理】[实践]类别变量频数分析图表、数值变量分布图表与正态性检验(包含对数正态)
Numpy creates an empty array data = np empty(shape=[1, 64,64,3])
想请教一下,我在佛山,到哪里开户比较好?手机开户是安全么?
第四批入围企业公示——年度TOP100智能网联供应商评选
I want to ask how to open an account at China Merchants Securities? Is it safe to open a stock account through the link
目录相关命令
exlipse同时操作多行。比如同时在多行同列输入相同的文字
mysql拒绝访问、管理员身份打开的
SQL programming problem, test case failed
华为帐号多端协同,打造美好互联生活
[kubernetes series] k8s set mysql8 case insensitive
编程实战赛来啦!B站周边、高级会员等好礼送你啦!
navicat数据库建表是没有utf8选项。
What network security problems are exposed when a large-scale QQ number theft event occurs?
Google Earth engine (GEE) - ghsl: global human settlements layer, built grid 1975-1990-2000-2015 (p2016) data set
Complete TCP forwarding server (kernel linked list + mutex)
visualstudio 和sql