Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- 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 useSET GLOBALfor now. - Summarise the log —
mysqldumpslowgroups similar queries;-s tsorts by total time. Look atCount,TimeandRows(rows examined vs rows sent). For deeper reports usept-query-digestfrom Percona Toolkit. On MariaDB,mysqldumpslowcan end withDied at … line 182on some log lines — the summary printed before it is still right. - 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).
- Full table scans — the
sysschema 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;"- 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;"- 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.
- 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;"- Turn the threshold back up after the analysis (for example 2 s) — a very low
long_query_timewrites a big log on busy servers. Add the slow log to logrotate.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Slow log stays empty | Threshold 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 error | MariaDB or MySQL 5.7. | Use the config file and SET GLOBAL. |
| Log grows very fast | long_query_time = 0 or log_queries_not_using_indexes on. | Raise the threshold; turn that option off; logrotate. |
ALTER TABLE … LOCK=NONE refused | This change cannot be done online. | Plan a maintenance window, or use pt-online-schema-change / gh-ost. |
| Index not used | Leading column not in the WHERE, or a function on the column. | Put the filtered column first; avoid functions; ANALYZE TABLE. |
| performance_schema tables empty | Turned off in the config. | performance_schema = ON (restart). |
Go further
- Command pages: mysql.
- Next how-tos: Find slow PostgreSQL queries, See who is connected to a database, Back up and restore MySQL, Rotate log files with logrotate.
- Learn it: Data services at work — MySQL. Full guide: MySQL.
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.