当前位置:网站首页>SQL statement optimization steps (1)
SQL statement optimization steps (1)
2022-06-28 06:58:00 【Xiaobinbin &】
mysql Database is a common database processing language , It's also a limitation IT A database language widely used in the industry . Databases are used to process data , Add, delete, modify and check the data (CRUD), For large capacity data processing , The events that need to be handled may be 1 God ,2 God , For the SQL The optimization of statements is very important .
The following is from 5 There are three ways to understand SQL The basic steps of optimization
One 、 Query execution efficiency
1、 Check the frequency of each command in the database
show global status like 'com_______'

2、 see innoDB Frequency of adding, deleting, modifying and querying
show global status like 'innoDB_rows_%' ;

Two 、 Locate inefficient execution SQL frequency
The following two methods can be used to locate those with low execution efficiency SQL sentence
1、 Slow query log :( View the completed SQL How often statements are executed )
2、show processlist ( View the running speed of statements in real time )

The following describes the meaning of each field
| title | meaning |
| Id | The sequence number of each statement previously executed allocated by the system |
| User | Current user name |
| Host | Display the host number sent by this statement 、 port , It can be used to track users who have problems |
| db | Displays which database this process is currently connected to |
| Command | Displays the execution status of the currently connected command , Sleep (sleep), Inquire about (query), Connect (connect) |
| Time | Shows the duration of this state |
| state | Show current SQL Statement state |
| Info | Which execution statement corresponds to the record of this row of data |
Be careful :state Status statement :
eg: select Statements are columns , To pass copying to tmp table,sorting result,sending data Wait for the status to complete .
3、 ... and 、explain Analysis execution plan
stay sql Add... Before the sentence explain You can query the execution plan of this statement
explain select * from tb_score;

Analyze the meaning of each field :
| Field | meaning |
| id | select The serial number of the query , Indicates execution select Order of statements |
| select_type | Express select The type of , Common are SIMPLE( A simple watch )、PRIMARY( Primary key query )、UNION( Link query ,union After statement )、SUBQUERY( First in subquery select) |
| table | Output structure table |
| type | Indicates the type of table connection , Sort performance from high to low (system-->const-->eq_rf-->ref-->ref_or_null-->index_merge-->index_subquery-->range-->index-->all) |
| possible_keys | Indicates the index that may be used in query |
| key | Represents the index actually used in the query |
| key_len | Length of index used |
| rows | Number of scan lines |
| extra | Description and description of the implementation using filesort / using index/using tenporary |
3.1 Id select Query serial number
d The fields are select The serial number of the query , It's a set of numbers , Represents execution in query select Clause or the order of the operation table , There is a concentrated situation
1、 id The same indicates that the loading is smooth from top to bottom
2、 id Different id The bigger the value is. , The higher the priority , Priority to be implemented
3.2 select_type Table type
Value meaning :( From top to bottom, efficiency is getting lower and lower )
simple ordinary select Inquire about , No subqueries or union
primary In a statement that contains subqueries , The outermost query is primary
subquery Subquery
derived ( derivative ), Put the table of subquery in temporary table
union The joint query
union result from union Get the structure in the table select
3.3 table and type
table Identify which table to source from
type It shows the type of access , Is a more important indicator
type , Access from the top down , The access speed is getting lower and lower
null Do not access any tables and indexes , Direct return select * from now();
system There is only one record in the table ( The system tables )
const ( Constant unique index query ) It can be found by indexing only once , Generally used for primary keys and unique, There is only one record
eq_ref Associated query with only one result
ref Non unique index query , Returns all rows for a single value
range Range queries
index Traversed all the index numbers (b-tree structure )
all Will traverse the entire table to find the matching rows
3.4 key
possible key Possible indexes
key Index actually used , If it is null I just didn't follow the index
key_len key Length of index , The shorter the better
3.5 rows
rows Number of lines scanned
3.6 xtra
using filesort : Use files to sort , Low efficiency , Consumption performance
explain select * from student order by sname
here sname It's not an index , That is, you need to scan the whole disk and then sort , Low efficiency , The optimization process is to sname Create index .
using temporary Use a temporary watch , Low efficiency , Consumption performance
using index Use index , The fastest
Be careful : If the first two appear, you need to consider optimization
Four 、show profile analysis sql
testing sql Statement execution time
4.1、 Check whether the current system supports profile Instructions
select @@have_profiling;
4.2、 Check profile Open or not
select @@profiling;
4.3、 Start profile function
set @@profiling;
4.4、 View the elapsed time of all previous instructions
show profiles;
4.5、 See where an instruction takes longer
show prifile for query 5; //query 5 Indicates the order of execution id
5、 ... and 、trace Analysis optimizer
sql sentence --> encapsulation -->trace Optimizer
1、 Start optimizer
set optmizer_trace='enabled=on',end_markers_in_json=on;
2、 Set the occupied memory size
set optimizer_trace_max_men_size=1000000;
3、 View the optimizer execution plan
select * from information_schma.optimizer_trace\G;
边栏推荐
- 微信小程序编译页面空白bug的原因
- 饿久了,大脑会进入“省电模式”!感官被削弱,还看不清东西丨爱丁堡大学...
- Linux MySQL implements root user login without password
- 小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
- Rust FFI 编程 - libc crate
- UPC -- expression evaluation
- 自律挑战30天
- 图片按日期批量导入WPS表格
- FPGA - 7 Series FPGA selectio -07- iserdese2 of advanced logic resources
- Cmake tips
猜你喜欢

freeswitch使用mod_shout模块播放mp3
![[digital statistics DP] counting problem](/img/8d/ac05c1a88543b76fca86cd744e9cb1.jpg)
[digital statistics DP] counting problem

Interpretation of Blog

Freeswitch sets the maximum call duration

How bacnet/ip gateway collects data of building centralized control system

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

"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?

JS regular expression system explanation (comprehensive summary)

js正则表达式系统讲解(全面的总结)

选拔赛题目代码
随机推荐
freeswitch使用mod_shout模块播放mp3
图片按日期批量导入WPS表格
Puge -- understanding of getordefault() method
ROS rviz_ Satellite function package visualizes GNSS track and uses satellite map
代码没写错,渲染页面不显示原因
整型提升和大小端字节序
自律挑战30天
【Rust日报】2020-05-24 Rash, Rocket, Mun, Casbin
"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?
redis的入门学习到起飞,就这一篇搞定
[interval DP] stone consolidation
Is it safe to open a stock trading account on your mobile phone?
编译原理期末复习
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
[online tutorial] official iptables tutorial -- learning notes 1
创建格式化时间,格式化时区的gson对象。json解析时间格式化 ZonedDateTime
【C语言】详解 C 语言获取数组长度
Exception handling (I) -- null pointer and array index out of bounds
Freeswitch uses Mod_ Shot module plays mp3