Enabling the slow query log in MySQL
Today, we will enable MySQL in MySQL using MySQL shell on Windows and Ubuntu 20.04 slow_query_log
. For this tutorial, we are using MySQL version 8.0 and Ubuntu 20.04.
MySQLslow_query_log
MySQL slow_query_log
contains SQL statements that took longer than seconds to run long_query_time
and required at least min_examined_row_limit
rows (records) to be examined.
The SQL queries that appear in MySQL slow_query_log
are the ones that take a lot of running time. Hence, these are the candidates that need to be optimized.
By default, the slow query log is turned off. Let’s see how to enable it on Windows and Ubuntu 20.04.
Enabling MySQL in Windows/Ubuntuslow_query_log
The query given below can be executed on both Windows and Ubuntu operating systems. We need to first enter the MySQL shell and then execute the following command to enable MySQL slow_query_log
.
Sample code:
mysql> SET GLOBAL slow_query_log = 'ON';
Now, run the following query to ensure it is enabled slow_query_log
.
Sample code:
mysql> SHOW VARIABLES LIKE '%slow%';
Output:
+-----------------------------+--------------------------+
| Variable_name | Value |
+-----------------------------+--------------------------+
| log_slow_admin_statements | OFF |
| log_slow_extra | OFF |
| log_slow_replica_statements | OFF |
| log_slow_slave_statements | OFF |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | DESKTOP-QF52OT4-slow.log |
+-----------------------------+--------------------------+
7 rows in set (0.01 sec)
Alternatively, we can also execute the following command to check slow_query_log
whether it is enabled.
Sample code:
mysql> SHOW VARIABLES LIKE '%quer%';
Output:
+----------------------------------------+--------------------------+
| Variable_name | Value |
+----------------------------------------+--------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | NO |
| log_queries_not_using_indexes | OFF |
| log_throttle_queries_not_using_indexes | 0 |
| long_query_time | 10.000000 |
| query_alloc_block_size | 8192 |
| query_prealloc_size | 8192 |
| slow_query_log | ON |
| slow_query_log_file | DESKTOP-QF52OT4-slow.log |
+----------------------------------------+--------------------------+
10 rows in set (0.00 sec)
See the second to last line in both outputs (shown above). slow_query_log
Now it is ON
.
Once we enable that slow_query_log
, we can also enable other options. For example, we can also update how long a query takes to execute before it is logged.
Sample code:
mysql> SET GLOBAL long_query_time = 20;
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Get the version in MySQL
Publish Date:2025/04/24 Views:169 Category:MySQL
-
In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T
Full Join in MySQL
Publish Date:2025/04/24 Views:70 Category:MySQL
-
This article aims to explore how to perform a full join or full outer join in MySQL. Full outer join is used to merge or combine the entire data from two separate tables. For example, suppose we have two tables named student_id and student_
Grouping by month in MySQL
Publish Date:2025/04/24 Views:166 Category:MySQL
-
In this article, we will learn how to group values by month in MySQL database. Businesses and organizations have to find user or customer data based on purchase or usage trends over a few months. If a particular business achieves its
How to drop all tables in MySQL
Publish Date:2025/04/24 Views:196 Category:MySQL
-
This article demonstrates several ways for users to delete all tables in MySQL and lists sample scripts to implement this method. The reason you can't just drop all the tables in one line of code is that in a large and well-designed databas
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Insert timestamp into MySQL table
Publish Date:2025/04/23 Views:78 Category:MySQL
-
Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T
The difference between two tables in MySQL
Publish Date:2025/04/23 Views:102 Category:MySQL
-
In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta
MySQL sorts data alphabetically
Publish Date:2025/04/23 Views:130 Category:MySQL
-
In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t