# Find slow MySQL queries with performance_schema

> Rank MySQL queries by total time with performance_schema and the sys schema, convert picosecond timers to milliseconds, or use the slow query log.

Source: https://sqleye.com/blog/find-slow-queries-in-mysql-performance-schema/ · Published: 2026-10-20 · Last updated: 2026-11-29 · Publisher: CodeROG (https://coderog.com)

To find slow queries in MySQL 8.0 or 8.4, query `performance_schema.events_statements_summary_by_digest`, which aggregates statements by normalised digest, and sort by `SUM_TIMER_WAIT`. Timers are in picoseconds, so divide by 1,000,000,000 to get milliseconds. The slow query log is the alternative when you want individual statements.

## Is the Performance Schema on?

The MySQL manual states that the Performance Schema is enabled by default (8.0 and 8.4). Check it:

```sql
SHOW VARIABLES LIKE 'performance_schema';
```

`ON` means it initialised successfully. The manual only shows setting it at server startup in the configuration file, as `performance_schema=ON` under `[mysqld]`; treat it as a startup option, not something to toggle at runtime.

## How do I rank statements by total time?

`events_statements_summary_by_digest` has one row per schema and digest. A digest is a normalised form of the statement: literals become `?`, comments are removed and whitespace is adjusted, then a SHA-256 hash is computed. So these two statements land in the same row:

```sql
SELECT * FROM orders WHERE customer_id = 10 AND quantity > 20;
SELECT * FROM orders WHERE customer_id = 20 AND quantity > 100;
```

The columns you need, from the manual:

| Column | Meaning |
| --- | --- |
| `SCHEMA_NAME`, `DIGEST` | Grouping columns |
| `DIGEST_TEXT` | The normalised statement text |
| `COUNT_STAR` | Number of occurrences |
| `SUM_TIMER_WAIT`, `MIN_TIMER_WAIT`, `AVG_TIMER_WAIT`, `MAX_TIMER_WAIT` | Latency, in picoseconds |
| `SUM_ROWS_EXAMINED`, `SUM_ROWS_SENT` | Aggregates of the per-statement row counts |
| `SUM_NO_INDEX_USED`, `SUM_NO_GOOD_INDEX_USED` | Aggregates of the per-statement index flags |
| `FIRST_SEEN`, `LAST_SEEN` | When the digest was first and last seen |

The `SUM_` columns follow the manual's rule that a `SUM_xxx` column aggregates the matching `xxx` column of `events_statements_current`.

### How do I get milliseconds?

The manual says Performance Schema times are shown in picoseconds (trillionths of a second). A millisecond is 10^9 picoseconds, so:

```sql
SELECT SCHEMA_NAME,
       DIGEST_TEXT,
       COUNT_STAR,
       ROUND(SUM_TIMER_WAIT / 1000000000, 2) AS total_ms,
       ROUND(AVG_TIMER_WAIT / 1000000000, 2) AS avg_ms,
       SUM_ROWS_EXAMINED,
       SUM_NO_INDEX_USED
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;
```

The values depend entirely on your workload, so there is no sample output here. The manual also notes picosecond values are approximate: use them for ordering and comparison, not as exact measurements.

In the 8.4 manual there is a built-in `FORMAT_PICO_TIME()` that turns picoseconds into a string such as `3.50 ns` or `3.15 min`. Its older cousin `sys.format_time()` is documented as deprecated in favour of it. Check that your server version has it before relying on it; the division above works everywhere.

### What should I look at in the results?

- A high `SUM_TIMER_WAIT` with a low average is a cheap query run very often. Look for an N+1 pattern.
- A high `AVG_TIMER_WAIT` is a slow query. Compare `SUM_ROWS_EXAMINED` with `SUM_ROWS_SENT`: reading millions of rows to return a few is the classic sign of a missing index.
- A non-zero `SUM_NO_INDEX_USED` means some executions performed a table scan without using an index. Per statement, the `NO_INDEX_USED` flag is `1` if the statement performed a table scan without using an index.

To start a fresh measurement window, `TRUNCATE TABLE` on the digest table removes its rows (it does not zero them):

