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 DML Statements
8.2.3 Optimizing Database Privileges
8.2.4 Optimizing INFORMATION_SCHEMA Queries
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 How MySQL Uses Internal Temporary Tables
8.5 Optimizing for InnoDB Tables
8.5.1 Optimizing Storage Layout for InnoDB Tables
8.5.2 Optimizing InnoDB Transaction Management
8.5.3 Optimizing InnoDB Logging
8.5.4 Bulk Data Loading for InnoDB Tables
8.5.5 Optimizing InnoDB Queries
8.5.6 Optimizing InnoDB DDL Operations
8.5.7 Optimizing InnoDB Disk I/O
8.5.8 Optimizing InnoDB Configuration Variables
8.5.9 Optimizing InnoDB for Systems with Many Tables
8.6 Optimizing for MyISAM Tables
8.6.1 Optimizing MyISAM Queries
8.6.2 Bulk Data Loading for MyISAM Tables
8.6.3 Speed of REPAIR TABLE Statements
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.8.5 Controlling the Query Optimizer
8.9 Buffering and Caching
8.9.1 The InnoDB Buffer Pool
8.9.2 The MyISAM Key Cache
8.9.3 The MySQL Query Cache
8.10 Optimizing Locking Operations
8.10.1 Internal Locking Methods
8.10.2 Table Locking Issues
8.10.3 Concurrent Inserts
8.10.4 Metadata Locking
8.10.5 External Locking
8.11 Optimizing the MySQL Server
8.11.1 System Factors and Startup Parameter Tuning
8.11.2 Tuning Server Parameters
8.11.3 Optimizing Disk I/O
8.11.4 Optimizing Memory Use
8.11.5 Optimizing Network Use
8.11.6 The Thread Pool Plugin
8.12 Measuring Performance (Benchmarking)
8.12.1 Measuring the Speed of Expressions and Functions
8.12.2 The MySQL Benchmark Suite
8.12.3 Using Your Own Benchmarks
8.12.4 Measuring Performance with performance_schema
8.12.5 Examining Thread Information

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.