NEED FOR SPEED: BEST PRACTICES FOR MYSQL PERFORMANCE TUNING JANIS GRIFFIN PERFORMANCE EVANGELIST / SENIOR DBA

Similar documents
Tuna Helper Proven Process for SQL Tuning. Dean Richards Senior DBA, Confio Software

Tired of MySQL Making You Wait? Alexander Rubin, Principal Consultant, Percona Janis Griffin, Database Evangelist, SolarWinds

BEST PRACTICES FOR EFFECTIVE MYSQL PERFORMANCE TUNING. By: Janis Griffin. Database Performance Evangelist/Senior DBA

QUERY OPTIMIZATION. CS 564- Spring ACKs: Jeff Naughton, Jignesh Patel, AnHai Doan

Speaker: Dean Richards Senior DBA, Confio Software

1. Introduction. 2. History. Table of Contents

Sakila Sample Database

Sakila Sample Database

Database performance becomes an important issue in the presence of

CONFIGURING SQL SERVER FOR PERFORMANCE LIKE A MICROSOFT CERTIFIED MASTER

Database Performance Analyzer

CS317 File and Database Systems

Looney Tuner? No, there IS a method to my madness!

SQL CSCI 201 Principles of Software Development

MySQL for Database Administrators Ed 3.1

BEYOND THE RDBMS: WORKING WITH RELATIONAL DATA IN MARKLOGIC

SQL Gone Wild: Taming Bad SQL the Easy Way (or the Hard Way) Sergey Koltakov Product Manager, Database Manageability

Effective Testing for Live Applications. March, 29, 2018 Sveta Smirnova

MySQL Database Administrator Training NIIT, Gurgaon India 31 August-10 September 2015

GREAT BANDWIDTH NOT DELIVERING GREAT PERFORMANCE?

Copyright 2018, Oracle and/or its affiliates. All rights reserved.

Optimizing Queries with EXPLAIN

PATCH MANAGER AUTOMATED PATCHING OF MICROSOFT SERVERS AND 3RD-PARTY APPS

OKC MySQL Users Group

Foglight. Resolving the Database Performance. Finding clues in your DB2 LUW workloads

MySQL for Database Administrators Ed 4

Jens Bollmann. Welcome! Performance 101 for Small Web Apps. Principal consultant and trainer within the Professional Services group at SkySQL Ab.

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Oracle Optimizer: What s New in Oracle Database 12c? Maria Colgan Master Product Manager

MySQL Query Tuning 101. Sveta Smirnova, Alexander Rubin April, 16, 2015

SQL Tuning Reading Recent Data Fast

Oracle 12.2 My Favorite Top Five New or Improved Features. Janis Griffin Senior DBA / Performance Evangelist

Relational Database Development

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Database Performance Analyzer

SolarWinds Orion Integrated Virtual Infrastructure Monitor Supplement

Vendor: Oracle. Exam Code: 1Z Exam Name: MySQL 5.0, 5.1 and 5.5 Certified Associate Exam. Version: Demo

Measuring VMware Environments

Two Success Stories - Optimised Real-Time Reporting with BI Apps

SolarWinds Engineer s Toolset Fast Fixes to Network Issues

3/3/2008. Announcements. A Table with a View (continued) Fields (Attributes) and Primary Keys. Video. Keys Primary & Foreign Primary/Foreign Key

ORACLE CERTIFIED ASSOCIATE ORACLE DATABASE 11g ADMINISTRATOR

IT Best Practices Audit TCS offers a wide range of IT Best Practices Audit content covering 15 subjects and over 2200 topics, including:

ITS. MySQL for Database Administrators (40 Hours) (Exam code 1z0-883) (OCP My SQL DBA)

Database Performance Analyzer

Insider Threat Detection Including review of 2017 SolarWinds Federal Cybersecurity Survey

NetFlow Traffic Analyzer

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Session id: The Self-Managing Database: Guided Application and SQL Tuning

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

DATABASE DEVELOPMENT (H4)

Banner SQL 201. Georgia Summit Thursday, September 19th 4:10pm - 5:00pm Estes B

How To Remove Information_schema From Sql File

CO MySQL for Database Administrators

Parameter Sniffing Problem with Stored Procedures. Milos Radivojevic

SUPPORT SERVICES AND RESOURCE GUIDE

<Insert Picture Here> DBA s New Best Friend: Advanced SQL Tuning Features of Oracle Database 11g

MySQL Indexing. Best Practices for MySQL 5.6. Peter Zaitsev CEO, Percona MySQL Connect Sep 22, 2013 San Francisco,CA

Persistence Performance Tips

Oracle Database 11g: SQL Tuning Workshop

