slow_query

Overview

Slow queries logs are written to tidb-slow.log (set with slow-query-file in the config) if they are over the slow-threshold (300ms by default). If record-plan-in-slow-log is enabled this will include the execution plan. This uses slowLogEncoder.

Each TiDB node writes its own slow logs.

SLOW_QUERY

   +------------+
   | App or CLI |
   +------------+
         |
     SQL Query
     SLOW_QUERY
         |
     +--------+
     |  TiDB  |
     +--------+
         |
  +---------------+
  | tidb-slow.log |
  +---------------+

The slow log can be viewed directly or via the information_schema.SLOW_QUERY table.

The column definition is in slowQueryCols.

The SlowQueryExtractor helps to extract some predicates of slow_query.

A lot of the other logic can be found in slow_query.go.

This table has RuntimeStats, which adds stats to EXPLAIN ANALYZE output.

CLUSTER_SLOW_QUERY

There is also the information_schema.CLUSTER_SLOW_QUERY table that combines the slow log from all nodes into a single table.

The TiDB Dashboard uses the CLUSTER_SLOW_QUERY table to display the slow queries in a webpage.

                  +----------------+
                  | TiDB Dashboard |
                  |   or App/CLI   |
                  +----------------+
                         |
                      SQL Query
                  CLUSTER_SLOW_QUERY
                         |
                      +--------+
        +--gRPC-------| TiDB 1 |------gRPC--+
        |             +--------+            |
   +--------+            |               +--------+
   | TiDB 0 |            |               | TiDB 2 |
   +--------+            |               +--------+
      |                  |                  |
+---------------+  +---------------+  +---------------+
| tidb-slow.log |  | tidb-slow.log |  | tidb-slow.log |
+---------------+  +---------------+  +---------------+

CLUSTER_SLOW_QUERY is using the coprocessor gRPC interface to send cop requests to the TiDB node. This allows the reuse of query condition push down and column prune, to reduce unnecessary data transfer.

There are no RuntimeStats for this table, see this issue for a request to add that.

Documentation