Jesper Wisborg Krogh
Hornsby, NSW, Australia
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484255834 . For more detailed information, please visit http://www.apress.com/source-code .
ISBN 978-1-4842-5583-4 e-ISBN 978-1-4842-5584-1
https://doi.org/10.1007/978-1-4842-5584-1
Jesper Wisborg Krogh 2020
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
Introduction
MySQL performance tuning is a very large subject that takes years to master. The length of this book bears testimony to that, even with the scope reduced to focus on query-related topics. There are no simple recipes for improving performance, and often a solution requires understanding the relationship between various parts not only within MySQL but also for other parts of the stack. If you feel that it is overwhelming to get started, you are far from the first one, but do not despair as it is with performance tuning like with other skills that practice makes perfect.
The aim of this book is to help you to get a good start on the journey to become skilled at improving the performance of the queries executing on your MySQL instances. As said, there are no simple recipes, so the best way forward is to learn how the various components involved in performance tuning work. That is what the bulk of this book tries to do as well as giving examples of what to look for and how to perform common tasks. On the other hand, the scope has been limited to MySQL itself, so there is very limited discussion about the operating system, file system, and hardware levels.
MySQL is famous for its support for storage engines. However, this book exclusively covers the InnoDB storage engine except for the discussion of internal temporary tables. With respect to MySQL versions, only MySQL 8 is considered. That said, most of the discussion also applies to older versions of MySQL, and in general it is mentioned when a feature is new in MySQL 8 or that MySQL 8 has a different behavior compared to older versions.
Book Audience
The book has been written for developers and database administrators who have experience working with MySQL and want to expand their knowledge into the realm of query performance tuning. No prior experience with performance tuning is required.
Examples and the Books GitHub Repository
I have tried to add as many examples and outputs from examples as possible. Some of the examples are quite short, some are quite long. In either case, I hope you are able to follow them and reproduce the effect or result demonstrated. At the same time, please do bear in mind that by nature there is often randomness involved (sometimes even explicitly as with the index statistics), and the exact outcome of the examples may depend on how the tables and data have been used prior to the example. In other words, you may get different results even if you did everything right. This particularly applies to numbers that relate to index statistics, timings, and the like.
Examples that are long or produce outputs that are either long or wide have been added to this books GitHub repository. This includes some of the figures that may be hard to read with the image size that the page format allows. The link to the repository can be found from the books homepage at www.apress.com/gp/book/9781484255834 .
The GitHub repository will also be the home of the errata for the book once that is created. I will use the errata not only to communicate errors in the book but also to provide updates when bug fixes and new features in MySQL 8 cause changes to book content. If necessary, I will also update the examples in the repository to reflect the behavior in the newer releases. For these reasons, I recommend that you keep an eye on the repository.
Book Structure
The book is divided into six parts with a total of 27 chapters. I have attempted to keep each chapter relatively self-contained with the aim that you can use the book as a reference book. The drawback of this choice is that there is some duplication of information from time to time. An example is Chapter, so some of the information is repeated. This was a deliberate choice, and I hope it helps you reduce the amount of page flipping to find the information you need.
The six parts progressively move you through the topics starting with some basic background and finishing with more solution-oriented tasks. The first part starts out discussing the methodology, benchmarks, and test data. The second part focuses on the sources of information such as the Performance Schema. The third part covers the tools such as MySQL Shell used in this book. The fourth part provides the theoretical background used in the last two parts. The fifth part focuses on analyzing queries, transactions, and locks. Finally, the sixth part discusses how to improve performance through the configuration, query optimization, replication, and caching. There are cases where some content is a little out of place, like all replication information is contained in a single chapter.
Part I: Getting Started
Part I introduces you to the concepts of MySQL query performance tuning. This includes some high-level considerations, of which some are not unique to MySQL (but are of course discussed in the context of MySQL). The four chapters are