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.