SlideShare a Scribd company logo
<Insert Picture Here>

Optimizing MySQL

Morgan Tocker, MySQL Community Manager

http://www.tocker.ca/

Safe Harbor Statement
The	
  following	
  is	
  intended	
  to	
  outline	
  our	
  general	
  product	
  direction.	
  
It	
  is	
  intended	
  for	
  information	
  purposes	
  only,	
  and	
  may	
  not	
  be	
  
incorporated	
  into	
  any	
  contract.	
  It	
  is	
  not	
  a	
  commitment	
  to	
  deliver	
  
any	
  material,	
  code,	
  or	
  functionality,	
  and	
  should	
  not	
  be	
  relied	
  upon	
  
in	
  making	
  purchasing	
  decisions.	
  



The	
  development,	
  release,	
  and	
  timing	
  of	
  any	
  features	
  or	
  
functionality	
  described	
  for	
  Oracle’s	
  products	
  remains	
  at	
  the	
  sole	
  
discretion	
  of	
  Oracle.
Options
•
•
•
•

Upgrade Hardware and/or MySQL Version
Optimize Configuration
Optimize Queries
Optimize Schema
Commentary
• There are some specific cases where upgrades can
make individual queries faster (i.e. subqueries). When I
like this optimization the most, is to increase“capacity”.
Lots of multi-core, multi-disk enhancements in 5.5 / 5.6.
• MySQL 5.6 comes with a much more optimized
configuration. There are now only 3-4 settings that
need to change.
• Optimizing queries is my favourite method :) There can
be some big wins here.
• Optimizing schema relates to query optimization.
Sometimes you have to change schema to support
certain queries, but it always starts with the queries.
Optimizing Queries
• Allows you to focus on queries that damage user
experiences rather than just slow.
• Best method is to install a profiler.
• MySQL also supports slow query logging +
Performance Schema (5.6+).
# Continent Asia + population > 5M!
mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' 

AND population > 5000000G!
*************************** 1. row ***************************!
id: 1!
select_type: SIMPLE!
table: Country!
type: ALL!
possible_keys: NULL!
key: NULL!
key_len: NULL!
ref: NULL!
rows: 267!
Extra: Using where!
1 row in set (0.00 sec)
mysql> ALTER TABLE Country ADD INDEX p (population);!
Query OK, 0 rows affected (0.02 sec)!
Records: 0 Duplicates: 0 Warnings: 0!

!

mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' 

AND population > 5000000G!
*************************** 1. row ***************************!
id: 1!
select_type: SIMPLE!
table: Country!
type: ALL!
possible_keys: p!
key: NULL!
key_len: NULL!
ref: NULL!
rows: 264!
Extra: Using where!
1 row in set (0.01 sec)
# 50 Million!
mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' 

AND population > 50000000G!
*************************** 1. row ***************************!
id: 1!
select_type: SIMPLE!
table: Country!
type: range!
possible_keys: p!
key: p!
key_len: 4!
ref: NULL!
rows: 24!
Extra: Using index condition; Using where!
1 row in set (0.00 sec)
mysql> ALTER TABLE Country ADD INDEX c (continent);!
Query OK, 0 rows affected (0.02 sec)!
Records: 0 Duplicates: 0 Warnings: 0!

!

mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' !
AND population > 50000000G!
*************************** 1. row ***************************!
id: 1!
select_type: SIMPLE!
table: Country!
type: ref!
possible_keys: p,c!
key: c!
key_len: 1!
ref: const!
rows: 51!
Extra: Using index condition; Using where!
1 row in set (0.00 sec)
mysql> ALTER TABLE Country ADD INDEX pc (population,continent);!
Query OK, 0 rows affected (0.02 sec)!
Records: 0 Duplicates: 0 Warnings: 0!

!

mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' !
AND population > 50000000G!
*************************** 1. row ***************************!
id: 1!
select_type: SIMPLE!
table: Country!
type: ref!
possible_keys: p,c,pc!
key: c!
key_len: 1!
ref: const!
rows: 51!
Extra: Using index condition; Using where!
1 row in set (0.00 sec)
mysql> ALTER TABLE Country ADD INDEX cp (continent,population);!
Query OK, 0 rows affected (0.01 sec)!
Records: 0 Duplicates: 0 Warnings: 0!

!

mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' !
AND population > 50000000G!
*************************** 1. row ***************************!
id: 1!
select_type: SIMPLE!
table: Country!
type: range!
possible_keys: p,c,pc,cp!
key: cp!
key_len: 5!
ref: NULL!
rows: 11!
Extra: Using index condition!
1 row in set (0.00 sec)
Tips
• Think of where you can use indexes to eliminate
rows - that’s the column you typically index.
• Read: http://dev.mysql.com/doc/refman/5.6/en/
explain.html
• Composite indexes somewhat an advanced topic:
• Useful when a single column does not eliminate
enough work.
• “Ranges to the right”

More Related Content

PDF
MySQL 5.6 - Operations and Diagnostics Improvements
PDF
Mysql 57-upcoming-changes
PDF
MySQL 5.7 + JSON
PDF
MySQL Query Optimization
PDF
My sql 5.7-upcoming-changes-v2
PDF
MySQL NoSQL APIs
PDF
MySQL 5.7: Core Server Changes
PDF
MySQL For Linux Sysadmins
MySQL 5.6 - Operations and Diagnostics Improvements
Mysql 57-upcoming-changes
MySQL 5.7 + JSON
MySQL Query Optimization
My sql 5.7-upcoming-changes-v2
MySQL NoSQL APIs
MySQL 5.7: Core Server Changes
MySQL For Linux Sysadmins

What's hot (20)

PDF
MySQL Server Defaults
PDF
Upcoming changes in MySQL 5.7
PDF
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
PDF
The InnoDB Storage Engine for MySQL
PDF
MySQL Performance Schema in 20 Minutes
PDF
Mysql server query path
ODP
MySQL 5.7 - What's new and How to upgrade
PDF
MySQL Performance Metrics that Matter
PPT
Understanding MySql locking issues
PDF
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
PDF
MySQL: From Single Instance to Big Data
PDF
What's New MySQL 8.0?
PDF
Performance Schema for MySQL Troubleshooting
PDF
MariaDB Optimizer
PDF
How to migrate from MySQL to MariaDB without tears
PDF
How to Avoid Pitfalls in Schema Upgrade with Percona XtraDB Cluster
PDF
MySQL 5.7 in a Nutshell
PDF
Using Apache Spark and MySQL for Data Analysis
PDF
MySQL 5.6 Performance
PDF
Upgrade to MySQL 5.7 and latest news planned for MySQL 8
MySQL Server Defaults
Upcoming changes in MySQL 5.7
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
The InnoDB Storage Engine for MySQL
MySQL Performance Schema in 20 Minutes
Mysql server query path
MySQL 5.7 - What's new and How to upgrade
MySQL Performance Metrics that Matter
Understanding MySql locking issues
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
MySQL: From Single Instance to Big Data
What's New MySQL 8.0?
Performance Schema for MySQL Troubleshooting
MariaDB Optimizer
How to migrate from MySQL to MariaDB without tears
How to Avoid Pitfalls in Schema Upgrade with Percona XtraDB Cluster
MySQL 5.7 in a Nutshell
Using Apache Spark and MySQL for Data Analysis
MySQL 5.6 Performance
Upgrade to MySQL 5.7 and latest news planned for MySQL 8
Ad

Viewers also liked (20)

