KB · MySQL · Script

MySQL statements that examine far more rows than they return

MySQL · Script · Verified on MySQL 8.0 and 8.4 · Updated 2026-12-04

What it does

For every statement digest, performance_schema counts the rows MySQL examined and the rows it sent back. A statement that examines 50,000 rows to send back one is usually missing an index, or has one it can't use. This lists the statements over a ratio you choose, sorted by total rows examined. That puts the ones doing the most wasted work first.

digests_examined_vs_sent.sqlMIT · Download ·
-- digests_examined_vs_sent.sql: statements that read far more rows than they return
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/mysql/statements-examining-too-many-rows/
--
-- Usage: mysql -t < digests_examined_vs_sent.sql
--        mysql -t -e "set @min_calls = 100, @min_ratio = 50; source digests_examined_vs_sent.sql"
--
-- @min_calls  ignore statements run fewer times than this (default 1000)
-- @min_ratio  rows examined per row sent, at least (default 100)
--
-- A statement that examines 50,000 rows to send back 1 is usually missing an index, or has
-- one it can't use (a function on the column, a leading wildcard, an implicit type cast).
-- UPDATE and DELETE send no rows, so they're judged by rows examined per row affected.

SET @min_calls = IFNULL(@min_calls, 1000);
SET @min_ratio = IFNULL(@min_ratio, 100);

SELECT IFNULL(SCHEMA_NAME, '')                                           AS schema_name,
       LEFT(DIGEST, 12)                                                  AS digest,
       COUNT_STAR                                                        AS calls,
       ROUND(SUM_ROWS_EXAMINED / COUNT_STAR)                             AS exam_per_call,
       ROUND((SUM_ROWS_SENT + SUM_ROWS_AFFECTED) / COUNT_STAR, 1)        AS out_per_call,
       ROUND(SUM_ROWS_EXAMINED / GREATEST(SUM_ROWS_SENT + SUM_ROWS_AFFECTED, 1)) AS exam_per_out,
       SUM_NO_INDEX_USED                                                 AS no_index_used,
       ROUND(SUM_TIMER_WAIT / 1e12, 1)                                   AS total_sec,
       LEFT(REPLACE(DIGEST_TEXT, '\n', ' '), 70)                         AS statement
FROM performance_schema.events_statements_summary_by_digest
WHERE DIGEST IS NOT NULL
  AND COUNT_STAR >= @min_calls
  AND SUM_ROWS_EXAMINED / GREATEST(SUM_ROWS_SENT + SUM_ROWS_AFFECTED, 1) >= @min_ratio
ORDER BY SUM_ROWS_EXAMINED DESC
LIMIT 25;

curl -O https://hashjoin.com/kb/mysql/statements-examining-too-many-rows/digests_examined_vs_sent.sql

Usage

mysql -t < digests_examined_vs_sent.sql
mysql -t -e "set @min_calls = 100, @min_ratio = 50; source digests_examined_vs_sent.sql"

UPDATE and DELETE send no rows, so the script counts rows affected as their output.

Reading the output

From the lab: 1,200 lookups by an unindexed column on a 200,000-row table, plus 2,000 runs of an indexed query:

+-------------+--------------+-------+---------------+--------------+--------------+---------------+-----------+---------------------------------------------------+
| schema_name | digest       | calls | exam_per_call | out_per_call | exam_per_out | no_index_used | total_sec | statement                                         |
+-------------+--------------+-------+---------------+--------------+--------------+---------------+-----------+---------------------------------------------------+
| kblab       | 0525724e2c09 |  1200 |        200000 |          1.0 |       200000 |          1200 |      22.6 | SELECT `id` FROM `kblab` . `items` WHERE NAME = ? |

Every call reads the whole table to return one row, and no_index_used says so for all 1,200. The indexed query (40 rows examined for 20 returned) isn't listed.

What to do with a row

EXPLAIN the statement with realistic values. The usual reasons an index isn't used:

Gotchas

What it reads

performance_schema.events_statements_summary_by_digest

Running this fleet-wide, by hand?

HASHJOIN builds the control plane that runs upgrades, failovers and repairs like this one across every host, with a lock per unit, a drain on Ctrl-C and a log you can read at 3 a.m.