Optimize Your Databases Using Foglight for Oracle s Performance Investigator

Mobile Admin GETTING STARTED GUIDE. Version 8.2. Last Updated: Thursday, May 25, 2017

Grouping Data using GROUP BY in MySQL

<Insert Picture Here> New MySQL Enterprise Backup 4.1: Better Very Large Database Backup & Recovery and More!

SolarWinds Orion Platform Scalability

MySQL for Developers Ed 3

SQL functions fit into two broad categories: Data definition language Data manipulation language

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

WHITE PAPER. The Top 5 Threats in File Server Management

Virtual Communications Express Admin Guide: Configure Call Center

Molecular Devices High Content Screening Computer Specifications

Writing High Performance SQL Statements. Tim Sharp July 14, 2014

[Document Title] Network IP Multicast Monitoring & Troubleshooting. [Document Subtitle] Angi Gahler. Share: Author: Brad Hale

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

The SELECT-FROM-WHERE Structure

Oracle Sql Describe Schemas Query To Find Database

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

Optimizing Database Administration with IBM DB2 Autonomics for z/os IBM Redbooks Solution Guide

<Insert Picture Here> Inside the Oracle Database 11g Optimizer Removing the black magic

Database Performance Analyzer

GETTING STARTED GUIDE. Mobile Admin. Version 8.2

NCM Connector for Cisco SmartAdvisor

Independent consultant. Oracle ACE Director. Member of OakTable Network. Available for consulting In-house workshops. Performance Troubleshooting

Introduction to Computer Science and Business

EE221 Databases Practicals Manual

MySQL Group Replication in a nutshell

Resource Mapping A Wait Time Based Methodology for Database Performance Analysis

Course Outline and Objectives: Database Programming with SQL

MySQL Performance Tuning

Oracle Database 11g: Real Application Testing & Manageability Overview

Independent consultant. Oracle ACE Director. Member of OakTable Network. Available for consulting In-house workshops. Performance Troubleshooting

Database Management Systems. Chapter 5

Dashboards in SalesNexus

Advanced SQL Programming and Optimization. Everything you wanted to know about Stored Procedures!

The SELECT-FROM-WHERE Structure

Tips from the Trenches Preventing downtime for the over extended DBA. Andrew Moore Senior Remote DBA Percona Managed Services

Common MySQL Scalability Mistakes AUTHOR

Practical MySQL indexing guidelines

Data Warehouse Tuning. Without SQL Modification

Database Management Systems. Chapter 5

Transcription:

NEED FOR SPEED: BEST PRACTICES FOR MYSQL PERFORMANCE TUNING JANIS GRIFFIN PERFORMANCE EVANGELIST / SENIOR DBA 1

WHO AM I?» Senior DBA / Performance Evangelist for Solarwinds Janis.Griffin@solarwinds.com Twitter - @DoBoutAnything Current 25+ Years in Oracle and now MySQL DBA and Developer» Specialize in Performance Tuning» Review Database Performance for Customers and Prospects» Common Question How do I tune it? 2

AGENDA» Challenges Of Tuning Who should tune Which SQLs to tune» Utilize Response Time Analysis (RTA)» Gather Details about SQL Examine Query Execution Plans» Use SQL or Query Diagramming Who registered yesterday for Tuning Class Look up Overdue DVD Rentals by Customer Weekly Sales Rep Report by Category by Region» Monitor Make sure it stays tuned 3

CHALLENGES OF TUNING» SQL Tuning is Hard» Requires Expertise in Many Areas Technical Plan, Data Access, SQL Design Business What is the Purpose of SQL?» Tuning Takes Time Large Number of SQL Statements Each Statement is Different» Low Priority in Some Companies Vendor Applications Focus on Hardware or System Issues» Never Ending 4

WHO SHOULD TUNE» Developers? Developing applications is very difficult Typically focused on functionality Not much time left to tune SQL Don t get enough practice or simply don t know SQL runs differently in Production versus Dev/Test» DBA? Do not know the code like developers do Focus on Keep the Lights On Very complex environment» Need a team approach 5

WHICH SQL TO TUNE» User / Batch Job Complaints Known Poorly Performing SQL» Queries Performing Most I/O (LIO, PIO) Table or Index Scans» Queries Consuming CPU» Highest Response Times (DPA) Performance Schema Information Schema 7

RESPONSE TIME VERSUS HEALTH METRICS 7

MONSTERS LURKING IN YOUR DATABASE 8

RESPONSE TIME VERSUS HEALTH METRICS HOW DO YOU TO FIND THE FASTEST WAY TO WORK? 9

