mysql view real-time statements and slow sql
View real-time statements
In addition to manually executed statements, Mysql also has many statements executed by other modules in the background. Logically speaking, those statements executed by other modules cannot be viewed in real time because this resource consumption is particularly large, but When we really need to view real-time SQL statements, it is not impossible. We need to manually turn on a log switchgeneral_log
.
First log in to mysql, and then executeshow variables like "general_log%";
to see the feedback results, as follows:
mysql> show variables like "general_log%";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| general_log | OFF |
| general_log_file | |
+------------------+-------+
2 rows in set (0.04 sec)
found that this Value isoff
, then it means that real-time recordinggeneral_log
is not enabled. If we want to enable it, it is very simple, as follows:
mysql> set global log_output = file;
mysql> set global general_log = 'ON';
mysql> set global general_log_file = '/tmp/mysql/general_log.log';
It can be seen that we not only turned on the switch of general_log
, but also set the log output mode to a file (if log_output=table
is set, the log results will be recorded to the name In the table with gengera_log
, the default engine of this table is CSV
). At the same time, it is specified that its storage location is /tmp/mysql/general_log.log
.
But this is a temporary method. If mysql is restarted, it will become invalid. If you want it to be permanently effective, you need to editmy.cnf
and add the following two sentences:
general_log = 1
general_log_file = /tmp/mysql/general_sql.log
Pay attention here! Enabling general_log will affect performance, use it with caution! The official system must be closed when it is finished!!! Close statementSET GLOBAL general_log = 'OFF';
.
View slow sql
Slow sql means those sql that execute very slowly. These sql slow down the execution efficiency of the process and have a lot of room for optimization. By default, SQL is considered slow if the execution time exceeds 1 second. If you enter show variables like 'long%'
in mysql, you will see the following content:
mysql> show variables like 'long%';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
Thislong_query_time
can be changed. Here it is 1, which means that any query time greater than (not greater than or equal to) 1 second will be recorded in the log. The maximum value is 10. If 0 is written, all statements are output.
One more thing here. After using the commandset global long_query_time=4
to modify the slow query threshold to 4 seconds, you need to reconnect or open a new session to see the modified value. You use show variables like 'long_query_time'
to view the variable value of the current session. You can also use show global variables like 'long_query_time';
without reconnecting the session.
So where are these slow logs recorded? Useshow variables like '%slow_query_log%';
to see:
mysql> show variables like '%slow_query_log%';
+---------------------+-----------------------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /tmp/mysql/DB-Server-slow.log |
+---------------------+-----------------------------------------------+
2 rows in set (0.00 sec)
This shows that the address of the slow log is/tmp/mysql/DB-Server-slow.log
, but the slow logging function is not enabled. If you want to start, the statement is: set global slow_query_log=1;
, which is the same as opening the real-time log general_log
above. This method is only a temporary method. It will become invalid after restarting mysql. If you want to To take effect in the long term, please add the following two sentences to themy.cnf
file:
slow_query_log =1
slow_query_log_file=/tmp/mysql/DB-Server-slow.log
The slow log also has a system variable calledlog-queries-not-using-indexes
, which means that queries that do not use indexes are also recorded in the slow query log, even though they may execute very quickly (can options). If tuning, it is recommended to enable this option. In addition, if this parameter is enabled, SQL using full index scan
will also be recorded in the slow query log. As follows:
mysql> show variables like 'log_queries_not_using_indexes';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF |
+-------------------------------+-------+
1 row in set (0.00 sec)
mysql> set global log_queries_not_using_indexes=1;
Query OK, 0 rows affected (0.00 sec)
If you want to try whether the slow SQL is recorded, you can use a statement likeselect sleep(5);
. The execution effect is as follows:
mysql> select sleep(5) ;
+----------+
| sleep(5) |
+----------+
| 0 |
+----------+
1 row in set (5.00 sec)
mysql> select * from mysql.slow_log;
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+
| start_time | user_host | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text | thread_id |
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+
| 2018-01-30 21:45:23 | root[root] @ localhost [] | 00:00:05 | 00:00:00 | 1 | 0 | | 0 | 0 | 1 | select sleep(5) | 2 |
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+
1 rows in set (0.00 sec)
References
http://www.cnblogs.com/kerrycode/p/5593204.html
https://www.cnblogs.com/qmfsun/p/4844472.html
http://www.cnblogs.com/jasondan/p/3491258.html