Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 188Data · MySQL

How to find slow MySQL queries

WordPress, a shop or an ERP gets slow, and top shows mysqld at 300 % CPU. MySQL has two good tools to find the guilty query: the slow query log (every statement over a time limit, with how many rows it read) and performance_schema (a live summary of all statement types). Then EXPLAIN shows whether an index is missing. This how-to turns both on, reads the results, and fixes a query safely.

  • 5 Lessen
  • 2 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

188.1

The quick answer

sudo mysql -e "SET PERSIST slow_query_log = ON; SET PERSIST long_query_time = 0.5;"
sudo mysql -e "SHOW VARIABLES LIKE 'slow_query_log_file';"
mysqldumpslow -s t -t 10 /var/lib/mysql/db01-slow.log

188.2

Step by step

  1. Turn on the slow log (quick answer) — SET PERSIST (MySQL 8) changes the running server and keeps the value after a restart. On MariaDB, set it in the config file (slow_query_log = 1, long_query_time = 0.5) and use SET GLOBAL for now.
  2. Summarise the log — mysqldumpslow groups similar queries; -s t sorts by total time. Look at Count, Time and Rows (rows examined vs rows sent). For deeper reports use pt-query-digest from Percona Toolkit. On MariaDB, mysqldumpslow can end with Died at … line 182 on some log lines — the summary printed before it is still right.
  3. Live summary from performance_schema — no log needed (on by default in MySQL 8):
sudo mysql -e "SELECT LEFT(digest_text,70) AS query, count_star AS calls, ROUND(sum_timer_wait/1e12,1) AS total_s, ROUND(avg_timer_wait/1e9,1) AS avg_ms, sum_rows_examined AS rows_read FROM performance_schema.events_statements_summary_by_digest ORDER BY sum_timer_wait DESC LIMIT 10;"

Timers are in picoseconds (÷ 10¹² = seconds).

  1. Full table scans — the sys schema has ready views:
sudo mysql -e "SELECT query, db, exec_count, no_index_used_count FROM sys.statements_with_full_table_scans ORDER BY no_index_used_count DESC LIMIT 10;"
  1. What runs right now — and stop one query (not the whole connection):
sudo mysql -e "SELECT id, user, db, time, state, LEFT(info,60) FROM information_schema.processlist WHERE command <> 'Sleep' ORDER BY time DESC;"
sudo mysql -e "KILL QUERY 4711;"
  1. Explain the query — with real values:
sudo mysql shop -e "EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 4711 ORDER BY created_at DESC LIMIT 20\G"

EXPLAIN ANALYZE needs MySQL 8.0.18+; on MariaDB use ANALYZE SELECT …, or plain EXPLAIN. Bad signs: type: ALL ("Table scan"), a huge rows number, "Using filesort" on big tables.

  1. Add the index online — InnoDB builds it without blocking reads and writes:
sudo mysql shop -e "ALTER TABLE orders ADD INDEX idx_customer_created (customer_id, created_at), ALGORITHM=INPLACE, LOCK=NONE;"
  1. Turn the threshold back up after the analysis (for example 2 s) — a very low long_query_time writes a big log on busy servers. Add the slow log to logrotate.

188.3

Check that it worked

EXPLAIN now shows type: ref with key: idx_customer_created and a small rows value; the query disappears from the top of mysqldumpslow and performance_schema; CPU of mysqld goes down.

188.4

Common problems

ProblemCauseFix
Slow log stays emptyThreshold too high, or the log file path not writable.long_query_time = 0.5; check slow_query_log_file and mysqld rights.
SET PERSIST gives a syntax errorMariaDB or MySQL 5.7.Use the config file and SET GLOBAL.
Log grows very fastlong_query_time = 0 or log_queries_not_using_indexes on.Raise the threshold; turn that option off; logrotate.
ALTER TABLE … LOCK=NONE refusedThis change cannot be done online.Plan a maintenance window, or use pt-online-schema-change / gh-ost.
Index not usedLeading column not in the WHERE, or a function on the column.Put the filtered column first; avoid functions; ANALYZE TABLE.
performance_schema tables emptyTurned off in the config.performance_schema = ON (restart).

188.5

Go further

1What is the difference between the slow query log and performance_schema?

The slow log writes each slow statement to a file; performance_schema keeps a live summary of all statement types in memory.

2Why use KILL QUERY instead of KILL?

KILL QUERY stops only the running statement; KILL ends the whole connection of the app.

Commando's in deze les

Alle commando's in dit hoofdstuk