当前位置:网站首页>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
边栏推荐
- "Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?
- The code is correct, and the rendering page does not display the reason
- [produced by Xinghai] operation and maintenance inspection collection
- Error reporting - resolve core JS / modules / es error. cause. JS error
- Deleting MySQL under Linux
- [c #] [reprint]furion frame address and tutorial address
- Mise en œuvre de l'actionneur asynchrone d'exécution à partir de zéro
- Rust FFI 编程 - libc crate
- C language tutorial
- FPGA - 7系列 FPGA SelectIO -07- 高级逻辑资源之ISERDESE2
猜你喜欢

编译原理期末复习

整型提升和大小端字节序

Triode driven brushless motor

Hungry for a long time, the brain will enter the "power saving mode"! Your senses are weakened and you can't see clearly

FPGA - 7 Series FPGA selectio -07- iserdese2 of advanced logic resources

Niubi 666, this project makes web page making as simple as building blocks

Integer promotion and size side byte order

Interpretation of Blog

Tryout title code

我的MVVM开源项目《出行防疫App》已发布
随机推荐
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
Eyebeam advanced settings
服务器正文18:UDP可靠传输的理解和思考(读云凤博客有感)
How to open UMD, KMD log and dump diagrams in CAMX architecture
最后的二十九天
Puge -- three basic sorting, bubbling, selection and quickness
Wechat applets - basics takes you to understand the life cycle of applets (I)
CRC32概述以及实现和使用
FPM tool installation
FPGA - 7 Series FPGA selectio -07- iserdese2 of advanced logic resources
Batch import of pictures into WPS table by date
MySQL (II) - basic operation
2 startup, interrupt and system call
C语言教程大全
Rn7302 three-phase electric quantity detection (based on STM32 single chip microcomputer)
Compile configuration in file
【星海出品】 运维巡检合集
编译配置in文件
Libuv framework echo server C source code explanation (TCP part)
4. use MySQL shell to install and deploy Mgr clusters | explain Mgr in simple terms