| name | mysql-optimizer |
| description | MySQL optimization expertise covering query optimization with EXPLAIN analysis, indexing strategies, InnoDB tuning, slow query log analysis, connection pooling, replication architectures (master-slave, group replication), partitioning, character set handling, and backup strategies using mysqldump and Percona XtraBackup.
Use when the user asks about mysql optimizer, mysql optimizer best practices, or needs guidance on mysql optimizer implementation.
Do NOT use when the user needs a different specialized skill or is asking about an unrelated technology domain.
|
| license | Apache-2.0 |
| metadata | {"author":"foundry-skills","version":"1.0.0","tags":"database sql backend","category":"backend-systems","subcategory":"database","depends":"","disclaimer":"none","difficulty":"intermediate"} |
MySQL Optimizer
Core Philosophy
MySQL optimization is a systematic discipline. Every performance issue has a root cause discoverable through methodical analysis. The optimization workflow is: measure, analyze with EXPLAIN, identify the bottleneck, fix it, and verify the improvement. Never optimize without evidence.
Query Optimization with EXPLAIN
Reading EXPLAIN Output
EXPLAIN FORMAT=TREE
SELECT o.id, o.total, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.status = 'pending'
AND o.created_at > '2025-01-01'
ORDER BY o.created_at DESC
LIMIT 20;
EXPLAIN Key Columns
| Column | What to Look For |
|---|
type | ALL (full scan) is bad. Best to worst: system > const > eq_ref > ref > range > index > ALL |
key | Which index is used. NULL means no index |
rows | Estimated rows examined. High numbers indicate missing indexes |
filtered | Percentage of rows remaining after table condition. Low values mean scanning lots of unused rows |
Extra | Using filesort (expensive sort), Using temporary (temp table), Using index (covering index, good) |
EXPLAIN ANALYZE (MySQL 8.0.18+)
EXPLAIN ANALYZE
SELECT c.name, COUNT(*) as order_count, SUM(o.total) as total_spent
FROM customers c
JOIN orders o ON c.id = o.customer_id
WHERE o.created_at >= '2025-01-01'
GROUP BY c.id
HAVING total_spent > 1000
ORDER BY total_spent DESC
LIMIT 10;
Common EXPLAIN Anti-Patterns and Fixes
EXPLAIN SELECT * FROM orders WHERE YEAR(created_at) = 2025;
SELECT * FROM orders WHERE created_at >= '2025-01-01' AND created_at < '2026-01-01';
EXPLAIN SELECT * FROM orders WHERE customer_id = 123 ORDER BY created_at DESC;
CREATE INDEX idx_customer_date ON orders (customer_id, created_at DESC);
EXPLAIN SELECT status, COUNT(*) FROM orders GROUP BY status;
CREATE INDEX idx_status ON orders (status);
EXPLAIN SELECT * FROM users WHERE phone = 5551234567;
users phone ;
Indexing Strategy
Index Types
CREATE INDEX idx_email ON users (email);
CREATE INDEX idx_composite ON orders (customer_id, status, created_at);
CREATE INDEX idx_covering ON orders (customer_id, status, created_at, total);
SELECT status, created_at, total FROM orders WHERE customer_id = 123;
CREATE INDEX idx_url ON pages (url(100));
CREATE FULLTEXT INDEX idx_content ON articles (title, body);
SELECT * FROM articles WHERE MATCH(title, body) AGAINST('mysql optimization' IN BOOLEAN MODE);
CREATE SPATIAL INDEX idx_location ON stores (location);
CREATE INDEX idx_recent ON events (created_at DESC);
ALTER TABLE orders ALTER INDEX idx_status INVISIBLE;
orders INDEX idx_status VISIBLE;
Index Selection Guidelines
1. Start with queries from the slow query log
2. Identify WHERE, JOIN, ORDER BY, and GROUP BY columns
3. Build composite indexes following the order:
- Equality conditions first (WHERE status = 'active')
- Range conditions next (WHERE created_at > '2025-01-01')
- ORDER BY / GROUP BY columns last
4. Include SELECT columns for covering index when feasible
5. Avoid over-indexing: each index slows writes
6. Use FORCE INDEX only as last resort
Index Maintenance
SELECT * FROM sys.schema_unused_indexes WHERE object_schema = 'mydb';
SELECT * FROM sys.schema_redundant_indexes WHERE table_schema = 'mydb';
SHOW INDEX FROM orders;
ANALYZE TABLE orders;
SELECT table_name, index_name,
ROUND(stat_value * @@innodb_page_size / 1024 / 1024, 2) AS size_mb
FROM mysql.innodb_index_stats
WHERE database_name = 'mydb' AND stat_name = 'size';
InnoDB Tuning
Critical Parameters
[mysqld]
innodb_buffer_pool_size = 24G
innodb_buffer_pool_instances = 8
innodb_redo_log_capacity = 4G
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_method = O_DIRECT
innodb_flush_neighbors = 0
innodb_thread_concurrency = 0
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_doublewrite = ON
innodb_change_buffer_max_size = 25
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
Buffer Pool Monitoring
SELECT
ROUND(@@innodb_buffer_pool_size / 1024 / 1024 / 1024, 2) AS pool_size_gb,
ROUND(data_length / 1024 / 1024 / 1024, 2) AS data_size_gb
FROM (
SELECT SUM(data_length + index_length) AS data_length
FROM information_schema.tables
WHERE table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
) t;
SHOW STATUS LIKE 'Innodb_buffer_pool_read%';
Slow Query Log Analysis
Configuration
[mysqld]
slow_query_log = ON
slow_query_log_file = [system-path]
long_query_time = 0.5
log_queries_not_using_indexes = ON
min_examined_row_limit = 1000
log_slow_admin_statements = ON
log_slow_replica_statements = ON
Analysis with pt-query-digest
# Percona Toolkit: aggregate and rank slow queries
pt-query-digest [system-path] --limit=20
# Filter by database
pt-query-digest [system-path] --filter '$event->{db} eq "mydb"'
# Filter by time range
pt-query-digest [system-path] --since '2025-03-01' --until '2025-03-02'
Performance Schema Queries
SELECT
DIGEST_TEXT AS query_pattern,
COUNT_STAR AS executions,
ROUND(SUM_TIMER_WAIT / 1e12, 2) AS total_seconds,
ROUND(AVG_TIMER_WAIT / 1e12, 4) AS avg_seconds,
SUM_ROWS_EXAMINED,
SUM_ROWS_SENT,
FIRST_SEEN, LAST_SEEN
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 20;
Connection Pooling
ProxySQL Configuration
INSERT INTO mysql_servers (hostgroup_id, hostname, port, weight) VALUES
(10, 'mysql-primary', 3306, 100),
(20, 'mysql-replica-1', 3306, 50),
(20, 'mysql-replica-2', 3306, 50);
INSERT INTO mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup) VALUES
(1, 1, '^SELECT.*FOR UPDATE', 10),
(2, 1, '^SELECT', 20),
(3, 1, '.*', 10);
LOAD MYSQL SERVERS TO RUNTIME;
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
SAVE MYSQL QUERY RULES TO DISK;
Replication
Async Replication (Master-Replica)
CREATE USER 'repl'@'%' IDENTIFIED BY 'YOUR_SECURE_PASSWORD_HERE';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='primary-host',
SOURCE_USER='repl',
SOURCE_PASSWORD='YOUR_SECURE_PASSWORD_HERE',
SOURCE_AUTO_POSITION=1;
START REPLICA;
SHOW REPLICA STATUS\G
Group Replication (Multi-Primary)
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
START GROUP_REPLICATION;
Partitioning
CREATE TABLE events (
id BIGINT AUTO_INCREMENT,
event_type VARCHAR(50),
payload JSON,
created_at DATETIME NOT NULL,
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (TO_DAYS(created_at)) (
PARTITION p2025_01 VALUES LESS THAN (TO_DAYS('2025-02-01')),
PARTITION p2025_02 VALUES LESS THAN (TO_DAYS('2025-03-01')),
PARTITION p_future VALUES LESS THAN MAXVALUE
);
ALTER TABLE events ADD PARTITION (
PARTITION p2025_04 VALUES LESS THAN (TO_DAYS('2025-05-01'))
);
ALTER TABLE events DROP PARTITION p2024_01;
Character Set Handling
CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Backup Strategies
mysqldump (Logical Backup)
mysqldump --single-transaction --routines --triggers --events \
--set-gtid-purged=ON --source-data=2 \
--databases mydb > backup_$(date +%Y%m%d).sql
Percona XtraBackup (Physical Backup)
# Full backup (online, non-blocking for InnoDB)
xtrabackup --backup --target-dir=/backups/full/$(date +%Y%m%d) \
--user=root --password=pass
# Incremental backup
xtrabackup --backup --target-dir=/backups/inc/$(date +%Y%m%d) \
--incremental-basedir=/backups/full/20250301
# Prepare and restore
xtrabackup --prepare --target-dir=/backups/full/20250301
xtrabackup --copy-back --target-dir=/backups/full/20250301
| Method | Speed | Size | Locking | Granularity |
|---|
| mysqldump | Slow | Small | No (InnoDB) | Database/Table |
| mydumper | Medium | Small | Minimal | Database/Table |
| XtraBackup | Fast | Large | No | Full/Incremental |
When to Use
Use this skill when:
- Designing or implementing mysql optimizer solutions
- Reviewing or improving existing mysql optimizer approaches
- Making architectural or implementation decisions about mysql optimizer
- Learning mysql optimizer patterns and best practices
- Troubleshooting mysql optimizer-related issues
Do NOT use this skill when:
- The question is about a fundamentally different technology domain
- A more specific sibling skill covers the exact topic needed
- The user needs a complete hands-on tutorial rather than expert guidance
Output Format
# Mysql Optimizer Analysis
## Context Assessment
[Situation summary and constraints]
## Recommended Approach
[Primary recommendation with rationale]
## Implementation Steps
1. [Step with specific details]
2. [Step with specific details]
3. [Step with specific details]
## Trade-offs and Considerations
- [Key trade-off 1]
- [Key trade-off 2]
## Next Steps
- [Immediate action item]
- [Follow-up action item]
Example
Input: "Help me implement mysql optimizer for a medium-scale production application"
Output: A structured analysis covering current state assessment, recommended mysql optimizer approach with specific patterns, implementation roadmap with milestones, and risk mitigation strategies tailored to the application scale and constraints.
Edge Cases
- Legacy system integration: When mysql optimizer must coexist with legacy approaches, provide a gradual migration path rather than a complete rewrite
- Scale mismatch: When the solution complexity exceeds the project scale, recommend a simpler approach and note when to revisit
- Team skill gaps: When the team lacks experience with the recommended approach, include learning resources and simpler alternatives
- Conflicting requirements: When constraints conflict (e.g., performance vs. maintainability), explicitly state the trade-off and recommend based on stated priorities