当前位置:网站首页>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
边栏推荐
- 普歌 -- 单例模式
- Build your jmeter+jenkins+ant
- FPGA - 7 Series FPGA selectio -07- iserdese2 of advanced logic resources
- 【Rust翻譯】從頭實現Rust异步執行器
- KMP string
- How bacnet/ip gateway collects data of building centralized control system
- Trie string statistics
- Is it safe to open a stock trading account on your mobile phone?
- Boost the rising point | yolov5 combined with alpha IOU
- [online tutorial] official iptables tutorial -- learning notes 1
猜你喜欢
JS regular expression system explanation (comprehensive summary)
三极管驱动无刷电机
Build your jmeter+jenkins+ant
Niubi 666, this project makes web page making as simple as building blocks
选拔赛题目代码
FPGA - 7系列 FPGA SelectIO -08- 高级逻辑资源之OSERDESE2
Freeswitch sets the maximum call duration
小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
图片按日期批量导入WPS表格
微信小程序编译页面空白bug的原因
随机推荐
VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
普歌--三大基础排序,冒泡·选择·快速
@RequestParam
What if the applet page is set to 100% height or left blank?
【星海出品】 运维巡检合集
Tryout title code
【C语言】详解 C 语言获取数组长度
4~20mA输入/0~5V输出的I/V转换电路
Hungry for a long time, the brain will enter the "power saving mode"! Your senses are weakened and you can't see clearly
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
记JPA使用自定义VO接收 JPQL查询结果
Batch import of pictures into WPS table by date
Exception handling (I) -- null pointer and array index out of bounds
力扣515.在每棵树行中找最大值
什么是一致性哈希?可以应用在哪些场景?
Rust FFI 编程 - libc crate
[C#][转载]furion框架地址和教程地址
Linked list (I) - remove linked list elements
Puge -- understanding of getordefault() method
代码没写错,渲染页面不显示原因