MySQL 8 Query Performance
Tuning
A Systematic Method for Improving Execution Speeds
â Jesper Wisborg Krogh
MySQL 8 Query Performance Tuning
A Systematic Method for Improving Execution Speeds
Jesper Wisborg Krogh
www.allitebooks.com
Speeds
ISBN-13 (pbk): 978-1-4842-5583-4 ISBN-13 (electronic): 978-1-4842-5584-1 https://doi.org/10.1007/978-1-4842-5584-1
Copyright © 2020 by Jesper Wisborg Krogh
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.
Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Jonathan Gennick
Development Editor: Laura Berendson Coordinating Editor: Jill Balzano
Cover image designed by Freepik (www.freepik.com)
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.
For information on translations, please e-mail [email protected], or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/9781484255834. For more detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper Jesper Wisborg Krogh Hornsby, NSW, Australia
To the MySQL Support team, it has been a pleasure to work with you all.
www.allitebooks.com
v
Part I: Getting Started ������������������������������������������������������������������������������������� 1 Chapter 1: MySQL Performance Tuning �������������������������������������������������������������������� 3
Consider the Whole Stack ������������������������������������������������������������������������������������������������������������� 3 Monitoring ������������������������������������������������������������������������������������������������������������������������������������� 5 The Lifecycle of a Query���������������������������������������������������������������������������������������������������������������� 6 Summary��������������������������������������������������������������������������������������������������������������������������������������� 8Chapter 2: Query Tuning Methodology���������������������������������������������������������������������� 9
Overview ��������������������������������������������������������������������������������������������������������������������������������������� 9 Verify the Problem ����������������������������������������������������������������������������������������������������������������������� 11 Determine the Cause ������������������������������������������������������������������������������������������������������������������ 11 Determine the Solution ��������������������������������������������������������������������������������������������������������������� 12 Implement the Solution ��������������������������������������������������������������������������������������������������������������� 13 Work Proactively ������������������������������������������������������������������������������������������������������������������������� 15 Summary������������������������������������������������������������������������������������������������������������������������������������� 16Chapter 3: Benchmarking with Sysbench �������������������������������������������������������������� 19
Best Practices ����������������������������������������������������������������������������������������������������������������������������� 19 Standard TPC Benchmarks ��������������������������������������������������������������������������������������������������������� 22 Common Benchmarks Tools �������������������������������������������������������������������������������������������������������� 23 Sysbench Installation ������������������������������������������������������������������������������������������������������������������ 24 Executing Benchmarks ��������������������������������������������������������������������������������������������������������������� 32Table of Contents
About the Author ��������������������������������������������������������������������������������������������������xxiii
About the Technical Reviewer �������������������������������������������������������������������������������xxv
Acknowledgments �����������������������������������������������������������������������������������������������xxvii
Introduction ����������������������������������������������������������������������������������������������������������xxix
vi
Creating Custom Benchmarks ���������������������������������������������������������������������������������������������������� 40 Overview of the Custom Script ���������������������������������������������������������������������������������������������� 41 Defining the Options �������������������������������������������������������������������������������������������������������������� 43 The run Command ����������������������������������������������������������������������������������������������������������������� 45 The prepare Command ���������������������������������������������������������������������������������������������������������� 48 The cleanup Command ���������������������������������������������������������������������������������������������������������� 50 Registering Commands ��������������������������������������������������������������������������������������������������������� 51 Summary������������������������������������������������������������������������������������������������������������������������������������� 53
Chapter 4: Test Data ����������������������������������������������������������������������������������������������� 55
Downloading the Example Databases ����������������������������������������������������������������������������������������� 55 The world Database �������������������������������������������������������������������������������������������������������������������� 56 Schema���������������������������������������������������������������������������������������������������������������������������������� 56 Installation ����������������������������������������������������������������������������������������������������������������������������� 57 The world_x Database ���������������������������������������������������������������������������������������������������������������� 58 Schema���������������������������������������������������������������������������������������������������������������������������������� 58 Installation ����������������������������������������������������������������������������������������������������������������������������� 59 The sakila Database �������������������������������������������������������������������������������������������������������������������� 60 Schema���������������������������������������������������������������������������������������������������������������������������������� 60 Installation ����������������������������������������������������������������������������������������������������������������������������� 66 The employees Database ������������������������������������������������������������������������������������������������������������ 67 Schema���������������������������������������������������������������������������������������������������������������������������������� 67 Installation ����������������������������������������������������������������������������������������������������������������������������� 71 Other Databases ������������������������������������������������������������������������������������������������������������������������� 72 Summary������������������������������������������������������������������������������������������������������������������������������������� 73Part II: Sources of Information ��������������������������������������������������������������������� 75
Chapter 5: The Performance Schema ��������������������������������������������������������������������� 77
Terminology �������������������������������������������������������������������������������������������������������������������������������� 77 Threads ��������������������������������������������������������������������������������������������������������������������������������������� 78 Instruments ��������������������������������������������������������������������������������������������������������������������������������� 83 Consumers ���������������������������������������������������������������������������������������������������������������������������������� 85 Table of ConTenTsEvents ����������������������������������������������������������������������������������������������������������������������������������������� 87 Event Types ���������������������������������������������������������������������������������������������������������������������������� 88 Event Scopes ������������������������������������������������������������������������������������������������������������������������� 89 Event Nesting ������������������������������������������������������������������������������������������������������������������������ 90 Event Properties �������������������������������������������������������������������������������������������������������������������� 92 Actors and Objects ���������������������������������������������������������������������������������������������������������������������� 94 Digests ���������������������������������������������������������������������������������������������������������������������������������������� 94 Table Types ���������������������������������������������������������������������������������������������������������������������������������� 96 Dynamic Configuration ���������������������������������������������������������������������������������������������������������������� 97 Summary������������������������������������������������������������������������������������������������������������������������������������� 99
Chapter 6: The sys Schema ���������������������������������������������������������������������������������� 101
sys Schema Configuration �������������������������������������������������������������������������������������������������������� 101 Formatting Functions ���������������������������������������������������������������������������������������������������������������� 105 The Views ���������������������������������������������������������������������������������������������������������������������������������� 107 Helper Functions and Procedures ��������������������������������������������������������������������������������������������� 109 Summary����������������������������������������������������������������������������������������������������������������������������������� 110Chapter 7: The Information Schema ��������������������������������������������������������������������� 111
What Is the Information Schema? ��������������������������������������������������������������������������������������������� 111 Privileges ���������������������������������������������������������������������������������������������������������������������������������� 112 Views ���������������������������������������������������������������������������������������������������������������������������������������� 113 System Information ������������������������������������������������������������������������������������������������������������� 113 Schema Information ������������������������������������������������������������������������������������������������������������ 115 Performance Information ���������������������������������������������������������������������������������������������������� 121 Privilege Information ����������������������������������������������������������������������������������������������������������� 126 Caching of Index Statistics Data ����������������������������������������������������������������������������������������������� 128 Summary����������������������������������������������������������������������������������������������������������������������������������� 131Chapter 8: SHOW Statements ������������������������������������������������������������������������������� 133
Relationship to the Information Schema ����������������������������������������������������������������������������������� 133 Relationship to the Performance Schema��������������������������������������������������������������������������������� 135 Engine Status ���������������������������������������������������������������������������������������������������������������������������� 137viii
Replication and Binary Logs ����������������������������������������������������������������������������������������������������� 139 Listing Binary Logs �������������������������������������������������������������������������������������������������������������� 139 Viewing Log Events ������������������������������������������������������������������������������������������������������������� 140 Show Connected Replicas ��������������������������������������������������������������������������������������������������� 145 Miscellaneous Statements �������������������������������������������������������������������������������������������������������� 146 Summary����������������������������������������������������������������������������������������������������������������������������������� 150
Chapter 9: The Slow Query Log����������������������������������������������������������������������������� 153
Configuration����������������������������������������������������������������������������������������������������������������������������� 154 Log Events��������������������������������������������������������������������������������������������������������������������������������� 158 Aggregation ������������������������������������������������������������������������������������������������������������������������������� 160 Summary����������������������������������������������������������������������������������������������������������������������������������� 164Part III: Tools ����������������������������������������������������������������������������������������������� 165
Chapter 10 MySQL Enterprise Monitor ����������������������������������������������������������������� 167
Overview ����������������������������������������������������������������������������������������������������������������������������������� 167 Installation �������������������������������������������������������������������������������������������������������������������������������� 170 Download ����������������������������������������������������������������������������������������������������������������������������� 170 Installation Process ������������������������������������������������������������������������������������������������������������� 175 Starting and Stopping the Service Manager ����������������������������������������������������������������������������� 182 Microsoft Windows �������������������������������������������������������������������������������������������������������������� 183 Linux ������������������������������������������������������������������������������������������������������������������������������������ 184 Adding MySQL Instances ���������������������������������������������������������������������������������������������������������� 186 The Graphical User Interface����������������������������������������������������������������������������������������������������� 189 General Navigation �������������������������������������������������������������������������������������������������������������� 189 Advisors ������������������������������������������������������������������������������������������������������������������������������� 191 Timeseries Graphs ���������������������������������������������������