MySQL diagnosis: slow log, optimizer trace, performance schema
Find the slow statement, ask the optimizer why it chose a plan, and see where the time went. A free visual lesson with sample data.
More on MySQL and InnoDB
- InnoDB storage: clustered pages, change buffer, adaptive hash, doublewrite
- InnoDB redo, the binlog and the two-phase commit between them
- InnoDB read views, undo purge and the history list
- InnoDB Gap Locks and Next-Key Locks Explained
- A primary key is known before execution starts
- MySQL EXPLAIN Explained: const, ref, range, index, ALL
- MySQL settings that change query speed
Keep learning
Overview
Slow query log Statements over a time or row threshold, after they finish General log Every statement received Short debugging sessions on a test server The plan and estimates, without running Checking an index or rewrite EXPLAIN ANALYZE Plan plus real rows and timings, by running it Cost of the query Estimate versus reality Optimizer trace Every candidate and its cost for one statement A surprising plan choice performance_schema / sys Aggregated statements, waits, locks, in memory Low (a few percent) Live ranking and lock questions SHOW PROFILE Per-stage timings of one statement Error log Startup, crashes, deadlocks, aborted connections Anything that is not a single slow query chapters · mysql and innodb Trace logs: slow log, optimizer trace and performance schema When a database feels slow, guessing wastes days. MySQL can tell you exactly what it did: which statements were slow, what plan each used, why the optimizer chose it, and where time went inside the server. Each tool answers a different question and has a different cost, so the skill is knowing which to reach for first and which never to leave on in production.
1. The slow query log: find what is worth fixing
One entry is an anecdote. The useful view is a digest that groups identical statement shapes: mysqldumpslow -s t ships with MySQL, and pt-query-digest from Percona Toolkit adds percentiles and a ranked report. Rank by total time, because a 0.3 second query that runs twelve thousand times costs more than a 14 second one that runs six times, then look at rows examined against rows sent. Try it below.
2. EXPLAIN, EXPLAIN ANALYZE and the optimizer trace
Once you have a candidate, EXPLAIN shows the plan (see reading EXPLAIN). In 8.0.18 and later EXPLAIN ANALYZE also runs it and prints, per step, estimated rows, actual rows, loops and time. A step whose estimate is off by a factor of ten or more points at stale statistics or skewed data (the stale statistics case). Remember that it really executes the statement. When the plan itself is a surprise, for example an index exists but is ignored, the optimizer trace shows the reasoning. It is per session and keeps the last statements only: The JSON is long. Four parts carry most of the story: which indexes were considered, the cost per range alternative, the table-scan baseline, and the final chosen path. Open them in the trimmed example. For how costs are computed and how the join order is picked, read the optimizer chapter.
3. performance_schema and the sys schema
performance_schema is MySQL's built-in instrumentation, on by default in 8.0 and held in memory. The sys schema wraps it in readable views. Four to remember: Where to look inside the server Which statement shapes cost the most overall? performance_schema.events_statements_summary_by_digest, or sys.statement_analysis (sorted by total latency, with rows examined and full-scan flags) Which statements run full table scans? What is the server waiting on (I/O, mutexes, locks)? performance_schema.events_waits_summary_global_by_event_name, sys.waits_global_by_latency Who holds and who waits for a lock? performance_schema.data_locks, data_lock_waits, sys.innodb_lock_waits (see MySQL locks) ! Why not the general log in production
4. A diagnosis workflow and a tool picker
Find: slow log or sys.statement_analysis, ranked by total time. Plan: EXPLAIN, then EXPLAIN ANALYZE if estimates look wrong. Explain the plan: optimizer trace, only when the plan surprises you. Fix and verify: change one thing, re-run EXPLAIN and the same measurement.
Tools compared
Overhead values are rough guidance, not measurements What it records Best for ✓ Recap Measure before you tune: slow log or the statement digest to find the costly shapes, EXPLAIN and EXPLAIN ANALYZE to see the plan and the truth, optimizer trace when the choice is a mystery, performance_schema for waits and locks. Keep the cheap tools on, use the heavy ones briefly, and never leave the general log running in production.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.