当前位置:网站首页>MySQL optimized slow log query
MySQL optimized slow log query
2022-06-12 09:41:00 【Rookie ~~】
Catalog
One 、 Slow query log (slow_query_log) Concept

about SQL And index optimization , We will use explain To analyze SQL sentence . But there are thousands of real enterprise projects SQL, We can't start from scratch, one by one explain To analyze . Where can we get those long running , Performance consuming SQL??
When we analyze the business involved in the project sql Efficiency time , You should open the slow query log , According to specific business 、 Specific concurrency , To estimate a time limit , The entire query cannot exceed the upper limit , Start the business after setting , During the pressure test, we will find out what sql Our query exceeded our forecast time , So these sql Will be recorded in the slow query log , And then use explain Analyze these time-consuming statements , Judge why it is inefficient . You can know whether the index is useful , Or there is no index to be added ( Primary key 、unique Or secondary index ), Or the index is used , However, due to the large amount of data in the table , It takes a long time , At this time, we can divide the table into several small tables , Let's see if there is grouping and sorting , If there is where There is also grouping and sorting , Consider adding a union index .
Slow query log related parameters :show variables like '%slow_query%';(MySQL Many global switches defined , Are stored in global variables , It can be used show/set variables View or set the value of global variable )
The slow query log switch is off by default
Slow query log path : Default in /var/lib/mysql/ Next
The slow query log contains all the execution time exceeding the parameter long_query_time( Company : second ) Of the set value SQL Statement log , stay MySQL Use the command to view , as follows :
This value can be modified , as follows :
Now change it to more than 1 Of a second SQL Will be recorded in the slow query log ! It can be set to 0.01 second , Express 10 millisecond .
Two 、 Slow query log practice
1. Turn on the slow query log switch


When turning on the slow query log switch , Error indication slow_query_log It's a global The variable of ( Others only affect the current session The variable of , Such as :long_query_time 、profiling), The modification will affect all session, That is, it affects all users who are accessing the current MySQL server The client of .
Turn on the slow query log switch successfully !
2. Reasonable setting 、 The upper limit of slow query time acceptable to the business long_query_time

View another session
Discovery is still default 10s, so long_query_time It only affects the present session
3. Pressure measurement performs various businesses

It has exceeded our set long_query_time=0.1s
4. View slow query log
Slow query log path :/var/lib/mysql/


5. use explain Analyze these time-consuming sql sentence , So as to optimize

It turned out to be a whole table search , Sweep the whole primary key index tree . And then again paswd As a filter , The index should be set , Even if you set the index, you still can't use the index , And then I found out that because passwd String format involves type conversion , So there's no index , Correct sql It should be the following statement .
To sum up the problem at the beginning 

Next, we can give some examples , For example, when I do a business, I use where Filter condition plus order by, And then use explain After analysis using filedort, Involving external sorting , Why does it involve external sorting ? Because our data and indexes are stored on the disk , If you don't have a proper index , We have to do it. order by You can only sort externally , This is time-consuming , Then I tried to use where Filter conditions and order by The sorted fields establish a union index ...
Then it will be combined with the index of various problems mentioned in the previous blog , For example, type forced conversion 、 The filter conditions used MySQL No index is used for any of the functions , Take into account the optimization and so on . Reference answer :MySQL Index FAQs
3、 ... and 、show profiles see sql Specific running time
MySQL Generally, only two decimal places are displayed 
open profiling switch , More detailed time can be displayed 
No report error , explain profiling Variables only affect the current session
边栏推荐
- 测试用例和bug描述规范参考
- Introduction to applet
- 抽象类和接口
- 7-13 地下迷宫探索(邻接表)
- Common technical questions in functional test interview. Would you like to summarize them?
- anxious
- Distributed task scheduling
- Differences among list, set and map
- SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
- Overview of software definition storage (one article is enough)
猜你喜欢
随机推荐
gnu-efi开发环境设置
5 most common CEPH failure scenarios
【云原生】Eureka服务注册的搭建
Ceph如何改善存储性能以及提升存储稳定性
Hotspot Metaspace
7-4 网红点打卡攻略(dfs)
Crazy temporary products: super low price, big scuffle and new hope
[cloud native] establishment of Eureka service registration
MySQL-MVCC
Summary of APP test interview questions, which must be taken in the interview
Do you know how to improve software testing ability?
Distributed transaction solution 1: TCC (compensation transaction)
001: what is a data lake?
Auto.js学习笔记9:脚本引擎使用,启动指定路径脚本文件和关闭等基础方法
哈希表的理论讲解
Cas d'essai et spécification de description des bogues référence
Research progress of DNA digital information storage
Common omissions in software test reports, give yourself a wake-up call
Selection of interview questions for software testing
自动化测试学习路线,快来学吧







