当前位置:网站首页>1: Enable slow query log and find slow SQL
1: Enable slow query log and find slow SQL
2022-07-28 05:57:00 【Performer】
Why should I start slow query ?
Find the one to optimize sql, Slow query log is mysql A built-in function , You can record the execution of more than a specified time sql sentence .
How to start a slow query
1: Modify the configuration file my.conf,( Need to restart , The configuration is permanently valid , Will not be lost )
lookup my.cnf file
- linux The default path is generally /etc/my.cnf (windows To call down my.ini)
- Add the following to [mysqld] Next
service mysqld restart
# Open slow query log
slow_query_log=ON
# How long it takes will be recorded in the slow query log ( second )
long_query_time=0.5
# Slow query log storage mode , Can support at the same time File and table storage
log_output='FILE,TABLE'

- restart mysql The server , Input
SHOW VARIABLES LIKE 'slow_query_log'Check whether to turn on ,ON Already open
2: Set... Through global variables ( No need to restart , Once the restart , Configuration lost )
- Execute directly on the client The following command
SHOW VARIABLES LIKE 'slow_query_log'
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 0.001;
SET GLOBAL log_output = 'FILE,TABLE';


Find slow SQL
Query through table
- When slow_query_log contain TABLE when , Directly through sql Inquire about , without , It may be just opened , The timeout has not been found sql
SELECT * FROM mysql.slow_log

- Meaning of some fields
start_time Implement this sql The time of the statement
user_host user name And the host name
query_time Inquire about sql The time spent in the statement
lock_time Time to acquire lock
rows_sent Number of rows returned
rows_examined The number of rows queried by the server
db Query the database
sql_text Executive sql sentence
thread_id Thread of execution id
- For example, we can sort according to the time-consuming , Find the most time-consuming sql
SELECT
*
FROM
mysql.slow_log
ORDER BY query_time DESC

Query by file
- First you need to turn on FILE , Can pass
SHOW VARIABLES LIKE 'log_output'Check whether to turn on File store
- Query the path of log storage Can pass
SHOW VARIABLES LIKE 'slow_query_log_file'Inquire about


- have access to mysqldumpslow analysis :
for example : Find the most time-consuming front 10 statement
mysqldumpslow -s t -t 10 -a xxx-slow.log

- The specific parameters are as follows :
* mysqldumpslow --help
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
Parse and summarize the MySQL slow query log. Options are
--verbose verbose
--debug debug
--help write this text to standard output
-v Show more detailed information
-d debug
-s ORDER How to sort , Default at
al: Average lock time
ar: Average number of returned records
at: Average query time
c: Access count
l: Lock time
r: Back to the record
t: Query time
-r take -s In reverse order
-t NUM top n It means , Show the first few
-a Don't show the numbers as N, Display the string as 'S'
-n NUM abstract numbers with at least n digits within names
-g PATTERN You can write a regular , Only regular guilds show
-h HOSTNAME Slow query log to Host name -slow.log Format naming ,-h You can specify to read the slow query log of the specified host name , By default *, Read all slow query logs
-i NAME MySQL Server Instance name of ( If used mysql.server startup Script words )
-l Do not subtract the lock time from the total time
边栏推荐
猜你喜欢
随机推荐
(php毕业设计)基于php校园网络报修管理系统获取
常见WAF拦截页面总结
结果填空 煤球数目
正则表达式
Annotation and grid addition of ArcGIS map making
书籍-乌合之众
Acquisition of mental health service system based on PHP (PHP graduation design)
使用sourcetree推送仓库时 Failed to connect to www.google.com port 80: Timed out
Redis 主从架构的搭建
Books - smart investors
进程线程协程的区别
MYSQL的select语句查询;运算符课后练习
mysql分页出现问题
HDU-1284:钱币兑换问题 推理+动态规划(dp)
Sankey diagram drawing based on highcharts platform
Microsoft edge browser plug-in (2)
数据处理之增删改;约束
NSCTF-web题目writeup
Making E-R diagram based on XMIND
MySQL练习题50道+答案