RESPONSE TIME ANALYSIS (RTA) Focus on Response Time» Understand the total time a Query spends in Database» Measure time while Query executes» MySQL helps by providing Instrumented Waits or Thread States 10

MYSQL 5.6+» Greatly improved Performance_Schema More information» Current/Historical Events at all levels Statements Stages Waits» Performance_Schema consumers now ON by Default» Storage Engine now defaults to INNODB» More improvements to come in 5.7 11

THREAD STATE EXAMPLE» \ 12

STATEMENTS, STAGES & WAITS SELECT visibility FROM linkdb. linktable WHERE id1 =? AND id2 =? AND link_type =? FOR UPDATE 13

MYSQL RESPONSE TIME DATA 14

RTA TUNING DATA 15

RTA BLOCKING ISSUE 16

RESPONSE TIME SCENARIO» Which scenario is worse?» SQL Statement 1 Executed 1000 times Caused 10 minutes of wait time for end user Waited 90% of time on sending data» SQL Statement 2 Executed 1 time Caused 10 minutes of wait time for end user Waited 90% on system lock 17

GATHER METRICS» Get baseline metrics How long does it take now What is acceptable (10 sec, 2 min, 1 hour)» Collect Wait or Thread States Locking / Blocking (system lock) I/O problem (sending data) Calculating statistics (statistics) Network slowdown (writing to net) May be multiple issues All have different resolutions 18

GET EXECUTION PLAN» Explain» Explain Extended» Optimizer Trace 5.6.3+» MySQL Workbench 19

EXPLAIN CHEAT SHEET» http://d1kgm347lql2s5.cloudfront.net/blog/wp-content/uploads/explain-diagram1.pdf 20

EXPLAIN EXTENDED EXAMPLE» Shows optimizer transformation of query Can help you write better SQL 21

OPTIMIZER TRACING Stored in information_schema.optimizer_trace Default is 16k of Memory set optimizer_trace_max_mem_size=1000000; set optimizer_trace_features="greedy_search=off"; Not as verbose 22

MYSQL WORKBENCH 23

REVIEW TABLE & INDEX INFO» Table Definition Is it really a table or is it a view Size of tables / Quick way: mysqlshow status database {table} {column}» Examine Columns in Where Clause Cardinality of columns / data skew Do they have indexes?» Know Existing Indexes Columns those indexes contain If multi-column, know the left leading column Cardinality 24

CASE STUDIES» SQL Diagramming Who registered yesterday for Tuning Class Look up Overdue DVD Rentals by Customer Weekly Sales Rep Report by Category by Region 25

SQL STATEMENT 1» Who registered yesterday for SQL Tuning SELECT s.fname, s.lname, r.signup_date FROM student s INNER JOIN registration r ON s.student_id = r.student_id INNER JOIN class c ON r.class_id = c.class_id WHERE c.name = 'SQL TUNING' AND r.signup_date BETWEEN DATE_SUB('@BillDate', INTERVAL 1 DAY) and '@BillDate' AND r.cancelled = 'N'; # of Execs / Hr: 9,320 Avg Exec Time: 9 seconds Rows Examined: 872,095,872 Rows Sent: 0 Wait State: 99% on sending data 26

RESPONSE TIME DATA 27

EXECUTION PLAN 28

SQL DIAGRAMMING» Great Book SQL Tuning by Dan Tow Great book that teaches SQL Diagramming http://www.singingsql.com registration 5 30 5% 1 student 1 class.2% select count(*) from registration r where r.signup_date BETWEEN DATE_SUB('@BillDate', INTERVAL 1 DAY) and '@BillDate' AND r.cancelled = 'N'; 4,228 / 79,981 * 100 = 5.2% select count(0) from class where name = 'SQL TUNING' 2 / 1,000 =.2% 29

RELATIONSHIP DIAGRAM No relationships? 30

ADD & VIEW RELATIONSHIPS» ALTER TABLE registration ADD FOREIGN KEY (student_id) REFERENCES student(student_id);» ALTER TABLE registration ADD FOREIGN KEY (class_id) REFERENCES class(class_id);» SELECT table_name,» column_name,constraint_name,» referenced_table_name,referenced_column_name» FROM information_schema.key_column_usage» WHERE table_schema = 'csu' AND table_name = 'registration'» AND referenced_column_name IS NOT NULL; 31

NEW EXECUTION PLAN» CREATE INDEX class_nm ON class(name); 32

DID WE SPEED IT UP? F Keys added Added class_nm # of Execs / Hr: 86,354 Avg Exec Time: 432 milliseconds Rows Examined: 2.7m Rows Sent: 1.1m Wait State: 98% on sending data 33

