当前位置:网站首页>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;
边栏推荐
- 实现这个 issue 得700块钱人民币,有人做嘛?
- Compile configuration in file
- Shell script one click deployment (MySQL)
- VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
- 《微信小程序-基础篇》带你了解小程序中的生命周期(一)
- VM332 WAService.js:2 Error: _vm.changeTabs is not a function报错
- MySQL (II) - basic operation
- Error reporting - resolve core JS / modules / es error. cause. JS error
- Causes of wechat applet compilation page blank bug
- Build your jmeter+jenkins+ant
猜你喜欢

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

FPGA - 7系列 FPGA SelectIO -07- 高级逻辑资源之ISERDESE2

整型提昇和大小端字節序

FPGA - 7系列 FPGA SelectIO -08- 高级逻辑资源之OSERDESE2

声网 VQA:将实时互动中未知的视频画质用户主观体验变可知

Integer promotion and size side byte order

My MVVM open source project "travel epidemic prevention app" has been released

Exception handling (I) -- null pointer and array index out of bounds

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

Triode driven brushless motor
随机推荐
js正则表达式系统讲解(全面的总结)
【Rust日报】 2020-04-23 Rust 1.43.0 发布
[online tutorial] official iptables tutorial -- learning notes 1
【网络教程】IPtables官方教程--学习笔记1
Deleting MySQL under Linux
微信小程序分页功能,下拉刷新功能,直接干货拿来就用
eyebeam高级设置
ROS rviz_ Satellite function package visualizes GNSS track and uses satellite map
Niubi 666, this project makes web page making as simple as building blocks
Pytorch RNN learning notes
extern “C“概述
JS of learning notes -- split(), replace(), join()
freeswitch设置最大呼叫时长
How to open UMD, KMD log and dump diagrams in CAMX architecture
强化学习——格子世界
Extern "C" overview
Servlet value passing JSP
Reinforcement learning - grid world
[rust daily] published on rust 1.43.0 on April 23, 2020
Mise en œuvre de l'actionneur asynchrone d'exécution à partir de zéro