Professional Documents
Culture Documents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
iii
2. Indexes and Indexing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Red Herrings of Performance 37
Better, Faster Hardware! 37
MySQL Tuning 39
MySQL Indexes: A Visual Introduction 40
InnoDB Tables Are Indexes 41
Table Access Methods 45
Leftmost Prefix Requirement 49
EXPLAIN: Query Execution Plan 51
WHERE 54
GROUP BY 60
ORDER BY 65
Covering Indexes 71
Join Tables 71
Indexing: How to Think Like MySQL 80
Know the Query 80
Understand with EXPLAIN 81
Optimize the Query 82
Deploy and Verify 83
It Was a Good Index Until… 84
Queries Changed 84
Excessive, Duplicate, and Unused 85
Extreme Selectivity 86
It’s a Trap! (When MySQL Chooses Another Index) 87
Table Join Algorithms 87
Summary 89
Practice: Find Duplicate Indexes 90
3. Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Three Secrets 92
Indexes May Not Help 92
Less Data Is Better 96
Less QPS Is Better 96
Principle of Least Data 97
Data Access 97
Data Storage 104
Delete or Archive Data 115
Tools 115
Batch Size 115
Row Lock Contention 118
iv | Table of Contents
Space and Time 118
The Binary Log Paradox 119
Summary 120
Practice: Audit Query Data Access 121
5. Sharding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Why a Single Database Does Not Scale 152
Application Workload 152
Benchmarks Are Synthetic 155
Writes 156
Schema Changes 157
Operations 158
Pebbles, Not Boulders 159
Sharding: A Brief Introduction 160
Shard Key 161
Strategies 162
Challenges 167
Table of Contents | v
Alternatives 170
NewSQL 170
Middleware 171
Microservices 172
Don’t Use MySQL 172
Summary 173
Practice: Four-Year Fit 173
vi | Table of Contents
7. Replication Lag. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Foundation 234
Source to Replica 235
Binary Log Events 237
Replication Lag 238
Causes 240
Transaction Throughput 240
Post-Failure Rebuild 241
Network Issues 241
Risk: Data Loss 241
Asynchronous Replication 242
Semisynchronous Replication 244
Reducing Lag: Multithreaded Replication 246
Monitoring 250
Recovery Time 252
Summary 254
Practice: Monitor Subsecond Lag 255
8. Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Row Locking 260
Record and Next-Key Locks 262
Gap Locks 266
Secondary Indexes 269
Insert Intention Locks 273
MVCC and the Undo Logs 276
History List Length 280
Common Problems 282
Large Transactions (Transaction Size) 282
Long-Running Transactions 283
Stalled Transactions 284
Abandoned Transactions 285
Reporting 286
Active Transactions: Latest 286
Active Transactions: Summary 290
Active Transaction: History 291
Committed Transactions: Summary 292
Summary 294
Practice: Alert on History List Length 295
Practice: Examine Row Locks 296
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317