// SQL snippet

Read recent slow queries from the MySQL slow log table

The slow query log as a table you can query: newest entries first, with query_time, rows examined versus sent, and the SQL text.

Main statement
MySQL
También cubre
PostgreSQL

The task

MySQL's slow query log captures every statement slower than long_query_time. With log_output = TABLE, entries land in mysql.slow_log instead of a file, so SQL can read them: the newest slow queries, their execution time, rows examined versus rows sent, and the statement text, all without shell access to the server.

The snippet

Written for MySQL. Versions for PostgreSQL are under Variations.

MySQL
SELECT start_time, user_host, query_time, lock_time,
       rows_sent, rows_examined,
       LEFT(CONVERT(sql_text USING utf8mb4), 200) AS sql_text
FROM mysql.slow_log
ORDER BY start_time DESC
LIMIT 20;

Cómo funciona

slow_log stores entries as they arrive when slow_query_log is ON and log_output is TABLE. rows_examined versus rows_sent is the quick quality check: examining 100,000 rows to send 10 usually means a missing index. mysql.slow_log uses the CSV storage engine, which supports no indexes, so every query against it reads the whole table and sorts it; keep it small with TRUNCATE TABLE mysql.slow_log. sql_text is a binary column, hence the CONVERT to readable text. The default long_query_time of 10 seconds misses most problems on an OLTP system; 1 second, or a fraction such as 0.5, catches realistic offenders. SET GLOBAL changes are lost at restart unless you also put them in the configuration file or use SET PERSIST.

Variations

Enable the slow log into a table (run as admin)

SET GLOBAL slow_query_log = 'ON';
SET GLOBAL log_output = 'TABLE';
SET GLOBAL long_query_time = 1;

PostgreSQL alternative: pg_stat_statements over a threshold

PostgreSQL
SELECT calls,
       ROUND(mean_exec_time::numeric, 1) AS mean_ms,
       LEFT(query, 140) AS query
FROM pg_stat_statements
WHERE mean_exec_time > 500
ORDER BY mean_exec_time DESC
LIMIT 20;

Questions that come up

Is the slow log table a performance risk?

It costs more than the file. Every logged statement becomes a row inserted into a CSV table, and the table grows without limit until someone truncates it. With a low long_query_time on a busy server that is a lot of writes. FILE output (log_output = 'FILE', or 'FILE,TABLE' for both) plus mysqldumpslow or pt-query-digest is the usual production setup; the table form is convenient on development and moderately loaded servers.

What is a healthy rows_examined / rows_sent ratio?

Near 1:1 for point lookups and a small multiple for well-indexed range queries. Ratios in the thousands mean the statement read far more rows than it returned, usually a full table or index scan where a better index would have gone straight to the rows. Aggregates are the exception: SELECT COUNT(*) over a million rows returns one row and is not necessarily badly indexed.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.