当前位置:网站首页>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
边栏推荐
- IV Transforming regular expressions into finite state automata: DFA minimization
- 科创人·神州数码集团CIO沈旸:最佳实践模式正在失灵,开源加速分布式创新
- In 2026, the capacity of China's software defined storage market will be close to US $4.51 billion
- 奇葩错误 -- 轮廓检测检测到边框、膨胀腐蚀开闭运算效果颠倒
- 软件测试面试官问这些问题的背后意义你知道吗?
- There is always a negative line (upper shadow line) that will stop the advance of many armies, and there is always a positive line (lower shadow line) that will stop the rampant bombing of the air for
- Countdownlatch example
- II Transforming regular expressions into finite state automata: NFA state machine recognizes input strings
- Abstract classes and interfaces
- 简单介绍线程和进程区别
猜你喜欢
随机推荐
SQL basic syntax II
5种最常见的CEPH失败方案
MySQL优化之慢日志查询
Autojs学习笔记6:text(txt).findOne()切换app时会报错,最后解决实现效果,切换任何app直到脚本找到指定的txt文字的控件进行点击。
软件测试面试题精选
Selenium interview question sharing
After going to the bathroom, I figured out the ES search server
Distributed task scheduling
软件定义存储概览(一篇就够)
The Dragon Boat Festival is in good health -- people are becoming more and more important in my heart
Do you know how to improve software testing ability?
测试用例和bug描述规范参考
Auto.js学习笔记9:脚本引擎使用,启动指定路径脚本文件和关闭等基础方法
Essentials reading notes
MySQL索引常见问题
Countdownlatch example
Research progress of DNA digital information storage
Introduction to applet
List、Set、Map的区别
Overview of software definition storage (one article is enough)








