Databases 11 min read

Why Do MySQL Queries Slow Down? Understanding Write/Read Bottlenecks

This article explains why MySQL queries become slow by examining write‑operation issues such as dirty‑page flushing and locking, read‑operation problems like missing indexes and buffer‑pool evictions, and provides practical steps to diagnose and prevent these performance bottlenecks.

ITPUB
ITPUB
ITPUB
Why Do MySQL Queries Slow Down? Understanding Write/Read Bottlenecks

Background

Slow SQL execution is a frequent interview question and a critical performance concern for backend developers. In production environments, a sluggish query can indicate serious issues that need to be identified and mitigated.

Write Operations

During a write, MySQL (InnoDB) records changes in the redo log and updates the memory buffer. The redo log is written sequentially, which is fast, but when the circular redo log fills up under high concurrency, dirty pages accumulate.

When the redo log is full, InnoDB must flush dirty pages to disk, pausing further writes and causing query latency. Additionally, write‑side locks can block a transaction until the lock is released, or lead to deadlocks that require manual intervention.

To inspect current locks, run the statements shown in the diagram (e.g., querying information_schema.innodb_lock_waits and related tables). If a lock persists too long, you can release it with kill <thread_id>, where trx_mysql_thread_id identifies the thread.

Lock inspection query
Lock inspection query

Read Operations

Read‑side slowdowns are often caused by two main factors:

Missing indexes, which force a full table scan.

Dirty‑page flushing during reads when the buffer pool evicts a dirty page to make room for a needed page.

InnoDB keeps frequently accessed data and index pages in the buffer pool. When the pool reaches its limit, the least‑recently‑used pages are evicted; if the evicted page is dirty, it must be written to disk before reuse, adding latency.

Diagnosing with EXPLAIN

Use EXPLAIN <SQL> to view the execution plan. Important columns include:

type : access method (ALL, index, range, ref, eq_ref, const, system, NULL) – performance improves from left to right.

possible_keys : indexes that could be used.

key : index actually used.

rows : estimated rows examined.

Extra : additional actions such as Using index , Using where , Using temporary , Using filesort .

EXPLAIN output
EXPLAIN output

Slow Query Log

The slow query log records statements that exceed a configurable execution time. It is disabled by default and must be enabled manually.

Check the current status: SHOW VARIABLES LIKE 'slow_query_log'; Enable temporarily: SET GLOBAL slow_query_log = ON; Note: This setting is lost after a MySQL restart; to make it permanent, edit my.cnf.

Enable slow query log
Enable slow query log

Prevention and Optimization

To avoid or mitigate slow queries:

Ensure queries use appropriate indexes; regularly run EXPLAIN to verify.

Monitor and limit the dirty‑page ratio (keep it well below 75%).

Adjust innodb_io_capacity to match disk performance and control redo‑log flush speed.

Summary

Write‑side slowdowns arise from redo‑log saturation leading to dirty‑page flushing and from lock contention or deadlocks.

Read‑side slowdowns are typically due to missing indexes (full scans) or dirty‑page eviction during buffer‑pool replacement.

Use the slow query log and EXPLAIN to pinpoint problems, and tune InnoDB parameters to keep the system responsive.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performance tuningmysqlexplainslow-querydirty page
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.