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

Recommend

MySQL 5.7.21 installation and configuration method graphic tutorial (window)

Install mysql5.7.21 in the window environment. Th...

Vant+postcss-pxtorem implements browser adaptation function

Rem layout adaptation The styles in Vant use px a...

Vue implements tree table

This article example shares the specific code of ...

CSS3 implements the sample code of NES game console

Achieve resultsImplementation Code html <input...

Detailed explanation of MySQL's Seconds_Behind_Master

Table of contents Seconds_Behind_Master Original ...

Differences in the hr separator between browsers

When making a web page, you sometimes use a dividi...

JS interview question: Can forEach jump out of the loop?

When I was asked this question, I was ignorant an...

HTML code example: detailed explanation of hyperlinks

Hyperlinks are the most frequently used HTML elem...

How to track users with JS

Table of contents 1. Synchronous AJAX 2. Asynchro...

js realizes packaging multiple pictures into zip

Table of contents 1. Import files 2. HTML page 3....

MySQL 8.0.22 winx64 installation and configuration graphic tutorial

mysql 8.0.22 winx64 installation and configuratio...

How to delete a property of an object in JavaScript

1. delete delete is the only real way to remove a...

Example code for css flex layout with automatic line wrapping

To create a flex container, simply add a display:...