```sql
TRUNCATE TABLE performance_schema.events_statements_summary_by_digest;
```

If the table fills up, statistics for new digests go into a catch-all row where `DIGEST` is `NULL`. The table size is set at startup with `performance_schema_digests_size`.

## Is there a ready-made view in the sys schema?

Yes. `sys.statement_analysis` exists in both 8.0 and 8.4. The manual describes it as listing normalised statements with aggregated statistics, sorted by descending total latency by default:

```sql
SELECT query, db, exec_count, total_latency, avg_latency,
       rows_examined_avg, full_scan
FROM sys.statement_analysis
LIMIT 10;
```

It also exposes `max_latency`, `err_count`, `rows_sent`, `tmp_tables` and a `digest` column. Two memory columns, `max_controlled_memory` and `max_total_memory`, were added in 8.0.31. For how latency values are formatted in the output, run it on your server; the sys view documentation does not show sample output.

## How do I see individual slow statements?

The digest table only has aggregates. For individual recent statements use `events_statements_history_long`. Its consumer is disabled by default, as the `setup_consumers` listing in the manual shows. Enable it, run your workload, then read it:

```sql
UPDATE performance_schema.setup_consumers
SET ENABLED = 'YES'
WHERE NAME = 'events_statements_history_long';

SELECT SQL_TEXT,
       ROUND(TIMER_WAIT / 1000000000, 2) AS duration_ms,
       ROWS_EXAMINED, ROWS_SENT, NO_INDEX_USED
FROM performance_schema.events_statements_history_long
ORDER BY TIMER_WAIT DESC
LIMIT 10;
```

The table is a fixed-size ring: its size comes from `performance_schema_events_statements_history_long_size`, set at startup, so old statements are pushed out. `SQL_TEXT` is limited to 1024 bytes by default (`performance_schema_max_sql_text_length`, startup option). Enabling more consumers costs some overhead, so switch it off when you are done.

## What about the slow query log?

The slow query log writes statements that take longer than a threshold to a file. It is disabled by default. The manual says you can enable it and change its file name at runtime with the global `slow_query_log` and `slow_query_log_file` variables:

```sql
SET GLOBAL slow_query_log = 1;
```

The threshold is `long_query_time`. Its default is 10 (seconds), its minimum is 0, and it can be specified to a resolution of microseconds, so `0.5` means 500 ms. Set it in your option file, for example `long_query_time = 0.5` under `[mysqld]`, and confirm with `SHOW VARIABLES LIKE 'long_query_time'`. The file name defaults to `host_name-slow.log` in the data directory, or set `slow_query_log_file`. Two related options: `log_queries_not_using_indexes` also logs statements that do not use an index (not for tables with fewer than two rows), and `min_examined_row_limit` skips statements that examine fewer rows. `log_output` chooses the destination, and if it does not include `FILE` nothing is written to the file. The `mysqldumpslow` utility summarises a slow log file.

| | Performance Schema | Slow query log |
| --- | --- | --- |
| Default state | On (consumer for long history off) | Off |
| Output | Tables in memory | File (or table via `log_output`) |
| Aggregation | By digest, built in | Needs `mysqldumpslow` or your own tooling |
| Best for | "What costs the most overall?" | "What exceeded my threshold?" |

## How does SQLEye help?

> **Note.**
SQLEye is not released yet and there are no downloads. This describes designed behaviour.

SQLEye is a free desktop app that runs local MySQL and PostgreSQL instances and shows each query as it runs. Its MySQL instances are configured with `performance_schema=ON` and `events_statements_history_long` sized at 10,000 entries; the consumer is enabled only while capture is on for that instance. It reads the history table and shows each statement with its duration, rows sent and examined, and a "No index used" marker taken from `NO_INDEX_USED` / `NO_GOOD_INDEX_USED`. If more statements arrive between reads than the history holds, some can be missed. Capture is off by default.

See [MySQL in SQLEye](/docs/engines/mysql/) for the managed configuration, [slow queries](/docs/live-eye/slow-queries/) for the slow flag, and [capture](/docs/live-eye/capture/) for what changes on the server.
