当前位置:网站首页>[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 .
边栏推荐
- Autosar 学习记录(1) – EcuM_Init
- 2022危险化学品经营单位主要负责人试题及模拟考试
- LeetCode+ 71 - 75
- 2022 tea master (intermediate) recurrent training question bank and answers
- The computer has a network, but all browser pages can't be opened. What's the matter?
- Operation and maintenance management system, humanized operation experience
- Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
- 【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
- AUTOSAR learning record (1) – ECUM_ Init
- Atguigu---- scaffold --02- use scaffold (2)
猜你喜欢

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

Atguigu---- scaffold --02- use scaffold (2)

2022 tea master (intermediate) recurrent training question bank and answers

Microsoft announces open source (Godel) language model chat robot

运维面临挑战?智能运维管理系统来帮您

2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation

【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)

2022 mobile crane driver test exercises and online simulation test

【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案

redisson使用全解——redisson官方文檔+注釋(上篇)
随机推荐
软件测试方法和技术 - 基础知识概括
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
[microservice openfeign] feign's log record
【微服务|openfeign】Feign的日志记录
Image style migration cyclegan principle
I bet on performance and won the CTO of the company. I want to build Devops platform!
weback5基础配置详解
Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
Custom events of components ①
redisson使用全解——redisson官方文檔+注釋(上篇)
vscode 根据 ESLint 规范自动格式化代码
atguigu----脚手架--02-使用脚手架(2)
Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
MATLAB之基础知识
Do securities account opening affect the security of account opening
LeetCode+ 71 - 75
浅谈CVPR2022的几个研究热点
C # read and write customized config file
go-etcd
Eigen matrix operation Library