当前位置:网站首页>Optimization steps of SQL statements (II) -- MySQL statement optimization
Optimization steps of SQL statements (II) -- MySQL statement optimization
2022-06-28 06:58:00 【Xiaobinbin &】
mysql Database is a widely used database language , In order to improve the sql Statement execution efficiency , In our usual design process , The following aspects can be used for reference , Can effectively improve sql Statement execution efficiency .
1、 Mass insert data (sql files inserting )
adopt load When the command imports data , Appropriate settings can improve the efficiency of import
about innoDB Tables of type are saved in the order of primary keys , So import the data to be imported in the order of the primary key , It can effectively improve the efficiency of data
1) Primary key inserted successfully
Add : head sql file name View file data memory
Data import format
load data local infile ' route file name ' into table ' Table name '
fields terminabled by ','
lines terminated by '\n'nto table : File data needs to be inserted into that table
feilds terminabled : The field is divided by what symbol
lines terminabled: What symbol is used to divide the row
Be careful : When there is a primary key, the import speed will be faster , Therefore, it is recommended to use the primary key
2) Turn off uniqueness check
Turn off uniqueness check , After the data import is completed, the uniqueness verification can be started , It can also improve efficiency
Turn off uniqueness check set unique_checks=0
Enable uniqueness verification set unique_checks=1
Be careful : Opening and closing must exist in pairs 3) Close the commit transaction manually
Turn off auto submit manually , Then turn on auto submit after data import is completed , It will also improve efficiency
set auto_committed=0;
set auto_committed=1;2、 Optimize insert sentence
1). Try to use a single insert Sentence block
insert into student values ( , ) ,( , );
2). Use manual commit of transactions
set auto_committed=0; // Turn off auto submit
start transaction; // Open transaction
insert into...... //select Execute statement
commit(); // Manual submission
set auto_committed=1; // Turn on auto submit 3. Data is inserted in order ( The primary key is inserted orderly )
// Insert in order by primary key
insert into student values(1,);
insert into student values(2,);3、order by Optimize
How to optimize ,
Use index sorting (using index)
Use one scan to sort (filesort)
3.1 Two ways of sorting
filesort( File system sorting ) : Not directly through the index to return results
using index( Index sort ) : Return results directly through the index ( The return fields are all index fields ), High operating efficiency
1、 Single field sorting
2、 Multi field sorting : Press asc Or both desc Sort , Inconsistencies occur filesort Sort
3、 Press order by Then there is the multi field sorting , Sort by index
Such as idx_age_salary ,order by age,salary
3.2 Filesort Optimize
about filesort ,mysql There are two algorithms in
1. Two scan algorithm (mysql4.1 Before )
First, take out the sorting field and row pointer information according to the conditions , stay sort buffer Middle order , Next, read the record back to the table according to the row pointer , This operation may cause a large number of random IO The operation of , Low efficiency
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 , High memory overhead , But it's more efficient
mysql How to choose ?
Compare system variables max_length_for_sort_data and Query The total size of the fields extracted from the statement
max_length_for_sort_data Big words , Will use the second algorithm
Optimization strategy
Improve max_length_for_sort_data and sort_buffer_size Value , Increase the size of the sort area , Make it mandatory to use the second ( One scan algorithm ), Improve the efficiency of sorting
Set up max_length_for_sort_data
show variables like 'max_length_for_sort_data';
set max_length_for_sort_data= value ;
Set up sort_buffer_size
show variables like 'sort_buffer_size';
set sort_buffer_size= value ;4、group by Grouping sorting
Optimize : Index field grouping sort , The grouping field needs to be set as index to improve efficiency
because group by During operation , Also used order by Sort , Then you can use index fields to sort by groups .
let me put it another way : Set the grouped field as the index field , So when grouping , Is to sort by index group , More efficient
5、 Optimize nested queries ( Subquery )
Optimize : Use as few subqueries as possible , Change to table connection join Inquire about
6、OR The optimization of the
Optimize :OR The association conditions are all index fields
Optimize : Use union Join table optimization ( recommend )
Use OR When it comes to efficiency , It should be noted that :
1、 OR The associated conditions must all be index fields , Otherwise, it will not be impossible to use index query
2、 Cannot be a composite index , Must be indexed with a single column
7、limit Paging query
Optimize one : Complete sort paging operation on Index , Finally, according to the primary key Association, return to the original table to query other column contents
// Before optimization
select * from student limit 20000,10;
// After optimization
select * from student a,(select id from student limit 20000,10) b where a.id=b.id;Optimization II 、 Applicable to tables with self incrementing primary key ( A self increasing primary key cannot have a fault ), You can put limit The query is converted to a query in a certain location
select * from student where id >20000 limit 10;8、 Use SQL Tips ( Index tips )
SQL Prompt is an important means to optimize database , Simply speaking , Is in the SQL Add some human prompts in the statement to optimize the operation
1、USE INDEX Which index is recommended ( stay from Use after the table name )
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
select * from Table name use index( Indexes ) where ..... Case study
select * from student use index(name_index) where name=' Zhang San ';2、IGNORE INDEX Ignore the index
select * from Table name ignore index( Indexes ) where ..... Ignore that index
3、FORCE INDEX Force which index to use
select * from Table name force index( Indexes ) where ..... Ignore that index
Be careful :use and ignore Just recommend , It's not mandatory ,force Is mandatory
边栏推荐
- Some habits of it veterans in the workplace
- The code is correct, and the rendering page does not display the reason
- How bacnet/ip gateway collects data of building centralized control system
- FPM tool installation
- 图片按日期批量导入WPS表格
- Create a gson object that formats the time zone. JSON parsing time formatting zoneddatetime
- [rust daily] published on rust 1.43.0 on April 23, 2020
- 创建格式化时间,格式化时区的gson对象。json解析时间格式化 ZonedDateTime
- Triode driven brushless motor
- VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
猜你喜欢

Introduction to browser tools: think sky browser, team work browser

VM332 WAService.js:2 Error: _vm.changeTabs is not a function报错

pytorch RNN 学习笔记

助力涨点 | YOLOv5结合Alpha-IoU

FPGA - 7 Series FPGA selectio -09- io of advanced logic resources_ FIFO

JDBC learning (I) -- implementing simple CRUD operations

Compile configuration in file

Freeswitch使用originate转dialplan

代码没写错,渲染页面不显示原因

Comprehensive analysis of real enterprise software testing process
随机推荐
[digital statistics DP] counting problem
Students who do not understand the code can also send their own token. The current universal dividend model can be divided into BSC and any generation B
服务器正文18:UDP可靠传输的理解和思考(读云凤博客有感)
Freeswitch sets the maximum call duration
全方位透析真实企业软件测试流程
JDBC learning (I) -- implementing simple CRUD operations
YOLOv5增加小目标检测层
[C language] detailed explanation of C language to obtain array length
最后的二十九天
Techo Day 腾讯技术开放日,6月28日线上等你!
FPGA - 7系列 FPGA SelectIO -07- 高级逻辑资源之ISERDESE2
助力涨点 | YOLOv5结合Alpha-IoU
Exception handling (I) -- null pointer and array index out of bounds
[rust daily] published on rust 1.43.0 on April 23, 2020
Camx架构开UMD、KMD log以及dump图的方式
Wechat applets - basics takes you to understand the life cycle of applets (I)
小程序页面设置100%高度还是留白怎么办?
面经---测试工程师web端自动化---大厂面试题
FPGA - 7 Series FPGA selectio -08- oserdese2 of advanced logic resources
[rust daily] May 24, 2020 rush, rocket, Mun, caspin