当前位置:网站首页>I hope some suggestions on SQL optimization can help you who are tortured by SQL like me
I hope some suggestions on SQL optimization can help you who are tortured by SQL like me
2022-07-26 20:12:00 【Erudite Valley wild architect】
6.1 Optimize insert sentence
When it comes to data insert During operation , The following optimization schemes can be considered .
If you need to insert many rows of data into a table at the same time , You should try to use more than one value table insert sentence
This way will greatly reduce the connection between the client and the database 、 Turn off consumption, etc .
Make the efficiency ratio separate from the single execution insert Fast sentence .
Example , The original way is :
insert into xxx values(1,'Tom'); insert into xxx values(2,'Cat'); insert into xxx values(3,'Jerry');The optimized scheme is :
insert into xxx values(1,'Tom'),(2,'Cat'),(3,'Jerry');
Data is inserted in order
insert into tb_test values(4,'Tim'); insert into tb_test values(1,'Tom'); insert into tb_test values(3,'Jerry'); insert into tb_test values(5,'Rose'); insert into tb_test values(2,'Cat');After optimization
insert into tb_test values(1,'Tom'); insert into tb_test values(2,'Cat'); insert into tb_test values(3,'Jerry'); insert into tb_test values(4,'Tim'); insert into tb_test values(5,'Rose');
6.2 Optimize order by sentence
6.2.1 Environmental preparation
CREATE TABLE `emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`age` int(3) NOT NULL,
`salary` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `emp` (`id`, `name`, `age`, `salary`) values('1','Tom','25','2300');
insert into `emp` (`id`, `name`, `age`, `salary`) values('2','Jerry','30','3500');
insert into `emp` (`id`, `name`, `age`, `salary`) values('3','Luci','25','2800');
insert into `emp` (`id`, `name`, `age`, `salary`) values('4','Jay','36','3500');
insert into `emp` (`id`, `name`, `age`, `salary`) values('5','Tom2','21','2200');
insert into `emp` (`id`, `name`, `age`, `salary`) values('6','Jerry2','31','3300');
insert into `emp` (`id`, `name`, `age`, `salary`) values('7','Luci2','26','2700');
insert into `emp` (`id`, `name`, `age`, `salary`) values('8','Jay2','33','3500');
insert into `emp` (`id`, `name`, `age`, `salary`) values('9','Tom3','23','2400');
insert into `emp` (`id`, `name`, `age`, `salary`) values('10','Jerry3','32','3100');
insert into `emp` (`id`, `name`, `age`, `salary`) values('11','Luci3','26','2900');
insert into `emp` (`id`, `name`, `age`, `salary`) values('12','Jay3','37','4500');
create index idx_emp_age_salary on emp(age,salary);6.2.2 Two ways of sorting
1). The first is to sort the returned data , That is to say filesort Sort
tips
Sorting that does not return the sorting result directly through the index is called FileSort Sort .
explain select * from emp order by age desc;
explain select * from emp order by age asc;
2). The second way is to return ordered data directly through ordered index scanning , This is the case using index,
No need for extra sorting , High operating efficiency .
explain select id from emp order by age asc;
explain select id,age from emp order by age asc;
explain select id,age,salary from emp order by age asc;
Multi field sorting
-- Multi field sorting
explain select id,age,salary from emp order by age, salary;
explain select id,age,salary from emp order by age desc, salary desc;
explain select id,age,salary from emp order by salary desc, age desc;
explain select id,age,salary from emp order by age desc, salary asc ;summary :
I understand MySQL Sort by , The goal of optimization is clear :
Try to minimize the extra ordering , Return ordered data directly through index .
where Conditions and Order by Use the same index , also Order By In the same order as the index , also Order by The fields of are all in ascending order , Or in descending order . Otherwise, extra operation is necessary , And then there will be FileSort.
6.2.3 Filesort Optimization principle
tips
Understanding can
By creating the appropriate index , Can reduce the Filesort Appearance , But in some cases , Conditionality cannot allow Filesort disappear , Then we need to speed up Filesort The sorting operation of . about Filesort , MySQL There are two sort algorithms :
1) Two scan algorithm :MySQL4.1 Before , Use this method to sort . First, take out the sorting field and row pointer information according to the conditions , Then in the sorting area sort buffer Middle order , If sort buffer Not enough , On the temporary watch temporary table The sorting result is stored in . After sorting , Then read the record according to the row pointer back to the table , This operation may result in a large number of random I/O operation .
2) One scan algorithm : Take out all the fields that meet the conditions at once , Then in the sorting area sort buffer After sorting, output the result set directly . When sorting, memory overhead is large , But the efficiency of sorting is higher than that of twice scanning algorithm .
MySQL By comparing system variables max_length_for_sort_data The size and Query The total size of the field taken out by the statement , To determine which sort algorithm to use , If max_length_for_sort_data Bigger , Then use the second optimized algorithm ; Otherwise use the first .
Can be improved properly sort_buffer_size and max_length_for_sort_data System variables , To increase the size of the sorting area , Improve the efficiency of sorting .
show variables like 'max_length_for_sort_data';
show variables like 'sort_buffer_size';6.3 Optimize group by sentence
tips
Continue to use 6.2 Optimize order by Table created in statement emp
Create index ( preparation ):
create index idx_emp_age_salary on emp(age,salary); because GROUP BY In fact, the sorting operation will also be carried out , And with the ORDER BY comparison ,GROUP BY It's mainly about the grouping operation after sorting .
Of course , If some other aggregate functions are used when grouping , So we need to calculate some aggregate functions . therefore , stay GROUP BY During the implementation of , And ORDER BY You can also use the index .
If the query contains group by But users want to avoid the consumption of sorting results , Then you can execute order by null No sorting . as follows :
drop index idx_emp_age_salary on emp;
explain select age,count(*) from emp group by age;
After optimization
explain select age,count(*) from emp group by age order by null;
As can be seen from the above example , first SQL The statement needs to be "filesort", And the second one. SQL because order by null There is no need for "filesort", As mentioned above Filesort It's very time consuming .
6.4 Optimize nested queries
tips
Continue to use 4.3 Use explain Analyze the tables created in the execution plan
Mysql4.1 After the version , Start supporting SQL Subquery of . This technology can be used SELECT Statement to create a single column query result , Then use this result as a filter in another query . Using subquery can complete many logical steps at once SQL operation , At the same time, transaction or table lock can be avoided , And it's easy to write . however , In some cases , Subqueries can be joined more efficiently (JOIN) replace .
Example , Find all user information with roles :
explain select * from t_user where id in (select user_id from user_role );The execution plan is :

After optimization :
explain select * from t_user u , user_role ur where u.id = ur.user_id;
Connect (Join) The reason why queries are more efficient , Because MySQL There is no need to create a temporary table in memory to complete this logically two-step query .
6.5 Use SQL Tips
tips:
Continue to use 5.2.1 Prepare the tables created in the environment
SQL Tips , Is an important means to optimize the database , Simply speaking , Is in the SQL Add some human prompts in the statement to optimize the operation .
6.5.1 USE INDEX
After the table name in the query statement , add to use index To provide hope MySQL To refer to the index list , You can make MySQL No longer consider other available indexes .
create index idx_seller_name on tb_seller(name);
explain select * from tb_seller where name=' Xiaomi Tech '
explain select * from tb_seller use index(idx_seller_name) where name=' Xiaomi Tech '
6.5.2 IGNORE INDEX
If the user just wants to let MySQL Ignore one or more indexes , You can use ignore index As hint .
explain select * from tb_seller ignore index(idx_seller_name_sta_addr) where name = ' Xiaomi Tech ';
6.5.3 FORCE INDEX
Is mandatory MySQL Use a specific index , It can be used in query force index As hint .
create index idx_seller_address on tb_seller(address);
explain select * from tb_seller force index(idx_seller_address) where address = ' The Beijing municipal ';
focus Java Technology dry goods sharing , Welcome like-minded friends , Exchange and study together
边栏推荐
- eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c
- 几张图帮你捋清“中国金融机构体系”
- 【OBS】解决OBS推两个rtmp流 + 带时间戳问题
- 【ffmpeg】给视频文件添加时间戳 汇总
- 【Pytorch进阶】pytorch模型的保存与使用
- 【JVM 系列】JVM 调优
- Is it safe for CSCI qiniu school to open an account? What is qiniu for
- How to wait for the return results of multiple asynchronous tasks synchronously?
- 超强接口协作平台如何打造:细数Apifox的六把武器
- 罗永浩赌上最后一次创业信用
猜你喜欢

Analysis of interface testing

Overview of canvas

Week 6 Convolutional Neural Networks (CNNs)

【MySQL】 - 索引原理与使用

Docker使用mysql:5.6和 owncloud 镜像,构建一个个人网盘,安装搭建私有仓库 Harbor

Leetcode daily practice - 26. Delete duplicates in an ordered array

Use of load balancing
![[Android] the black technology behind kotlin's rapid compilation. Learn about it~](/img/d6/ae107f75c158e97913e6d75eac5b84.png)
[Android] the black technology behind kotlin's rapid compilation. Learn about it~

How to uninstall win11 edge? The method tutorial of completely uninstalling win11 edge browser

本机号码一键登录原理与应用(荣耀典藏版)
随机推荐
【ffmpeg】给视频文件添加时间戳 汇总
使用请求头认证来测试需要授权的 API 接口
本机号码一键登录原理与应用(荣耀典藏版)
【OBS】解决OBS推两个rtmp流 + 带时间戳问题
金仓数据库 KingbaseES SQL 语言参考手册 (13. SQL语句:ALTER SYNONYM 到 COMMENT)
Zhongtian steel uses tdengine in GPS and AIS scheduling
Decompile jar files (idea environment)
three. Two methods of making Earth annotation with JS
负载均衡的使用
tf.GraphKeys
Kingbases SQL language reference manual of Jincang database (13. SQL statement: alter synonym to comment)
金融机构导图
以 NFT 为抵押品的借贷协议模式探讨
一个开源的网页画板,真的太方便了
金仓数据库 KingbaseES SQL 语言参考手册 (21. KES正则表达式支持)
Kingbasees SQL language reference manual of Jincang database (20. SQL statements: merge to values)
【Pytorch进阶】pytorch模型的保存与使用
Where can I find the files downloaded from iPad
Zabbix调用api检索方法
Record an analysis of a.Net property management background service stuck