当前位置:网站首页>[MySQL learning notes 25] SQL statement optimization
[MySQL learning notes 25] SQL statement optimization
2022-07-01 07:35:00 【yqs_ two hundred and eighty-one million eight hundred and seven】
insert Optimize
1. Multiple single insert Statements can be merged into a batch insert insert sentence . One time insertion is better than many times , Because each insertion requires a connection .
2. Manually control transactions . because mysql The transaction of is automatic by default , Every time you execute one sql Statement will start a transaction .
3. Insert primary key in order . This sum mysql Bottom related .
4. Mass data import is not recommended insert, It is load Instructions .
# When the client connects to the server , Add parameters –local-infile
mysql --local-infile -u root -p
# Set global parameters local_infile by 1, Turn on the switch to import data from the local load file
set global local_infile = 1;
# perform load The instruction will prepare the data , Load into table structure
load data local infile ‘/root/sql1.log’ into table ‘tb_user’ fields terminated by ‘,’ lines terminated by ‘\n’;
Primary key optimization
1. stay innodb In the engine , Table data is organized and stored according to the primary key , This storage method is called index organization table (index organized table IOT)
When the primary key is inserted out of order, it is easy to split pages , Therefore, disordered insertion should be avoided , Or you can directly use the auto increment primary key , Once and for all
expand : Page merge , When the amount of data in a page is less than 50% when ,mysql Will try to merge adjacent pages , Called page merge
2. The length of the primary key should be minimized in development , Because secondary indexes store primary keys , The longer it takes up more space
order by Optimize
using filesort: Put the data into the cache , Sort .
using index: Directly use the index to return the sorting results .
mysql Due to the use of B+Tree Indexes , It is easy to traverse the data in order , Index when order by When the following field has an index , No additional sorting is required , bring order by Increased efficiency .
If the overlay index is not used , Then it is unrealistic to use index sorting , Because the field is missing , So we need to do filesort.
filesort The buffer size of is limited , If the data exceeds the buffer, it will be sorted on the disk , Greatly affect performance .
The buffer size can be adjusted .
group by Optimize
using temporary: Use temporary tables to group , Low efficiency
using index: Use index grouping , Efficient
Set table user Yes (profession,age,status) Joint index
select profession from user group by profession;#using index
select age,count(age) from user group by age;#using index;using temporary
notes : There's one in front using index Because the overlay index
select profession , age, count() from tb_user group by profession,age;#using index
select age, count() from tb_user where profession = ‘ Software Engineering ’ group by age;#using index
limit Optimize
select * from tb_sku limit 2000000,10;
Replace with
select * from tb_sku t , (select id from tb_sku order by id limit 2000000,10) a where t.id = a.id;
count Optimize
myisam The engine uses a separate variable to record the total number of records in the table , therefore count It's very efficient , If not where
innodb The engine needs a full table scan , To count the quantity
Optimization idea : Open an extra place to store the total quantity
count( Field )<count( Primary key )<count(1)≈count(*)
update Optimize
When updating, try to update according to the indexed fields , This is a row lock , Otherwise, it will become a table lock .
边栏推荐
- TCP/UDP 通信问题整理
- 2022茶艺师(初级)操作证考试题库及模拟考试
- Custom events of components ②
- 手机开户选哪个证券公司比较好,哪个更安全
- Which securities company is better or safer for mobile phone account opening
- matlab保存DB4i深度相机图片
- TodoList经典案例①
- [image processing] image histogram equalization system with GUI interface
- C语言实现【扫雷游戏】完整版(实现源码)
- Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
猜你喜欢

Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you

奥迪AUDI EDI 项目中供应商需要了解哪些信息?

Eigen matrix operation Library

2022 test questions and mock examinations for main principals of hazardous chemicals business units

Ctfhub port scan (SSRF)

ctfshow-web352,353(SSRF)

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)

Custom events of components ①
![[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array](/img/5e/068268857b8c7c2065ba552fd23dbb.png)
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array

C# Newtonsoft.Json中JObject的使用
随机推荐
2022 test questions and mock examinations for main principals of hazardous chemicals business units
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
ctfshow-web352,353(SSRF)
Autosar 学习记录(1) – EcuM_Init
運維管理系統,人性化操作體驗
Apple account password auto fill
[target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)
【mysql学习笔记27】存储过程
2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)
Will Internet talents be scarce in the future? Which technology directions are popular?
Félicitations pour l'inscription réussie de wuxinghe
[R language] two /n data merge functions
运维管理系统,人性化操作体验
Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
LeetCode+ 71 - 75
C# Newtonsoft.Json中JObject的使用
【mysql学习笔记28】存储函数
运维管理有什么实用的技巧吗