当前位置:网站首页>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 .
边栏推荐
- Unity animator parameter
- Wuenda 2022 machine learning special course evaluation is coming!
- 【观察】智能产业加速,为何AI算力要先行?
- 为基础性语言摇旗呐喊
- 【科研数据处理】[实践]类别变量频数分析图表、数值变量分布图表与正态性检验(包含对数正态)
- Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
- Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!
- 优思学院:六西格玛不只是统计!
- Publicity of the fourth batch of shortlisted Enterprises - annual Top100 smart Internet supplier selection
- navicat数据库建表是没有utf8选项。
猜你喜欢
Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!
Optimization of unit test efficiency: why test programs? What are the benefits of testing?
Jetpack Compose 实现完美屏幕适配
重磅:国产IDE发布,由阿里研发,完全开源!
一文讲清楚什么是类型化数组、ArrayBuffer、TypedArray、DataView等概念
半导体动态杂谈
QQ 居然被盗了?原因在这......
SQL programming problem, test case failed
Multi terminal collaboration of Huawei accounts to create a better internet life
随机推荐
[Title brushing] avoid flooding
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
正则系列之断言Assertions
Inexplicable error occurred in unity's frequent switching branch result model
DNS resolution home network access public DNS practice
Golang template (text/template)
Getting started with shell Basics
Advanced usage of go language for loop break and continue
Dart extended feature
I want to ask how to open an account at China Merchants Securities? Is it safe to open a stock account through the link
In the digital age, XDR (extended detection and response) has unlimited possibilities
Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
How can I protect my private key?
SQL考勤统计月报表
Introduction to the renewal of substrate source code: the pallet alliance is incorporated into the main line,
【 scientific literature measurement 】 mining and visualization of keywords in foreign and Chinese Literature
DNS 解析之家庭网络接入 Public DNS 实战
幸运哈希竞猜系统开发(源码部署)趣投哈希游戏玩法开发(案例需求)