How to Debug Queries in MariaDB

Galaxy Glossary

How do I debug slow queries in MariaDB?

Debugging queries in MariaDB means tracing execution plans, runtime statistics, and server logs to spot bottlenecks and optimise performance.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

Why debug queries in MariaDB?

Query debugging exposes slow joins, missing indexes, and mis-sized buffers. Pinpointing these issues lets you shorten response times and lower server load.

What tools does MariaDB provide?

Core tools are EXPLAIN/EXPLAIN ANALYZE, SHOW PROFILE, SLOW QUERY LOG, and OPTIMIZER_TRACE. Each shows a different layer of runtime detail.

How to get the execution plan quickly?

Run EXPLAIN or richer EXPLAIN ANALYZE before your SELECT. They return join order, key usage, filtered rows, and estimated vs.actual timings.

Example

EXPLAIN ANALYZE SELECT * FROM Orders WHERE customer_id = 42;

How do I profile a single statement?

Turn on session profiling, run the query, then inspect stage timings.

SET profiling = 1;
SELECT ...

;
SHOW PROFILE FOR QUERY 1;

How to log slow queries?

Enable the slow query log globally or per session, define the threshold, and analyse the log file or mysql.slow_log table.

When should I use OPTIMIZER_TRACE?

Use OPTIMIZER_TRACE to reveal optimizer decisions such as cost calculations and rejected plans, helpful when EXPLAIN is not enough.

Best practices for faster debugging

Create representative test data, run EXPLAIN ANALYZE first, compare estimated vs.actual rows, check for full-table scans, and add or adjust indexes iteratively.

Key takeaways

Start with EXPLAIN ANALYZE, profile only when necessary, keep slow query logging on in staging, and automate log review for continuous insight.

.

Why How to Debug Queries in MariaDB is important

How to Debug Queries in MariaDB Example Usage


-- Find why a customer lookup is slow
EXPLAIN ANALYZE
SELECT o.id, o.total_amount
FROM Orders o
JOIN Customers c ON c.id = o.customer_id
WHERE c.email = 'alice@example.com';

How to Debug Queries in MariaDB Syntax


-- 1. Inspect plan
EXPLAIN [ANALYZE] [FORMAT=JSON] <select_stmt>;
-- 2. Enable per-session profiling
SET profiling = {0|1};
SHOW PROFILE [CPU, BLOCK IO] FOR QUERY <id>;
-- 3. Enable slow query log
SET GLOBAL slow_query_log = 1;
SET GLOBAL long_query_time = 1; -- seconds
-- 4. Optimizer trace
SET optimizer_trace="enabled=on";
SELECT * FROM Orders WHERE id = 123;
SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;

Common Mistakes

Frequently Asked Questions (FAQs)

Does EXPLAIN ANALYZE lock tables?

No, it executes the query but discards the result set, so normal read locks apply—no extra blocking.

Can I debug INSERT or UPDATE statements?

Yes. Use the slow query log for DML operations or wrap them in a transaction and inspect SHOW PROFILE per query ID.

Is OPTIMIZER_TRACE safe in production?

It adds minor overhead and stores JSON in performance_schema. Enable briefly and clear traces after inspection.

Want to learn about other SQL terms?