How do you find slow queries in MySQL?

IntermediateHands-onMySQL
Answer

Short answer

Enable the slow query log to record queries that take longer than a threshold, check SHOW PROCESSLIST for what's running right now, analyse candidates with EXPLAIN, and summarise the slow log with pt-query-digest or Performance Schema / sys views. On RDS, use Performance Insights.

Slow query log

SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;                -- seconds
SET GLOBAL log_queries_not_using_indexes = 'ON';
SHOW VARIABLES LIKE 'slow_query_log_file';     -- /var/lib/mysql/host-slow.log
pt-query-digest /var/lib/mysql/host-slow.log | head -50    # top queries by total time

What's running right now

SHOW FULL PROCESSLIST;                          -- look for long Time values, "Sending data", "Locked"
SELECT * FROM sys.statements_with_full_table_scans LIMIT 10;
SELECT * FROM sys.statement_analysis ORDER BY total_latency DESC LIMIT 10;

Analyse one query

EXPLAIN ANALYZE SELECT * FROM orders WHERE status = 'NEW' ORDER BY created_at;

Look for type: ALL (full scan), large rows estimates, Using filesort, Using temporary.

Typical fixes

Add or adjust an index, rewrite the query (avoid SELECT *, functions on indexed columns), paginate, or cache results.

What to say in the interview

"Slow query log with long_query_time, digested with pt-query-digest to find the worst offenders by total time, PROCESSLIST for live issues, then EXPLAIN ANALYZE to see why — usually a missing index or a full scan. On RDS, Performance Insights gives the same view."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: MySQL basics

More on MySQL