慢查询日志由执行时间超过 long_query_time 秒且至少需要检查 min_examined_row_limit 行的 SQL 语句组成。慢查询日志可用于查找执行时间较长的查询,因此这些查询是优化的候选项。然而,查看较长的慢查询日志可能是一项耗时的任务。为了简化此过程,您可以使用 mysqldumpslow 命令来处理慢查询日志文件并总结其内容。请参见 第 6.6.10 节,“mysqldumpslow — 总结慢查询日志文件”。The slow query log consists of SQL statements that take more than
long_query_time seconds to
execute and require at least
min_examined_row_limit rows to be
examined. The slow query log can be used to find queries that take
a long time to execute and are therefore candidates for
optimization. However, examining a long slow query log can be a
time-consuming task. To make this easier, you can use the
mysqldumpslow command to process a slow query
log file and summarize its contents. See
Section 6.6.10, “mysqldumpslow — Summarize Slow Query Log Files”.
获取初始锁的时间不计入执行时间。mysqld 在语句执行完毕并所有锁释放后,将该语句写入慢查询日志,因此日志顺序可能与执行顺序不同。The time to acquire the initial locks is not counted as execution time. mysqld writes a statement to the slow query log after it has been executed and after all locks have been released, so log order might differ from execution order.
long_query_time 的最小值和默认值分别为 0 和 10。该值可以指定到微秒级别。The minimum and default values of
long_query_time are 0 and 10,
respectively. The value can be specified to a resolution of
microseconds.
默认情况下,管理语句不会被记录,未使用索引进行查询的查询也不会被记录。此行为可以使用 log_slow_admin_statements 和 log_queries_not_using_indexes 来更改,如稍后所述。By default, administrative statements are not logged, nor are
queries that do not use indexes for lookups. This behavior can
be changed using
log_slow_admin_statements and
log_queries_not_using_indexes,
as described later.
默认情况下,慢查询日志是禁用的。要显式指定初始慢查询日志状态,请使用 --slow_query_log[={0|1}]。如果没有参数或参数为 1,--slow_query_log 将启用日志。如果参数为 0,则此选项将禁用日志。要指定日志文件名,请使用 --slow_query_log_file=。要指定日志输出位置,请使用 file_namelog_output 系统变量(如 第 7.4.1 节,“选择通用查询日志和慢查询日志的输出目的地” 所述)。By default, the slow query log is disabled. To specify the
initial slow query log state explicitly, use
--slow_query_log[={0|1}]. With no
argument or an argument of 1,
--slow_query_log enables the log.
With an argument of 0, this option disables the log. To specify
a log file name, use
--slow_query_log_file=.
To specify the log destination, use the
file_namelog_output system variable (as
described in Section 7.4.1, “Selecting General Query Log and Slow Query Log Output Destinations”).
如果您指定 TABLE 日志目标,请参阅 日志表和 “打开的文件过多”” 错误。If you specify the TABLE log destination,
see Log Tables and “Too many open files” Errors.
如果你没有为慢查询日志文件指定名称,默认名称是。服务器会在数据目录中创建该文件,除非给定绝对路径名以指定不同的目录。If you specify no name for the slow query log file, the default
name is
host_name-slow.log.
The server creates the file in the data directory unless an
absolute path name is given to specify a different directory.host_name-slow.log
要在运行时禁用或启用慢查询日志或更改日志文件名,请使用全局 slow_query_log 和 slow_query_log_file 系统变量。将 slow_query_log 设置为 0 以禁用日志,设置为 1 以启用日志。设置 slow_query_log_file 以指定日志文件名。如果日志文件已打开,它将被关闭,并打开新文件。To disable or enable the slow query log or change the log file
name at runtime, use the global
slow_query_log and
slow_query_log_file system
variables. Set slow_query_log
to 0 to disable the log or to 1 to enable it. Set
slow_query_log_file to specify
the name of the log file. If a log file already is open, it is
closed and the new file is opened.
如果您使用 --log-short-format 选项,服务器会向慢查询日志写入较少的信息。The server writes less information to the slow query log if you
use the --log-short-format
option.
要在慢查询日志中包含缓慢的管理语句,请启用 log_slow_admin_statements 系统变量。管理语句包括 ALTER TABLE、ANALYZE TABLE、CHECK TABLE、CREATE INDEX、DROP INDEX、OPTIMIZE TABLE 和 REPAIR TABLE。To include slow administrative statements in the slow query log,
enable the
log_slow_admin_statements
system variable. Administrative statements include
ALTER TABLE,
ANALYZE TABLE,
CHECK TABLE,
CREATE INDEX,
DROP INDEX,
OPTIMIZE TABLE, and
REPAIR TABLE.
要在写入慢查询日志的语句中包含未使用索引进行行查找的查询,请启用 log_queries_not_using_indexes 系统变量。(即使启用了该变量,服务器也不会记录那些由于表中少于两行而无法从索引中受益的查询。)To include queries that do not use indexes for row lookups in
the statements written to the slow query log, enable the
log_queries_not_using_indexes
system variable. (Even with that variable enabled, the server
does not log queries that would not benefit from the presence of
an index due to the table having fewer than two rows.)
当记录未使用索引的查询时,慢查询日志可能会迅速增大。可以通过设置log_throttle_queries_not_using_indexes 系统变量,对这些查询设置速率限制。默认情况下,该变量为 0,表示没有限制。正值会对未使用索引的查询记录施加每分钟限制。第一次此类查询会开启一个 60 秒的时间窗口,在此期间服务器会记录直到达到给定限制的查询,然后抑制额外的查询。如果在窗口结束时有被抑制的查询,服务器会记录一个摘要,指明有多少查询被抑制以及它们所花费的总时间。下一个 60 秒窗口将在服务器记录下一条未使用索引的查询时开始。When queries that do not use an index are logged, the slow query
log may grow quickly. It is possible to put a rate limit on
these queries by setting the
log_throttle_queries_not_using_indexes
system variable. By default, this variable is 0, which means
there is no limit. Positive values impose a per-minute limit on
logging of queries that do not use indexes. The first such query
opens a 60-second window within which the server logs queries up
to the given limit, then suppresses additional queries. If there
are suppressed queries when the window ends, the server logs a
summary that indicates how many there were and the aggregate
time spent in them. The next 60-second window begins when the
server logs the next query that does not use indexes.
服务器按以下顺序使用控制参数来确定是否将查询写入慢查询日志:The server uses the controlling parameters in the following order to determine whether to write a query to the slow query log:
查询必须不是管理语句,或者必须启用
log_slow_admin_statements。The query must either not be an administrative statement, orlog_slow_admin_statementsmust be enabled.查询必须至少花费
long_query_time秒,或者必须启用log_queries_not_using_indexes,且查询在行查找时未使用索引。The query must have taken at leastlong_query_timeseconds, orlog_queries_not_using_indexesmust be enabled and the query used no indexes for row lookups.查询必须检查至少
min_examined_row_limit行。The query must have examined at leastmin_examined_row_limitrows.查询不得根据
log_throttle_queries_not_using_indexes设置被抑制。The query must not be suppressed according to thelog_throttle_queries_not_using_indexessetting.
log_timestamps 系统变量控制写入慢查询日志文件(以及通用查询日志文件和错误日志)的消息的时间戳时区。它不会影响写入日志表的通用查询日志和慢查询日志消息的时区,但从这些表中检索的行可以使用 CONVERT_TZ() 或通过设置会话 time_zone 系统变量从本地系统时区转换为任何所需时区。The log_timestamps system
variable controls the time zone of timestamps in messages
written to the slow query log file (as well as to the general
query log file and the error log). It does not affect the time
zone of general query log and slow query log messages written to
log tables, but rows retrieved from those tables can be
converted from the local system time zone to any desired time
zone with CONVERT_TZ() or by
setting the session time_zone
system variable.
默认情况下,副本不会将复制的查询写入慢查询日志。要更改此设置,请启用 log_slow_replica_statements 系统变量。请注意,如果使用基于行的复制(binlog_format=ROW),这些系统变量不会生效。只有当查询以语句格式记录在二进制日志中时(即设置 binlog_format=STATEMENT),或设置 binlog_format=MIXED 并且语句以语句格式记录时,查询才会被添加到副本的慢查询日志中。以行格式记录的慢查询(当 binlog_format=MIXED 设置时)或当 binlog_format=ROW 设置时记录的查询,不会被添加到副本的慢查询日志中,即使启用了 log_slow_replica_statements。By default, a replica does not write replicated queries to the
slow query log. To change this, enable the
log_slow_replica_statements
system variable. Note that if row-based replication is in use
(binlog_format=ROW), these
system variables have no effect. Queries are only added to the
replica's slow query log when they are logged in statement
format in the binary log, that is, when
binlog_format=STATEMENT is set,
or when binlog_format=MIXED is
set and the statement is logged in statement format. Slow
queries that are logged in row format when
binlog_format=MIXED is set, or
that are logged when
binlog_format=ROW is set, are
not added to the replica's slow query log, even if
log_slow_replica_statements is
enabled.
当启用慢查询日志时,服务器会将输出写入由log_output系统变量指定的任何目标。如果启用日志,服务器会打开日志文件并将启动信息写入其中。然而,除非选择了FILE日志目标,否则不会将查询日志写入文件。如果目标为NONE,即使启用了慢查询日志,服务器也不会写入任何查询日志。如果未选择FILE作为输出目标,设置日志文件名对日志记录没有任何影响。When the slow query log is enabled, the server writes output to
any destinations specified by the
log_output system variable. If
you enable the log, the server opens the log file and writes
startup messages to it. However, further logging of queries to
the file does not occur unless the FILE log
destination is selected. If the destination is
NONE, the server writes no queries even if
the slow query log is enabled. Setting the log file name has no
effect on logging if FILE is not selected as
an output destination.
如果启用了慢查询日志,并且选择了 FILE 作为输出目标,则写入日志的每条语句前都会有一行以 # 字符开头,并包含以下字段(所有字段在同一行):If the slow query log is enabled and FILE is
selected as an output destination, each statement written to the
log is preceded by a line that begins with a
# character and has these fields (with all
fields on a single line):
Query_time: durationQuery_time:duration语句执行时间(秒)。The statement execution time in seconds.
Lock_time: durationLock_time:duration获取锁的时间(以秒为单位)。The time to acquire locks in seconds.
Rows_sent: NRows_sent:N发送给客户端的行数。The number of rows sent to the client.
Rows_examined:Rows_examined:服务器层检查的行数(不包括存储引擎内部的任何处理)。The number of rows examined by the server layer (not counting any processing internal to storage engines).
启用 log_slow_extra 系统变量会导致服务器在 FILE 输出中写入下列额外字段,除了前面列出的那些字段外(TABLE 输出不受影响)。一些字段描述涉及状态变量名称。有关更多信息,请查阅状态变量描述。然而,在慢查询日志中,这些计数器是按语句计算的值,而不是按会话累积的值。Enabling the log_slow_extra
system variable causes the server to write the following extra
fields to FILE output in addition to those
just listed (TABLE output is unaffected).
Some field descriptions refer to status variable names. Consult
the status variable descriptions for more information. However,
in the slow query log, the counters are per-statement values,
not cumulative per-session values.
Thread_id: IDThread_id:ID语句线程标识符。The statement thread identifier.
Errno: error_numberErrno:error_number语句错误编号,如果没有错误发生则为 0。The statement error number, or 0 if no error occurred.
Killed: NKilled:N如果语句被终止,则指示原因的错误号,如果语句正常终止,则为 0。If the statement was terminated, the error number indicating why, or 0 if the statement terminated normally.
Bytes_received: NBytes_received:N该语句的
Bytes_received值。TheBytes_receivedvalue for the statement.Bytes_sent: NBytes_sent:N该语句的
Bytes_sent值。TheBytes_sentvalue for the statement.Read_first: NRead_first:N该语句的
Handler_read_first值。TheHandler_read_firstvalue for the statement.Read_last: NRead_last:N该语句的
Handler_read_last值。TheHandler_read_lastvalue for the statement.Read_key: NRead_key:N该语句的
Handler_read_key值。TheHandler_read_keyvalue for the statement.Read_next: NRead_next:N该语句的
Handler_read_next值。TheHandler_read_nextvalue for the statement.Read_prev: NRead_prev:N该语句的
Handler_read_prev值。TheHandler_read_prevvalue for the statement.Read_rnd: NRead_rnd:N该语句的
Handler_read_rnd值。TheHandler_read_rndvalue for the statement.Read_rnd_next: NRead_rnd_next:N该语句的
Handler_read_rnd_next值。TheHandler_read_rnd_nextvalue for the statement.Sort_merge_passes: NSort_merge_passes:N该语句的
Sort_merge_passes值。TheSort_merge_passesvalue for the statement.Sort_range_count: NSort_range_count:N该语句的
Sort_range值。TheSort_rangevalue for the statement.Sort_rows: NSort_rows:NSort_scan_count: NSort_scan_count:NCreated_tmp_disk_tables: NCreated_tmp_disk_tables:N该语句的
Created_tmp_disk_tables值。TheCreated_tmp_disk_tablesvalue for the statement.Created_tmp_tables: NCreated_tmp_tables:N语句的
Created_tmp_tables值。TheCreated_tmp_tablesvalue for the statement.Start: timestampStart:timestamp语句执行开始时间。The statement execution start time.
End: timestampEnd:timestamp语句执行结束时间。The statement execution end time.
给定的慢查询日志文件可能包含启用 log_slow_extra 后添加了额外字段的行,也可能包含没有这些额外字段的行。日志文件分析器可以通过字段数量来判断一行是否包含这些额外字段。A given slow query log file may contain a mix of lines with and
without the extra fields added by enabling
log_slow_extra. Log file
analyzers can determine whether a line contains the additional
fields by the field count.
写入慢查询日志文件的每条语句之前,都会有一个 SET 语句,其中包含一个时间戳,表示慢查询语句开始执行的时间。Each statement written to the slow query log file is preceded by
a SET
statement that includes a timestamp, which indicates when the
slow statement began executing.
写入慢查询日志的语句中的密码会被服务器改写,不会以明文形式出现。参见 第 8.1.2.3 节,“密码与日志记录”。Passwords in statements written to the slow query log are rewritten by the server not to occur literally in plain text. See Section 8.1.2.3, “Passwords and Logging”.
无法解析的语句(例如由于语法错误)不会被写入慢查询日志。Statements that cannot be parsed (due, for example, to syntax errors) are not written to the slow query log.