PPT
MySQL Atchitecture and Concepts
PDF
MySQL 5.5 Guide to InnoDB Status
PDF
Locking and Concurrency Control
PDF
InnoDB Internal
PDF
MySQL Cloud Service Deep Dive
PPT
MySql slides (ppt)
ODP
Mastering InnoDB Diagnostics
PPTX
MySQL Architecture and Engine
PDF
MariaDB: Connect Storage Engine
PDF
InnoDB Architecture and Performance Optimization, Peter Zaitsev
PPT
GoldenGate for MySQL 설치 시 필요한 사항
PPTX
Web vulnerability seminar2
PDF
Linux con europe_2014_full_system_rollback_btrfs_snapper_0
PDF
Using MySQL in Automated Testing
PPT
Developing Information Schema Plugins
PDF
Extending MySQL Enterprise Monitor
ODP
Introduction to MySQL Enterprise Monitor
PDF
MySQL for Oracle DBAs
ODP
Getting to Know MySQL Enterprise Monitor
MySQL Atchitecture and Concepts
MySQL 5.5 Guide to InnoDB Status
Locking and Concurrency Control
InnoDB Internal
MySQL Cloud Service Deep Dive
MySql slides (ppt)
Mastering InnoDB Diagnostics
MySQL Architecture and Engine
MariaDB: Connect Storage Engine
InnoDB Architecture and Performance Optimization, Peter Zaitsev
GoldenGate for MySQL 설치 시 필요한 사항
Web vulnerability seminar2
Linux con europe_2014_full_system_rollback_btrfs_snapper_0
Using MySQL in Automated Testing
Developing Information Schema Plugins
Extending MySQL Enterprise Monitor
Introduction to MySQL Enterprise Monitor
MySQL for Oracle DBAs
Getting to Know MySQL Enterprise Monitor
Ad

Similar to Optimizing MySQL (20)

PDF
Mysql query optimization
PDF
Query Optimization with MySQL 5.6: Old and New Tricks
PDF
Quick Wins
PDF
Why Use EXPLAIN FORMAT=JSON?
PPTX
Master class on SQL ( Beginner to Advance ).pptx
PDF
[Java Küche RDB 最前線 2015] MySQL 5.7技術アップデート
KEY
10x improvement-mysql-100419105218-phpapp02
KEY
10x Performance Improvements
PDF
30334823 my sql-cluster-performance-tuning-best-practices
PPTX
Confoo 2021 - MySQL Indexes & Histograms
PPTX
Database training for developers
PDF
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
PDF
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
PDF
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
PPTX
slides about : Introduction_to_SQL.pptx
PDF
Troubleshooting MySQL Performance
PPTX
Ten query tuning techniques every SQL Server programmer should know
PDF
Webinar 2013 advanced_query_tuning
Mysql query optimization
Query Optimization with MySQL 5.6: Old and New Tricks
Quick Wins
Why Use EXPLAIN FORMAT=JSON?
Master class on SQL ( Beginner to Advance ).pptx
[Java Küche RDB 最前線 2015] MySQL 5.7技術アップデート
10x improvement-mysql-100419105218-phpapp02
10x Performance Improvements
30334823 my sql-cluster-performance-tuning-best-practices
Confoo 2021 - MySQL Indexes & Histograms
Database training for developers
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
MySQL Indexes and Histograms - RMOUG Training Days 2022
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
slides about : Introduction_to_SQL.pptx
Troubleshooting MySQL Performance
Ten query tuning techniques every SQL Server programmer should know
Webinar 2013 advanced_query_tuning

More from Morgan Tocker (9)

PDF
Introducing Spirit - Online Schema Change
PDF
MySQL Usability Guidelines
PDF
My First 90 days with Vitess
PDF
FOSDEM MySQL and Friends Devroom
PDF
Introducing TiDB - Percona Live Frankfurt
PDF
TiDB Introduction - Boston MySQL Meetup Group
PDF
TiDB Introduction - San Francisco MySQL Meetup
PDF
TiDB Introduction
PDF
MySQL 8.0 Optimizer Guide
Introducing Spirit - Online Schema Change
MySQL Usability Guidelines
My First 90 days with Vitess
FOSDEM MySQL and Friends Devroom
Introducing TiDB - Percona Live Frankfurt
TiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction
MySQL 8.0 Optimizer Guide

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation theory and applications.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Big Data Technologies - Introduction.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx
Understanding_Digital_Forensics_Presentation.pptx
20250228 LYD VKU AI Blended-Learning.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx

