Chapter 8 Optimization

Table of Contents

8.1 Optimization Overview
8.2 Optimizing SQL Statements
8.2.1 Optimizing SELECT Statements
8.2.2 Optimizing Data Change Statements
8.2.3 Optimizing Database Privileges
8.2.4 INFORMATION_SCHEMA Optimization
8.2.5 Other Optimization Tips
8.3 Optimization and Indexes
8.3.1 How MySQL Uses Indexes
8.3.2 Using Primary Keys
8.3.3 Using Foreign Keys
8.3.4 Column Indexes
8.3.5 Multiple-Column Indexes
8.3.6 Verifying Index Usage
8.3.7 InnoDB and MyISAM Index Statistics Collection
8.3.8 Comparison of B-Tree and Hash Indexes
8.4 Optimizing Database Structure
8.4.1 Optimizing Data Size
8.4.2 Optimizing MySQL Data Types
8.4.3 Optimizing for Many Tables
8.4.4 Internal Temporary Table Use in MySQL
8.5 Optimizing for MyISAM Tables
8.5.1 Optimizing MyISAM Queries
8.5.2 Bulk Data Loading for MyISAM Tables
8.5.3 Speed of REPAIR TABLE Statements
8.6 Optimizing for InnoDB Tables
8.6.1 Optimizing Storage Layout for InnoDB Tables
8.6.2 Optimizing InnoDB Transaction Management
8.6.3 Optimizing InnoDB Redo Logging
8.6.4 Bulk Data Loading for InnoDB Tables
8.6.5 Optimizing InnoDB Queries
8.6.6 Optimizing InnoDB DDL Operations
8.6.7 Optimizing InnoDB Disk I/O
8.6.8 Optimizing InnoDB for Systems with Many Tables
8.7 Optimizing for MEMORY Tables
8.8 Understanding the Query Execution Plan
8.8.1 Optimizing Queries with EXPLAIN
8.8.2 EXPLAIN Output Format
8.8.3 EXPLAIN EXTENDED Output Format
8.8.4 Estimating Query Performance
8.9 Controlling the Query Optimizer
8.9.1 Controlling Query Plan Evaluation
8.9.2 Controlling Switchable Optimizations
8.9.3 Index Hints
8.10 Buffering and Caching
8.10.1 The MyISAM Key Cache
8.10.2 The InnoDB Buffer Pool
8.10.3 The MySQL Query Cache
8.11 Optimizing Locking Operations
8.11.1 Internal Locking Methods
8.11.2 Table Locking Issues
8.11.3 Concurrent Inserts
8.11.4 External Locking
8.12 Optimizing the MySQL Server
8.12.1 System Factors and Startup Parameter Tuning
8.12.2 Tuning Server Parameters
8.12.3 Optimizing Disk I/O
8.12.4 Using Symbolic Links
8.12.5 Optimizing Memory Use
8.12.6 Optimizing Network Use
8.13 Measuring Performance (Benchmarking)
8.13.1 Measuring the Speed of Expressions and Functions
8.13.2 The MySQL Benchmark Suite
8.13.3 Using Your Own Benchmarks
8.14 Examining Thread Information
8.14.1 Thread Command Values
8.14.2 General Thread States
8.14.3 Delayed-Insert Thread States
8.14.4 Query Cache Thread States
8.14.5 Replication Master Thread States
8.14.6 Replication Slave I/O Thread States
8.14.7 Replication Slave SQL Thread States
8.14.8 Replication Slave Connection Thread States
8.14.9 MySQL Cluster Thread States
8.14.10 Event Scheduler Thread States

This chapter explains how to optimize MySQL performance and provides examples. Optimization involves configuring, tuning, and measuring performance, at several levels. Depending on your job role (developer, DBA, or a combination of both), you might optimize at the level of individual SQL statements, entire applications, a single database server, or multiple networked database servers. Sometimes you can be proactive and plan in advance for performance, while other times you might troubleshoot a configuration or code issue after a problem occurs. Optimizing CPU and memory usage can also improve scalability, allowing the database to handle more load without slowing down.