Databases 11 min read

Boost MySQL Performance: 10 Proven Optimization Techniques

This guide presents ten practical MySQL performance optimization methods—including index creation, query rewriting, limiting result sets, avoiding SELECT *, reducing joins, using proper data types, caching with Redis, and server configuration tweaks—each illustrated with concrete SQL code examples to help you speed up your database operations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Boost MySQL Performance: 10 Proven Optimization Techniques

1. Optimize SQL Statements

1.1 Create Indexes

Creating indexes on columns frequently used in query conditions can dramatically speed up data retrieval.

CREATE INDEX index_name ON table_name(column_name);

1.2 Reduce JOIN Operations

Excessive JOINs can degrade performance; consider subqueries, pagination, or caching to minimize them.

SELECT t1.id, t1.name, t2.address
FROM users t1
INNER JOIN addresses t2 ON t1.id = t2.user_id
WHERE t1.city = 'New York';

1.3 Optimize Query Conditions

Avoid functions or calculations in the WHERE clause; use constants or pre‑computed values instead.

SELECT * FROM users WHERE age > 18 AND age < 60;

1.4 Use LIMIT for Pagination

Limit the result set and select only required columns.

SELECT id, name, age FROM users LIMIT 10 OFFSET 10;

1.5 Avoid SELECT *

Specify needed columns to reduce data transfer.

SELECT id, name, age FROM users WHERE age > 30;

1.6 Reduce Data Type Conversions

Perform type casting outside of queries when possible.

SELECT * FROM users WHERE CAST(age AS SIGNED) > 30;

1.7 Avoid Temporary Tables

Prefer JOINs over temporary tables for better performance.

SELECT t1.id, t1.name, t2.address
FROM users t1
JOIN addresses t2 ON t1.id = t2.user_id
WHERE t1.city = 'New York';

1.8 Use Stored Procedures and Triggers

Encapsulate complex logic in stored procedures to reduce round‑trips.

DELIMITER $$
CREATE PROCEDURE get_users_by_age()
BEGIN
    SELECT * FROM users WHERE age > 30;
END$$
DELIMITER ;

1.9 Use UNION ALL Instead of UNION

UNION ALL avoids the overhead of duplicate elimination.

SELECT id, name, age FROM users WHERE age > 30
UNION ALL
SELECT id, name, age FROM customers WHERE age > 30;

2. Design an Appropriate Table Structure

2.1 Choose Proper Data Types

Select data types that match the stored values, e.g., using INT for integer fields instead of VARCHAR.

2.2 Set Appropriate Character Set and Collation

Use UTF‑8 rather than UTF‑16 to save space and improve performance.

2.3 Use Descriptive Table and Column Names

Avoid reserved words and keep names meaningful for easier maintenance.

2.4 Arrange Columns Strategically

Place frequently queried columns early and group related columns together.

2.5 Define Primary and Foreign Keys

Ensure each table has a primary key and use foreign keys to maintain referential integrity.

2.6 Use Partitioned Tables When Needed

For very large tables, partition by a suitable column to improve query speed.

2.7 Avoid Oversized Columns

Split large text columns into smaller pieces if possible to reduce storage overhead.

3. Effective Caching Strategies

3.1 MySQL Query Cache

Enable the built‑in query cache to store results of frequently run queries.

SET GLOBAL query_cache_size = 100M;
SET GLOBAL query_cache_type = '2';

3.2 External Cache (Redis)

Use Redis as an external cache to offload hot data from MySQL.

SET GLOBAL redis.host = '127.0.0.1';
SET GLOBAL redis.port = 6379;
SET GLOBAL redis.password = '';
SET GLOBAL redis.database = 0;
SET GLOBAL redis.timeout = 0;

Read from Redis: SELECT * FROM cache WHERE key = 'user:1:name'; Write to Redis:

UPDATE cache SET value = 'John Doe' WHERE key = 'user:1:name';

4. Database and Server Configuration

4.1 Memory Allocation

Allocate sufficient memory based on hardware and workload to avoid bottlenecks.

4.2 Buffer Sizes

Adjust buffer pool and other I/O buffers for optimal throughput.

4.3 Connection Limits

Set an appropriate maximum number of concurrent connections.

4.4 Thread Pool Settings

Configure thread pool size to handle concurrent queries efficiently.

4.5 Configuration File Tuning

Fine‑tune my.cnf (or equivalent) by disabling unnecessary services and setting appropriate log levels.

5. Ongoing Database Maintenance

5.1 Table Optimization

Periodically run ANALYZE TABLE or OPTIMIZE TABLE to improve statistics and storage.

5.2 Rebuild Indexes

Use REPAIR INDEX or ANALYZE INDEX to keep indexes efficient.

5.3 Fragment Cleanup

Run OPTIMIZE TABLE or REPAIR TABLE to eliminate fragmentation.

5.4 Data Reorganization

Regularly reorganize tables to maintain query performance.

5.5 Data Compression

Apply COMPRESS TABLE where appropriate to save disk space.

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 OptimizationSQLmysqlDatabase Tuning
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.