Detailed explanation of mysql record time-consuming sql example

Detailed explanation of mysql record time-consuming sql example

mysql records time-consuming sql

MySQL can record time-consuming SQL or SQL that does not use indexes in the slow log for optimization and analysis.

1. Enable mysql slow query log:

MySQL slow query log is very useful for tracking problematic queries. It can analyze the SQL statements that consume a lot of resources in the current program. So how to turn on MySQL slow query log recording?

mysql> show variables like 'log_slow_queries';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| log_slow_queries | OFF | 
+------------------+-------+
1 row in set (0.01 sec)

mysql> 

This means that the slow log function is not enabled. To enable it, you need to modify the MySQL configuration file and add the following two parameters in the configuration file "[mysqld]":

long_query_time=1
log-slow-queries=/var/mysql/logs/slow.log

illustrate

long_query_time

This parameter indicates the measurement time of slow query in seconds, with a minimum value of 1 and a default value of 10. Any SQL statement that takes longer than long_query_time will be recorded in the slow query log.

log-slow-queries[=file_name]

The file_name parameter is optional. The default value is host_name-slow.log. If the file_name parameter is specified, MySQL will record the slow query log in the file set by file_name. If file_name provides a relative path, MySQL will record the log in the MySQL data directory. This parameter can only be added in the configuration file and cannot be executed in the command line.

2. Configure the slow log to record unused index queries

You can add the "log_queries_not_using_indexes" parameter to the MySQL startup configuration file or command line parameters to add the query statements that do not use indexes to the slow log.

An example is as follows:

[root@localhost mysqlsla-2.03]# more /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
log_bin=/tmp/mysql/bin-log/mysql-bin.log
log_bin=ON
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1


log_slow_queries=/tmp/127_slow.log
long_query_time=1
log_queries_not_using_indexes

.......

After restarting mysql, the inspection results are as follows:

mysql> show variables like 'log_slow_queries';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| log_slow_queries | ON | 
+------------------+-------+
1 row in set (0.00 sec)

mysql> show variables like 'long_query_time';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| long_query_time | 2 | 
+-----------------+-------+
1 row in set (0.00 sec)

mysql>

The above is a detailed explanation of the SQL example of MySQL recording time-consuming. If you have any questions, please leave a message or discuss in the community of this site. Thank you for reading and hope to help everyone. Thank you for your support of this site!

You may also be interested in:
  • 5 Tips for Protecting Your MySQL Data Warehouse
  • How to avoid the trap of URL time zone in MySQL
  • Compile and install MySQL 5.6 in CentOS Yum
  • Examples of the correct way to use AES_ENCRYPT() and AES_DECRYPT() to encrypt and decrypt MySQL
  • Tutorial on using prepare, execute and deallocate statements in MySQL
  • Basic Tutorial on PDO Operations with MySQL (Recommended)

<<:  CocosCreator Universal Framework Design Network

>>:  Steps to use autoconf to generate Makefile and compile the project

Blog    

Recommend

Summary of 3 ways to lazy load vue-router

Not using lazy loading import Vue from 'vue&#...

Detailed explanation of MySQL execution plan

The EXPLAIN statement provides information about ...

Mac node deletion and reinstallation case study

Mac node delete and reinstall delete node -v sudo...

Method of building redis cluster based on docker

Download the redis image docker pull yyyyttttwwww...

Linux common commands chmod to modify file permissions 777 and 754

The following command is often used: chmod 777 文件...

How to implement responsive layout with CSS

Implementing responsive layout with CSS Responsiv...

Summary of 9 excellent code comparison tools recommended under Linux

When we write code, we often need to know the dif...

In-depth understanding of mathematical expressions in CSS calc()

The mathematical expression calc() is a function ...

Five things a good user experience designer should do well (picture and text)

This article is translated from the blog Usability...

Solve the problem of garbled data in MySQL database migration

Under the instructions of my leader, I took over ...

You Probably Don’t Need to Use Switch Statements in JavaScript

Table of contents No switch, no complex code bloc...

Solution to CSS flex-basis text overflow problem

The insignificant flex-basis has caused a lot of ...

Flash embedded in web pages and IE, FF, Maxthon compatibility issues

After going through a lot of hardships, I searched...

vue+tp5 realizes simple login function

This article example shares the specific code of ...

Interpretation of syslogd and syslog.conf files under Linux

1: Introduction to syslog.conf For different type...