There are several options that you can use to configure the slow query log and which queries are logged. As the overhead of having the log enabled goes up with the number of queries you log, a well-configured slow query log is important. Logging “just the right amount” of queries also makes it easier to identify queries of interest.
The slow query log is not enabled by default, and when the log is enabled, the default is only to log nonadministrative queries executed directly on the local instance and where the query takes more than 10 seconds to execute. Table 9-1 summarizes the configuration options you have at your disposal for fine-tuning this behavior. The information includes the default value and whether the option is used in the global scope or session scope or both. The options are listed alphabetically.
Table 9-1. Configuration options for the slow query log Option/Default Value/Scope Description
min_examined_row_limit Default: 0
Scope: Global, Session
Only queries examining more rows than this value will be logged. This can particularly be useful when enabling logging of all queries doing full scans.
log_output Default: FILE Scope: Global
Controls whether the slow query log and general query log are logged to a file, a table, or both or not at all.
log_queries_not_using_
indexes Default: OFF Scope: Global
When enabled, all queries that perform a full table or index scan are logged irrespective of how long they take.
log_short_format Default: OFF
Scope: Global
When enabled, less information is logged. This option can only be set in the configuration file.
log_slow_admin_statements Default: OFF
Scope: Global
When enabled, administrative statements like ALTER TABLE and OPTIMIZE TABLE are eligible for logging.
(continued)
ChAPTEr 9 ThE SLOW QuEry LOG
Option/Default Value/Scope Description log_slow_extra
Default: OFF Scope: Global
When enabled, there is extra information such as the value of the Handler_% status variables for the query. It is only supported when logging to a file and in MySQL 8.0.14 and later.
The main reason to not enable log_slow_extra is if you have scripts that require the old format.
log_slow_slave_statements Default: OFF
Scope: Global
When enabled, replicated statements are also eligible for logging. This only applies for binary log events in statement format.
log_throttle_queries_not_
using_indexes Default: 0 Scope: Global
When you have enabled logging of all queries doing a full scan, this option can throttle the maximum number of times the query can be logged per minute.
log_timestamps Default: UTC Scope: Global
Whether to use uTC or the system time zone for the timestamps. This option also applies to the error log and general query log. It only applies when logging to a file.
long_query_time Default: 10
Scope: Global, Session
The minimum query latency in seconds before a query is logged (unless it is doing full scans and you have enabled logging those queries). Fractional seconds are supported.
Set to 0 to log all queries.
Warning: Logging all queries has a significant overhead and is best done on test systems or for short periods of time.
slow_query_log Default: OFF Scope: Global
Whether to enable the slow query log.
slow_query_log_file
Default: <hostname>-slow.log Scope: Global
The path and filename of the slow query log file. The default location is in the data directory and is named from the hostname of the system.
Table 9-1. (continued)
156
It is recommended to leave log_output at the default and log the events to the file set by slow_query_log_file. It may seem attractive to get the slow query log as a table;
however, the data is in that case saved as comma-separated values (CSV), and queries against the table cannot use indexes. There are also some features such as log_slow_
extra that are not supported with log_output = TABLE.
The options mean that you have fine-grained control of which queries are logged.
All the options except log_short_format can be changed dynamically, so you can make changes as the situation requires. If you feel it can be hard to determine how the options interact, then Figure 9-1 shows a flowchart of the decision process determining whether a query should be logged. (The flowchart is illustrative only – the actual code path is different.)
ChAPTEr 9 ThE SLOW QuEry LOG
Figure 9-1. Flowchart to determine whether a query is logged to the slow log
158
The flow starts with the query type. For administrative and replicated statements, they only proceed if the respective option is enabled. Regular queries first check whether they qualify as not using indexes and then fall back on checking the query execution time (latency). If either condition is fulfilled, it is checked whether enough rows were examined.
Some finer details such as throttling of statements not using indexes are left out of the figure.
Once you have the query settings that you want, you need to look at the events in the log to determine whether any queries need attention.