SQL STATEMENT 2» Look up Overdue DVD Rentals by Customer for Last Month SELECT CONCAT(customer.last_name, ', ', customer.first_name) customer, address.phone, film.title, rental_date, FROM rental INNER JOIN customer ON rental.customer_id = customer.customer_id INNER JOIN address ON customer.address_id = address.address_id INNER JOIN inventory ON rental.inventory_id = inventory.inventory_id INNER JOIN film ON inventory.film_id = film.film_id WHERE rental.return_date IS NULL AND rental_date + INTERVAL film.rental_duration DAY > DATE_SUB(CURRENT_DATE(), INTERVAL 31 DAY) AND customer.last_name like %@ln%' ORDER BY rental_date desc; # of Execs / Hr: 950 Avg Exec Time: 2:07 minutes Rows Examined: 424m Rows Sent: 59m Wait State: 99% on sending data 34

RTA DATA 35

EXECUTION PLAN 36

SQL DIAGRAMMING inventory 3 1 rental 94 7% 91 1 customer 1.2% 1 film 1 address select count(*) from rental where return_date is null and rental_date> DATE_SUB(CURRENT_DATE(), INTERVAL 31 DAY); 85568 / 1175665 * 100 = 7.27% select avg(cnt) from (select last_name, count(*) cnt from customer group by last_name) a; 32 / 19168 * 100 =.16 37

RELATIONSHIP DIAGRAM Why doesn t it use this index? 38

SLOPPY CODING NEED TO FIX QUERY 39

NEW EXECUTION PLAN 40

DID WE IMPROVE IT? 41

SQL STATEMENT 3» Weekly Sales Rep Report by Category by Region SELECT staff.first_name, staff.last_name,city.city, country.country,category.name AS category,sum(payment.amount) AS total_sales FROM payment INNER JOIN rental ON payment.rental_id = rental.rental_id INNER JOIN inventory ON rental.inventory_id = inventory.inventory_id INNER JOIN film ON inventory.film_id = film.film_id INNER JOIN film_category ON film.film_id = film_category.film_id INNER JOIN category ON film_category.category_id = category.category_id INNER JOIN store ON store.store_id = inventory.store_id INNER JOIN address ON address.address_id = store.address_id INNER JOIN city ON city.city_id = address.city_id INNER JOIN country ON country.country_id = city.country_id INNER JOIN staff ON staff.staff_id = rental.staff_id WHERE rental_date > DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK) AND return_date IS NOT NULL AND staff.last_name like '@staff' GROUP BY staff.first_name, staff.last_name,city.city, country.country,category.name ORDER BY total_sales DESC; 42

RTA DATA 43

EXECUTION PLAN 44

INDEXES ON FILM 45

SQL DIAGRAMMING payment rental 2% film_category inventory staff.5% category film address city country select count(*) from rental WHERE rental_date > DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK) AND return_date IS NOT NULL 14355 / 1175665 * 100 = 1.8% select avg(cnt) from (select staff.last_name, count(*) cnt from staff group by staff.last_name) s; 1 / 202 * 100 = 0.49% 46

DATABASE DIAGRAM 47

NEW EXECUTION PLAN CREATE INDEX staff_ln ON staff(last_name); 48

IMPROVED RTA Added staff_ln 49

MONITOR» Monitor the improvement Be able to prove that tuning made a difference Take new metric measurements Compare them to initial readings Brag about the improvements no one else will» Monitor for next tuning opportunity Tuning is iterative There is always room for improvement Make sure you tune things that make a difference» Shameless Product Pitch - DPA 50

SUMMARY» Tuning Queries gives more bang for the buck» Make sure you are tuning the correct query Look at response time (RTA) not health metrics» Use Thread States, Wait Instruments Locking problems may not be a Query Tuning issue Thread states tell you where to start» Use Explain Extended to get the execution plan To see the query transformation» Consider SQL Diagramming A scientific approach to tuning» Monitor For Next Tuning Opportunity 50

FREE TRIAL» Resolve performance issues QUICKLY» Try Database Performance Analyzer FREE for 14 days» Improve root cause of slow performance Quickly identify root cause of issues that impact end-user response time See historical trends over days, months, and years Understand impact of VMware performance Agentless architecture, installs in minutes www.solarwinds.com/dpa-download/ 52

ABOUT SOLARWINDS 52

THANK YOU! The SOLARWINDS and SOLARWINDS & Design marks are the exclusive property of SolarWinds Worldwide, LLC, are registered with the U.S. Patent and Trademark Office, and may be registered or pending registration in other countries. All other SolarWinds trademarks, service marks, and logos may be common law marks, registered or pending registration in the United States or in other countries. All other trademarks mentioned herein are used for identification purposes only and may be or are trademarks or registered trademarks of their respective companies.