Example of how to enable Slow query in MySQL

Example of how to enable Slow query in MySQL

Preface

Slow query log is a very important function in MySQL. We can enable the slow query log function of MySQL so that we can analyze the execution status and performance of each SQL statement and optimize it accordingly.

This article introduces the relevant content about enabling Slow query in MySQL. Let’s take a look at the detailed introduction.

Here’s how:

1: Log in to the database to check whether Slow query is enabled:

mysql> show variables like 'slow_query%';

2: Enable MySQL slow log:

By default, the value of slow_query_log is OFF, which means that the slow query log is disabled. You can enable it by setting the value of slow_query_log: (This is temporarily enabled and will become invalid after restarting the service. If you want to disable it permanently, remember to add a statement in the myqsl configuration file, see the end of the article)

mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.03 sec)

3: Check whether it is enabled:

mysql> show variables like 'slow_query%'; 

4: Write mysql slow statement to test whether slow data is written:

mysql> select sleep(10) as a, 1 as b; 

5: Check the Mysql Slow file to see if data is written:

At this point, the above Mysql slow startup job is completed!

--------------------------

Permanently enable MySQL Slow log:

slow_query_log_file slow query log file path

slow_query_log is used to specify whether to open the slow query log

long_query_time Queries exceeding this number of seconds are written to the log

log_output=file must specify a file or a table. If it is a table, the slow query information will be saved in the slow_log table under the mysql database. This point must be made clear. The default value is NONE

Open the my.cnf configuration file and add the following code:

log_output=file
slow_query_log=on ;
slow_query_log_file = /tmp/mysql-slow.log
long_query_time = 2

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • MySQL slow query method and example
  • Detailed explanation of the reason why the MySQL 5.7 slow query log time is 8 hours behind the system time
  • MySQL slow query operation example analysis [enable, test, confirm, etc.]
  • Causes and solutions for slow MySQL query speed and poor performance
  • Causes and solutions for slow MySQL queries
  • Reasons and solutions for slow MySQL query stuck in sending data
  • How to optimize MySQL performance through MySQL slow query
  • How to enable the slow query log function in MySQL
  • Basic usage tutorial of MySQL slow query log
  • Detailed explanation of MySQL slow queries

<<:  The pitfalls of deploying Angular projects in Nginx

>>:  Install Linux rhel7.3 operating system on virtual machine (specific steps)

Recommend

Graphical explanation of the function call of proto file in Vue

1. Compile proto Create a new proto folder under ...

Solution to the problem of mysql master-slave switch canal

After configuring VIP, the error message that app...

Simple encapsulation of axios and example code for use

Preface Recently, when I was building a project, ...

How to develop uniapp using vscode

Because I have always used vscode to develop fron...

How to view the running time of MySQL statements through Query Profiler

The previous article introduced two methods to ch...

Example code for making the pre tag automatically wrap

The pre element defines preformatted text. Text en...

JavaScript implements product details of e-commerce platform

This article shares a common example of viewing p...

What is ssh port forwarding? What's the use?

Table of contents Preface 1. Local port forwardin...

Detailed explanation of triangle drawing and clever application examples in CSS

lead Some common triangles on web pages can be dr...

JavaScript article will show you how to play with web forms

1. Introduction Earlier we introduced the rapid d...

MySQL explain obtains query instruction information principle and example

explain is used to obtain query execution plan in...

Vite+Electron to quickly build VUE3 desktop applications

Table of contents 1. Introduction 2. Create a Vit...

How to install mysql in docker

I recently deployed Django and didn't want to...