-- 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;