QR CookingNotes

CookingNotes

Your Personal Recipe Book

Get it on Google Play
QR FiNoteMe

FiNoteMe

Smart Finance Tracker

Get it on Google Play
database MySQL high CPU usage is one of the most common performance problems developers face, especially beginners. When the database suddenly spikes to 90–100% CPU, queries slow down, websites freeze, and services stop responding.

Fixing “MySQL High CPU Usage” — A Beginner-Friendly Guide to Understanding and Solving the Problem

5 Min Read Verified Content

Welcome back to another “real problem + easy solution” tutorial!
Today’s issue is one that scares many beginners:

“My MySQL server is at 100% CPU and everything is slow. What should I do?”

Relax. You're not alone — and you're not stupid.
Most developers experience this at some point.

Let’s break it down without technical jargon.




1. What Does High CPU Usage Mean in MySQL?


When you see MySQL eating 80–100% CPU, it means:

  • MySQL is processing too many heavy queries

  • There are missing indexes

  • The server is under-powered

  • There are unoptimized SELECT queries

  • Too many connections are hitting MySQL at once

  • A specific query may be “stuck”

MySQL is not the enemy — something is overworking it.




2. The Real Causes of MySQL High CPU Usage


Here are the most common reasons MySQL consumes too much CPU:

🔥 Reason #1: Full Table Scans

Queries without indexes force MySQL to read every row in the table.

Example slow query:

SELECT * FROM users WHERE email = 'test@example.com';

If email is not indexed → MySQL scans the whole table.

🔥 Reason #2: Too Many JOINs or Large JOINs

Complicated JOINs on large tables = CPU meltdown.

🔥 Reason #3: ORDER BY + LIMIT Without Index

Example:

SELECT * FROM orders ORDER BY created_at DESC LIMIT 20;

If created_at has no index → expensive sorting = high CPU.

🔥 Reason #4: Too Many Concurrent Queries

If your app suddenly gets more visitors, MySQL gets hammered.

🔥 Reason #5: Long-running queries

Bad SELECT queries can stay alive for minutes or hours.

🔥 Reason #6: Misconfigured MySQL server

Default settings are not optimized for real production.




3. How to Fix MySQL High CPU Usage (Step-by-Step)


Now the fun part — solutions for beginners.



Step 1: Find the Queries Causing High CPU

Run this:

SHOW FULL PROCESSLIST;

Or for modern MySQL:

SELECT * FROM performance_schema.events_statements_current;

Look for queries that are:

  • Using many seconds

  • In “Sending data” state

  • “Copying to tmp table”

  • “Sorting result”

Those are CPU killers.



Step 2: Use EXPLAIN to Diagnose the Slow Query

Run:

EXPLAIN SELECT ...;

If you see:

  • type = ALL → full table scan

  • rows = huge number → bad performance

  • Using temporary → expensive

  • Using filesort → very expensive

You likely need an index.



Step 3: Add Missing Indexes (The #1 Solution)

Example slow query:

SELECT * FROM users WHERE email='test@domain.com';

Fix:

CREATE INDEX idx_users_email ON users(email);

Other common indexes:

CREATE INDEX idx_orders_status ON orders(status); CREATE INDEX idx_users_created ON users(created_at); CREATE INDEX idx_posts_userid ON posts(user_id);

Indexing alone often reduces CPU usage by 40%–95%.



Step 4: Kill Stuck or Heavy Queries

If a query is running for too long, kill it:

KILL QUERY <process_id>;

Do not kill random queries — only the expensive ones.



Step 5: Limit Max Connections

Too many connections → CPU death.

Edit my.cnf:

[mysqld] max_connections = 150

Restart MySQL after changes.


Step 6: Enable Query Cache (MySQL < 8 only)

If you're using MySQL 5.x:

query_cache_size = 64M query_cache_type = 1

For MySQL 8+:
Use Redis caching or application-level caching.



Step 7: Optimize MySQL Configuration

Add these recommended beginner-friendly settings:

innodb_buffer_pool_size = 70% of RAM innodb_log_file_size = 512M innodb_flush_log_at_trx_commit = 2 tmp_table_size = 256M max_allowed_packet = 256M


Step 8: Upgrade the Server (If Needed)

If your DB is huge and traffic is high, sometimes the server truly needs:

  • More RAM

  • More CPU cores

  • Faster disks (SSD/NVMe is a must)

  • Separate DB server




4. Prevention: Keep CPU Low in the Future

Here’s your long-term survival guide:

✔ Add indexes BEFORE launching new features
✔ Avoid SELECT *
✔ Cache repeated queries
✔ Optimize JOINs
✔ Do regular slow query log audits
✔ Don’t store huge blobs in MySQL
✔ Archive old data

This is how “big companies” keep MySQL stable.




5. Example: Real Before & After Improvement


Before optimization:

  • 92% CPU usage

  • Frequent timeouts

  • Slow website

  • 3 slow queries without indexes

Steps done:

  • Added 2 indexes

  • Cached home-page queries

  • Limited max connections

  • Killed long-running queries

After optimization:

  • 12% CPU usage

  • Page loads in < 200ms

  • Database stable even during traffic

This is 100% real and common.




Conclusion


MySQL high CPU usage is one of the most common beginner problems, but now you understand exactly:

  • why it happens

  • how to find the cause

  • how to fix it

  • how to prevent it

With the steps above, even a complete newbie can stabilize MySQL and reduce CPU usage dramatically.

Advertisement
Back to Database