About MySQL general query log and slow query log analysis (rpm)

MySQL in the log include: error logs, binary logs, general query log and slow query log, and so on. Here are the more common under the main two functions: general query log and slow query log.

1) The General Query Log: record client connections and executed statements established.

2) slow query log: a record of all query execution times all over long_query_time seconds or do not use the query index

(1) The General Query Log

In learning general log, you need to know two databases commonly used commands:

1) showvariables like ‘%version%’;

  

The above command, as something associated with the current database version number.

1) showvariables like ‘%general%’;


You can view the current general query log is turned on, if general_log is ON was open, was closed to OFF (default is off).

1) showvariables like ‘%log_output%’;


View the current slow query log output format, which can be FILE (hostname.log number stored in a data file in a database), may also be TABLE (mysql.general_log stored in the database)

Question: How do I turn MySQL general query log, and how to set the output to be output common log format it?

General open log query: set global general_log = on;

General Close Log Query: set globalgeneral_log = off;

General log output setting mode table: set globallog_output = 'TABLE';

Common log file set output mode: set globallog_output = 'FILE';

Log output table is provided and general papers: set global log_output = 'FILE, TABLE';

(Note: The above command only on the entry into force of the restart when MySQL failure, if you want to permanently take effect, you need to configure my.cnf)

FIG log output results are as follows:

Mysql.general_log data recorded in the table below:


Recorded in the local .log in the following format:


My.cnf configuration file is as follows:

general_log = 1 # 1 indicates turning common log, the log value of 0 represents off General Query

log_output = FILE, TABLE # Set common log format file and output tables

(2) slow query log

MySQL slow query log is a log provided by MySQL, is used to record the response time exceeds the threshold value of the statement in MySQL, refers specifically to run longer than long_query_time value of SQL, it will be recorded to the slow query log (the log can be written in a file or database table, if the requirements of high performance, it is recommended to write the file). By default, MySQL database is not turned slow query log, long_query_time the default value is 10 (i.e. 10 seconds is usually set to 1 second), i.e., 10 seconds or more to run slow query statement.

In general, slow queries occur in large table (for example: the amount of data a table of millions), and query the field is not indexed, this time, to match the query criteria fields full table scan, time-consuming checked long_query_time,

It was slow query.

Question: how to view the current status on the slow query log?

Enter the command in MySQL:

showvariables like ‘%quer%’;


The main master the following parameters:

(1) slow_query_log is ON is turned slow query log, OFF was slow query log off.

(2) the value of slow_query_log_file is slow query logging to a file (Note: The default name is the hostname .log, slow query log is written to the specified file, you need to specify the slow query log output file format, related command : show variables like '% log_output%'; to view the output format).

(3) long_query_time slow queries specified threshold, i.e. if the execution time exceeds the threshold value statement was slow query, the default value is 10 seconds.

(4) log_queries_not_using_indexes If the value is set to ON, all queries will not use the index record (NOTE: If only the log_queries_not_using_indexes set to ON, and the slow_query_log set to OFF, the setting will not take effect at this time, i.e., the settings take effect the premise is slow_query_log value is set to oN), usually temporarily turn when performance tuning.

Question: output settings MySQL slow query log format as a file or table, or both?

Command: show variables like '% log_output%';


Log_output can view the value of the output format, the value of the above TABLE. Of course, we can set the output format of the text, or both text and command recording database tables, set as follows:

# Slow query log output to the table (ie mysql.slow_log)

set globallog_output=’TABLE’;

# Slow query log output only to the text (ie: slow_query_log_file specified file)

setglobal log_output=’FILE’;

# Meanwhile slow query log output to a text and tables

setglobal log_output=’FILE,TABLE’;  

About Data Sheet Data Format slow query log in a text analysis:

Logging myql.slow_log slow query table in the following format:


Slow query log records to hostname.log file format is as follows:


It can be seen in either table or file, are specifically documented: the statement that is leading to the slow query (sql_text), the slow query query time (query_time) statement, lock the table (Lock_time), as well as the number of lines scanned (rows_examined) and other information.

Question: How slow query query the current number?

There are a number of variables which tracks the current slow query in MySQL:

Input command: show global status like '% slow%';


(Note: All the above command, if the shell is through MySQL parameters set in, MySQL if restarted, all set good parameters will fail, if you want to permanently take effect, you need to write the configuration parameters in my.cnf file) .

Supplementary knowledge: how to use the built-in MySQL slow query log analysis tool mysqldumpslow analyze the log?

perlmysqldumpslow –s c –t 10 slow-query.log

The specific parameter settings as follows:

Sort represents ways -s, c, t, l, r are recorded in accordance with the number, time, query time, number of records returned to sort, ac, at, al, ar, indicate corresponding flashback;

-t meaning expressed top, followed by the return to the previous after the data showing how many;

-g behind can write regular expression matching, case-insensitive.


The above parameters are as follows:

Count: 414 statement appeared 414 times;

Time = 3.51s (1454) performed a maximum period of 3.51s, the cumulative total time-consuming 1454s;

Lock = 0.0s (0) is the maximum time to wait for a lock 0s, the cumulative time spent waiting for a lock 0s;

Most number of rows Rows = 2194.9 (9097604) 2194.9 is sent to the client, the client is sent to the cumulative function of 90,976,404

http://blog.csdn.net/a600423444/article/details/6854289

(Note: mysqldumpslow script is written in perl language usage specific mysqldumpslow late repeat)

Question: In the actual learning process, learn how to set slow query is valid?

Quite simply, we can produce a slow query manually, for example, if the value of our slow query log_query_time is 1, then we can execute the following statement:

selectsleep(1);

That article is slow query statement, after which they can go to view the appropriate log output file or table if there is the article statement.

View the log. The above query time, the user server ip and returns the query time lines, etc., SQL query below  

Turn   https://blog.csdn.net/timchen525/article/details/75268151

 

Guess you like

Origin www.cnblogs.com/xd502djj/p/10936369.html