当前位置:网站首页>[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 .
边栏推荐
- 【mysql学习笔记27】存储过程
- 关系数据库如何工作
- Browser local storage
- Kdtree notes
- redisson使用全解——redisson官方文档+注释(上篇)
- [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
- 三极管是一项伟大的发明
- Summary of the concept and advantages of 5g massive MIMO
- [Shenzhen IO] precise Food Scale (some understanding of assembly language)
- 在券商账户上买基金安全吗
猜你喜欢

【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)

Are there any practical skills for operation and maintenance management

H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?

We found a huge hole in MySQL: do not judge the number of rows affected by update!!!

2022茶艺师(初级)操作证考试题库及模拟考试

1286_FreeRTOS的任务优先级设置实现分析

关系数据库如何工作

Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
![[programming training] delete public characters (hash mapping) + team competition (greedy)](/img/cd/63eb9da1e8956df0763797f079b67f.png)
[programming training] delete public characters (hash mapping) + team competition (greedy)

热烈祝贺五行和合酒成功挂牌
随机推荐
kdtree(kd树)笔记
Do securities account opening affect the security of account opening
[skill] create Bat quick open web page
Redisson uses the complete solution - redisson official documents + Notes (Part 1)
go-etcd
Solution to the problem that objects in unity2021 scene view cannot be directly selected
2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
2022 mobile crane driver test exercises and online simulation test
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
C language implementation [Sanzi chess game] (step analysis and implementation source code)
Custom events of components ②
Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
Summary of the concept and advantages of 5g massive MIMO
matlab保存DB4i深度相机图片
Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
【无标题】
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
十大劵商如何开户?另外,手机开户安全么?
Are there any practical skills for operation and maintenance management
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字