All pages
Powered by GitBook
1 of 4

Loading...

Loading...

Loading...

Loading...

Slow Query Log

Utilize the slow query log in MariaDB Server. This section helps you identify and optimize inefficient queries, improving overall database performance and responsiveness.

Slow Query Log Overview

The slow query log is a record of SQL queries that took a long time to perform.

Note that, if your queries contain user's passwords, the slow query log may contain passwords too. Thus, it should be protected.

The number of rows affected by the slow query are also recorded in the slow query log.

Enabling the Slow Query Log

The slow query log is disabled by default.

To enable the slow query log, set the slow_query_log system variable (or, from MariaDB 10.11, log_slow_query) to 1. It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

Configuring the Slow Query Log Filename

By default, the slow query log is written to ${hostname}-slow.log in the directory. However, this can be changed.

One way to configure the slow query log filename is to set the system variable (or, from , ). It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

If it is a relative path, then the is relative to the directory.

However, the system variable can also be an absolute path. For example:

Another way to configure the slow query log filename is to set the option, which configures MariaDB to use a common prefix for all log files (e.g. slow query log, , , , etc.). The slow query log filename will be built by adding -slow.log to this prefix. This option cannot be set dynamically. It can be set in a server in an prior to starting up the server. For example:

The cannot be an absolute path. The log file name is relative to the directory.

Choosing the Slow Query Log Output Destination

The slow query log can either be written to a file on disk, or it can be written to the table in the database. To choose the slow query log output destination, set the system variable.

Writing the Slow Query Log to a File

The slow query log is output to a file by default. However, it can be explicitly chosen by setting the system variable to FILE. It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

Writing the Slow Query Log to a Table

The slow query log can either be written to the table in the database by setting the system variable to TABLE. It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

Some rows in this table might look like this:

See for more information.

Disabling the Slow Query Log for a Session

A user can disable logging to the slow query log for a connection by setting the system variable (or, from , ) to 0. For example:

Disabling the Slow Query Log for Specific Statements

It is possible to disable logging to the slow query log for specific types of statements by setting the system variable. This option cannot be set dynamically. It can be set in a server in an prior to starting up the server. For example:

Configuring the Slow Query Log Time

The time that defines a slow query can be configured by setting the system variable (or, from , ). It uses a units of seconds, with an optional milliseconds component. The default value is 10. It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

Logging Queries That Don't Use Indexes

It can be beneficial to log queries that don't use indexes to the slow query log, since queries that don't use indexes can usually be optimized either by adding an index or by doing a slight rewrite. The slow query log can be configured to log queries that don't use indexes regardless of their execution time by adding the option "not_using_index" to or setting the system variable to 1. It can be changed dynamically with . Some examples:

It can also be set in a server in an prior to starting up the server. For example:

As a significant number of queries can run quickly even without indexes, you can use the system variable (or, from , ) with to limit the logged queries to those having a material impact on the server.

Excluding Queries That Examine Fewer Than a Minimum Row Limit

It can be beneficial to exclude queries that examine fewer than a minimum number of rows from the log. This can be done by setting the system variable, or, from , . It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

Logging Slow Administrative Statements

By default, the Slow Query Log logs administrative statements. To disable logging of administrative statements, remove "admin" from the system variable or alternatively set the system variable to OFF. The Slow Query Log considers the following statements administrative: , , , , , , and . In and later, this also includes statements.

You can dynamically enable this feature using a statement and setting it for just the current connection with LOCAL. Some examples:

It can also be set in a server in an prior to starting up the server. For example:

Enabling the Slow Query Log for Specific Criteria

It is possible to enable logging to the slow query log for queries that meet specific criteria by configuring the system variable. It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

You can find all options for log_slow_filter at or at .

Throttling the Slow Query Log

The slow query log can create a lot of I/O, so it can be beneficial to throttle it in some cases. The slow query log can be throttled by configuring the system variable. It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

Configuring the Slow Query Log Verbosity

There are a few optional pieces of information that can be included in the slow query log for each query. This optional information can be included by configuring the system variable. It can be changed dynamically with . For example:

It can also be set in a server in an prior to starting up the server. For example:

It is possible to have .

Viewing the Slow Query Log

Slow query logs written to file can be viewed with any text editor, or you can use the tool to ease the process by summarizing the information.

Queries that you find in the log are key queries to try to optimize by constructing a or by making .

For queries that appear in the log that cannot be optimized in the above ways, perhaps because they are simply very large selects, due to slow hardware, or very high lock/cpu/io contention, using shard/clustering/load balancing solutions, better hardware, or stats tables may help to improve these queries.

Slow query logs written to table can be viewed by querying the table.

