How to Debug Queries in MySQL

Galaxy Glossary

How do I debug slow or incorrect queries in MySQL?

Techniques and tools to quickly detect and fix logic or performance issues in MySQL queries.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

What is the fastest way to see why a MySQL query is slow?

Run EXPLAIN or EXPLAIN ANALYZE before your query to view the execution plan, index usage, and row estimates in under a second.

How do I read the EXPLAIN output?

Focus on type (access method), rows (estimated row count), and Extra (notes like "Using filesort"). High row counts or full table scans signal missing indexes.

Can I profile runtime hotspots?

Enable session profiling, then execute the query.SHOW PROFILE lists stages (parse, send data) with microsecond timing so you can spot bottlenecks.

How do I capture actual latency per step?

MySQL 89 provides EXPLAIN ANALYZE, which returns execution time for each plan node. Compare time-consuming steps with their estimated cost to validate the planner.

Where do I find lock-related slowdowns?

Query information_schema.innodb_trx and performance_schema.metadata_locks for blocking sessions.Investigate long SLEEP or LOCK_WAIT events.

How can I log only slow queries?

Set long_query_time = 1 and enable the slow_query_log. MySQL writes queries exceeding one second to a separate log file for later analysis.

Is there a way to test indexes without creating them?

Use EXPLAIN FORMAT=JSON; the potential_keys array lists candidate indexes.You can also create indexes invisibly (CREATE INDEX idx ON Orders(order_date) INVISIBLE) to test plans without affecting production reads.

Best practices for debugging queries

1) Narrow your SELECT list to needed columns. 2) Filter early with sargable predicates. 3) Compare schema stats with ANALYZE TABLE before benchmarking. 4) Test on production-like data volumes.

.

Why How to Debug Queries in MySQL is important

How to Debug Queries in MySQL Example Usage


EXPLAIN ANALYZE SELECT c.name, SUM(o.total_amount) AS lifetime_value\nFROM Customers c\nJOIN Orders o ON o.customer_id = c.id\nWHERE c.id = 42;

How to Debug Queries in MySQL Syntax


1. EXPLAIN SELECT ...\n   EXPLAIN [EXTENDED] [FORMAT=JSON]\n   EXPLAIN ANALYZE SELECT ...\n\n2. SHOW PROFILE [FOR QUERY n] [ALL | CPU | BLOCK IO | CONTEXT SWITCHES] ;\n\n3. SET PROFILING = 1;\n\n4. Slow query logging\n   SET GLOBAL slow_query_log = 1;\n   SET GLOBAL long_query_time = 1;\n\n5. Example (ecommerce)\n   EXPLAIN SELECT p.name, oi.quantity\n   FROM Orders o\n   JOIN OrderItems oi ON oi.order_id = o.id\n   JOIN Products p ON p.id = oi.product_id\n   WHERE o.id = 123;

Common Mistakes

Frequently Asked Questions (FAQs)

Does EXPLAIN change my data?

No, EXPLAIN only parses and plans the query; it never modifies or reads table data.

Why does EXPLAIN show Using filesort?

Filesort means MySQL will sort rows outside an index. Add a composite index covering the ORDER BY columns in order.

Can I debug INSERT or UPDATE statements?

Yes. Prepend EXPLAIN to UPDATE or DELETE to see affected rows and index usage. For INSERT ... SELECT, run EXPLAIN on the SELECT part.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.