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.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"
@min_callsskips statements that ran fewer times than this (default 1,000), so a one-off report query doesn't crowd out the real problems.@min_ratiois the minimum rows examined per row returned (default 100).
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:
- There isn't one on the column in the
WHERE. - A function or expression on the column:
WHERE DATE(created_at) = ?,WHERE LOWER(email) = ?. - A leading wildcard:
WHERE name LIKE '%foo'. - A type mismatch: comparing a string column to a number makes MySQL convert every row.
- The wrong column order in a composite index for this
WHERE.
Gotchas
- The counts are since the last restart (or the last time the table was truncated). To see what's expensive now rather than over the last month, keep snapshots: AWR for MySQL.
- Some statements are meant to read a lot: reports, aggregates (
COUNT(*)sends one row), batch jobs. Judge them by how often they run and when.
What it reads
performance_schema.events_statements_summary_by_digest
Related
- MySQL CPU at 90% but the slow query log is empty
- AWR for MySQL: snapshots of performance_schema statement digests
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.
New fixes and field notes by email, about once a month.