Variables Related to the Slow Query Log

  • - enable/disable the slow query log. Renamed to from .

  • - how the output will be written

  • . Whether to log OPTIMIZE, ANALYZE, ALTER and other administrative statements to the slow log. Deprecated from , use without admin.

  • - name of the slow query log file. Renamed to

Rotating the Slow Query Log on Unix and Linux

Unix and Linux distributions offer the utility, which makes it very easy to rotate log files. See for more information on how to use this utility to rotate the slow query log.

This page is licensed: CC BY-SA / Gnu FDL

from
.
  • long_query_time - time in seconds/microseconds defining a slow query. Renamed to log_slow_query_time from MariaDB 10.11.0.

  • log_queries_not_using_indexes - whether to log queries that don't use indexes

  • log_slow_admin_statements - whether to log certain admin statements

  • log_slow_disabled_statements - types of statements that should not be logged in the slow query log

  • min_examined_row_limit - minimum rows a query must examine to be slow. Renamed to log_slow_min_examined_row_limit from MariaDB 10.11.0.

  • log_slow_rate_limit - permits a fraction of slow queries to be logged

  • log_slow_verbosity - amount of detail in the log

  • log_slow_filter - limit which queries to log

  • log_slow_slave_statements - log slow statements executed by replica thread to the slow log if it is open.

  • SET GLOBAL
    option group
    option file
    datadir
    slow_query_log_file
    MariaDB 10.11
    log_slow_query_file
    SET GLOBAL
    option group
    option file
    slow_query_log_file
    datadir
    slow_query_log_file
    log-basename
    general query log
    error log
    binary logs
    option group
    option file
    log-basename
    datadir
    slow_log
    mysql
    log_output
    log_output
    SET GLOBAL
    option group
    option file
    slow_log
    mysql
    log_output
    SET GLOBAL
    option group
    option file
    Writing logs into tables
    slow_query_log
    MariaDB 10.11
    log_slow_query
    log_slow_disabled_statements
    option group
    option file
    long_query_time
    MariaDB 10.11
    log_slow_query_time
    SET GLOBAL
    option group
    option file
    log_slow_filter
    log_queries_not_using_indexes
    SET GLOBAL
    option group
    option file
    min_examined_row_limit
    MariaDB 10.11
    log_slow_min_examined_row_limit
    log_queries_not_using_indexes
    min_examined_row_limit
    MariaDB 10.11
    log_slow_min_examined_row_limit
    SET GLOBAL
    option group
    option file
    log_slow_filter
    log_slow_admin_statements
    ALTER TABLE
    ANALYZE TABLE
    CHECK TABLE
    CREATE INDEX
    DROP INDEX
    OPTIMIZE TABLE
    REPAIR TABLE
    ALTER SEQUENCE
    SET GLOBAL
    option group
    option file
    log_slow_filter
    SET GLOBAL
    option group
    option file
    log_slow_filter system variable
    Slow Query Log Extended Statistics
    log_slow_rate_limit
    SET GLOBAL
    option group
    option file
    log_slow_verbosity
    SET GLOBAL
    option group
    option file
    EXPLAIN output printed in the slow query log
    mariadb-dumpslow
    more efficient query
    better use of indexes
    slow_log
    slow_query_log
    log_slow_query
    MariaDB 10.11
    log_output
    log_slow_admin_statements
    log_slow_filter
    slow_query_log_file
    log_slow_query_file_name
    logrotate
    Rotating Logs on Unix and Linux
    MariaDB 10.11.0
    SET GLOBAL slow_query_log=1;
    [mariadb]
    ...
    slow_query_log
    SET GLOBAL slow_query_log_file='mariadb-slow.log';
    [mariadb]
    ...
    slow_query_log
    slow_query_log_file=mariadb-slow.log
    [mariadb]
    ...
    slow_query_log
    slow_query_log_file=/var/log/mysql/mariadb-slow.log
    [mariadb]
    ...
    log-basename=mariadb
    slow_query_log
    SET GLOBAL log_output='FILE';
    [mariadb]
    ...
    log_output=FILE
    slow_query_log
    slow_query_log_file=slow-queries.log
    SET GLOBAL log_output='TABLE';
    [mariadb]
    ...
    log_output=TABLE
    slow_query_log
    SELECT * FROM mysql.slow_log\G
    ...
    *************************** 2. row ***************************
        start_time: 2014-11-11 07:56:28.721519
         user_host: root[root] @ localhost []
        query_time: 00:00:12.000215
         lock_time: 00:00:00.000000
         rows_sent: 1
     rows_examined: 0
                db: test
    last_insert_id: 0
         insert_id: 0
         server_id: 1
          sql_text: SELECT SLEEP(12)
         thread_id: 74
    ...
    SET SESSION slow_query_log=0;
    [mariadb]
    ...
    log_output=FILE
    general_log
    general_log_file=queries.log
    log_slow_disabled_statements='admin,call,slave,sp'
    SET GLOBAL long_query_time=5.0;
    [mariadb]
    ...
    log_output=FILE
    slow_query_log
    slow_query_log_file=slow-queries.log
    long_query_time=5.0
    SET @@log_slow_filter=concat(@@log_slow_filter,",not_using_index");
    SET GLOBAL log_queries_not_using_indexes=ON;
    [mariadb]
    ...
    log_output=FILE
    slow_query_log
    slow_query_log_file=slow-queries.log
    long_query_time=5.0
    log_queries_not_using_indexes=ON
    SET GLOBAL min_examined_row_limit=100000;
    [mariadb]
    ...
    log_output=FILE
    slow_query_log
    slow_query_log_file=slow-queries.log
    long_query_time=5.0
    min_examined_row_limit=100000
    SET SESSION log_slow_filter=replace(@@log_slow_filter,"admin","");
    SET GLOBAL log_slow_admin_statements=ON;
    [mariadb]
    ...
    log_output=FILE
    slow_query_log
    slow_query_log_file=slow-queries.log
    long_query_time=5.0
    log_slow_admin_statements=ON
    SET GLOBAL log_slow_filter='filesort,filesort_on_disk,tmp_table,tmp_table_on_disk';
    [mariadb]
    ...
    log_output=FILE
    slow_query_log
    slow_query_log_file=slow-queries.log
    long_query_time=5.0
    log_slow_filter=filesort,filesort_on_disk,tmp_table,tmp_table_on_disk
    SET GLOBAL log_slow_rate_limit=5;
    [mariadb]
    ...
    log_output=FILE
    slow_query_log
    slow_query_log_file=slow-queries.log
    long_query_time=5.0
    log_slow_rate_limit=5
    SET GLOBAL log_slow_verbosity='full';
    [mariadb]
    ...
    log_output=FILE
    slow_query_log
    slow_query_log_file=slow-queries.log
    long_query_time=5.0
    log_slow_verbosity=query_plan,explain,engine

    log_slow_always_query_time System Variable

    • Description: Queries slower than log_slow_always_query_time are not affected by log_slow_rate_limit or log_slow_min_examined_row_limit. Query will be logged to the slow query log if the execution time of the query is longer than log_slow_query_time and log_slow_always_query_time. The argument will be treated as a decimal value with microsecond precision.

    • Command line: --log-slow-always-query-time=num

    • Scope: Global, Session

    • Dynamic: Yes

    • Data Type: numeric (double)

    • Default Value: 31536000.000000

    • Range: 0 to 31536000

    • Introduced:

    This page is licensed: CC BY-SA / Gnu FDL

    EXPLAIN in the Slow Query Log

    Switching it On

    EXPLAIN output can be switched on by specifying the "explain" keyword in the log_slow_verbosity system variable. Alternatively, you can set with the log-slow-verbosity command line argument.

    [mysqld]
    log-slow-verbosity=query_plan,explain

    EXPLAIN output will only be recorded if the slow query log is written to a file (and not to a table - see Writing logs into tables). This limitation also applies to other extended statistics that are written into the slow query log.

    What it Looks Like

    When explain recording is on, slow query log entries look like this:

    EXPLAIN lines start with `

    explain:`.

    See Also

    This page is licensed: CC BY-SA / Gnu FDL

    MDEV-407
    # Time: 131112 17:03:32
    # User@Host: root[root] @ localhost []
    # Thread_id: 2  Schema: dbt3sf1  QC_hit: No
    # Query_time: 5.524103  Lock_time: 0.000337  Rows_sent: 1  Rows_examined: 65633
    #
    # explain: id   select_type     table   type    possible_keys   key     key_len ref     rows    Extra
    # explain: 1    SIMPLE  nation  ref     PRIMARY,n_name  n_name  26      const   1       Using where; Using index
    # explain: 1    SIMPLE  customer        ref     PRIMARY,i_c_nationkey   i_c_nationkey   5       dbt3sf1.nation.n_nationkey      3145    Using index
    # explain: 1    SIMPLE  orders  ref     i_o_custkey     i_o_custkey     5       dbt3sf1.customer.c_custkey      7       Using index
    #
    SET TIMESTAMP=1384261412;
    SELECT COUNT(*) FROM customer, orders, nation WHERE c_custkey=o_custkey AND c_nationkey=n_nationkey AND n_name='GERMANY';
    MariaDB 10.3
    MariaDB 11.0
    MariaDB 11.7