Optimizing MySQL

  • 1. <Insert Picture Here> Optimizing MySQL
 Morgan Tocker, MySQL Community Manager
 http://www.tocker.ca/

  • 2. Safe Harbor Statement The  following  is  intended  to  outline  our  general  product  direction.   It  is  intended  for  information  purposes  only,  and  may  not  be   incorporated  into  any  contract.  It  is  not  a  commitment  to  deliver   any  material,  code,  or  functionality,  and  should  not  be  relied  upon   in  making  purchasing  decisions.   
 The  development,  release,  and  timing  of  any  features  or   functionality  described  for  Oracle’s  products  remains  at  the  sole   discretion  of  Oracle.
  • 3. Options • • • • Upgrade Hardware and/or MySQL Version Optimize Configuration Optimize Queries Optimize Schema
  • 4. Commentary • There are some specific cases where upgrades can make individual queries faster (i.e. subqueries). When I like this optimization the most, is to increase“capacity”. Lots of multi-core, multi-disk enhancements in 5.5 / 5.6. • MySQL 5.6 comes with a much more optimized configuration. There are now only 3-4 settings that need to change. • Optimizing queries is my favourite method :) There can be some big wins here. • Optimizing schema relates to query optimization. Sometimes you have to change schema to support certain queries, but it always starts with the queries.
  • 5. Optimizing Queries • Allows you to focus on queries that damage user experiences rather than just slow. • Best method is to install a profiler. • MySQL also supports slow query logging + Performance Schema (5.6+).
  • 6. # Continent Asia + population > 5M! mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' 
 AND population > 5000000G! *************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: ALL! possible_keys: NULL! key: NULL! key_len: NULL! ref: NULL! rows: 267! Extra: Using where! 1 row in set (0.00 sec)
  • 7. mysql> ALTER TABLE Country ADD INDEX p (population);! Query OK, 0 rows affected (0.02 sec)! Records: 0 Duplicates: 0 Warnings: 0! ! mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' 
 AND population > 5000000G! *************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: ALL! possible_keys: p! key: NULL! key_len: NULL! ref: NULL! rows: 264! Extra: Using where! 1 row in set (0.01 sec)
  • 8. # 50 Million! mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' 
 AND population > 50000000G! *************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: range! possible_keys: p! key: p! key_len: 4! ref: NULL! rows: 24! Extra: Using index condition; Using where! 1 row in set (0.00 sec)
  • 9. mysql> ALTER TABLE Country ADD INDEX c (continent);! Query OK, 0 rows affected (0.02 sec)! Records: 0 Duplicates: 0 Warnings: 0! ! mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' ! AND population > 50000000G! *************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: ref! possible_keys: p,c! key: c! key_len: 1! ref: const! rows: 51! Extra: Using index condition; Using where! 1 row in set (0.00 sec)
  • 10. mysql> ALTER TABLE Country ADD INDEX pc (population,continent);! Query OK, 0 rows affected (0.02 sec)! Records: 0 Duplicates: 0 Warnings: 0! ! mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' ! AND population > 50000000G! *************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: ref! possible_keys: p,c,pc! key: c! key_len: 1! ref: const! rows: 51! Extra: Using index condition; Using where! 1 row in set (0.00 sec)
  • 11. mysql> ALTER TABLE Country ADD INDEX cp (continent,population);! Query OK, 0 rows affected (0.01 sec)! Records: 0 Duplicates: 0 Warnings: 0! ! mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' ! AND population > 50000000G! *************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: range! possible_keys: p,c,pc,cp! key: cp! key_len: 5! ref: NULL! rows: 11! Extra: Using index condition! 1 row in set (0.00 sec)
  • 12. Tips • Think of where you can use indexes to eliminate rows - that’s the column you typically index. • Read: http://dev.mysql.com/doc/refman/5.6/en/ explain.html • Composite indexes somewhat an advanced topic: • Useful when a single column does not eliminate enough work. • “Ranges to the right”