SlideShare a Scribd company logo
1 of 42
© 2017 Percona1
MySQL Query Optimization Best
Practices
and Indexing
Alkin Tezuysal – Sr. Technical Manager
Percona
© 2017 Percona2
Who am I? @ask_dba
© 2017 Percona3
About Percona
Solutions for your success with MySQL , MongoDB and PostgreSQL
Support, Managed Services, Software
Our Software is 100% Open Source
Support Broad Ecosystem – MySQL, MariaDB, Amazon RDS
In Business for 12 years
More than 3000 customers, including top Internet companies and enterprises
© 2017 Percona4
About This Presentation
Indexing Basics
Finding and
Identifying
Slow Queries
Utilizing Explain
Plan
Advanced
Indexing
Tooling and
more
© 2017 Percona5
Indexing Basics
• What it does?
• Increase speed of given lookup (SQL)
• Access and maintain changes
• Helps Optimizer to reach its goal
© 2017 Percona6
Why do we need indexes?
• Data persists on disks
• Disks cheap but slow
• Data can be in memory
• Memory fast but expensive
Index is the answer to access data fast.
CREATE INDEX part_of_name ON customer (name(10));
© 2017 Percona7
Traversal
1. Tree Traversal
2. Follow leaf node chain
3. Fetch the table data
© 2017 Percona8
Leaf Nodes
1. Establish doubly linked list
2. Connect index leaf nodes
3. Indexed columns
© 2017 Percona9
B-tree Structure
© 2017 Percona10
Slow Index Lookups
• Low cardinality
• Large data sets
• Multiple index traversal
• Index column used as argument
• Looking for suffix
• Non-leading column lookup
• Data type mismatch
• Character Set / Collation mismatch
• MySQL Bug
© 2017 Percona11
The Optimizer
© 2017 Percona12
MySQL Optimizer
• Cost based
• Assign costs to select operations
• Assign costs to partial or alternate plans
• Seek for lowest cost
Access Method Join Order Subquery Strategy
© 2017 Percona13
Cost Model
© 2017 Percona14
Finding and Identifying Slow Queries
• Slow Query Log
• PMM/QAN
• Network sniff
• Others (Licensed)
• MySQL EM
• Vividcortex
• Solarwinds
• Monyog
© 2017 Percona15
Slow Query Tools
• Explain Plan
• Tabular
• JSON (5.7)
• Visual (Workbench)
• Running Query (5.7)
• pt-query-digest
• pt-visual-explain
• Performance Schema
• MySQL Sys Schema
• Optimizer Trace
• MySQL Workbench
• Status Variables
• show status like ‘Sort%’
• show status like ‘Handler%’
© 2017 Percona16
PMM/QAN
© 2017 Percona17
PMM/QAN
© 2017 Percona18
PMM/QAN
© 2017 Percona19
PMM Demo - https://pmmdemo.percona.com/
© 2017 Percona20
Explain Plan
© 2017 Percona21
Explain Plan (JSON)
> EXPLAIN format=JSON SELECT CONCAT(customer.last_name, ', ', customer.first_name) AS customer, address.phone, film.title 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 < CURRENT_DATE() LIMIT 5G
*************************** 1. row ***************************
EXPLAIN: {
"query_block": {
"select_id": 1,
"nested_loop": [
{
"table": {
"table_name": "film",
"access_type": "ALL",
"possible_keys": [
"PRIMARY"
],
"rows": 1000,
"filtered": 100
}
},
…
…
© 2017 Percona22
Explain Plan (pt-visual-explain)
JOIN
+- Bookmark lookup
| +- Table
| | table address
| | possible_keys PRIMARY
| +- Unique index lookup
| key address->PRIMARY
| possible_keys PRIMARY
| key_len 2
| ref sakila.customer.address_id
| rows 1
+- JOIN
+- Bookmark lookup
| +- Table
| | table customer
| | possible_keys PRIMARY,idx_fk_address_id
| +- Unique index lookup
| key customer->PRIMARY
| possible_keys PRIMARY,idx_fk_address_id
| key_len 2
| ref sakila.rental.customer_id
| rows 1
...
© 2017 Percona23
Cost Based Access Method
1. Find the optimal method
2. Check if access method useful
3. Estimate the cost of using access method
4. Select low cost access method
© 2017 Percona24
Query Execution
Table Scan
Index Scan
Index
Lookup
Range
Scan
Index
Merge
Loose
Index Scan
© 2017 Percona25
Indexing Best Practices
• Always have Primary Key
• Physical order of table, if not created explicitly, MySQL will create
hidden one (Global Mutex)
• Fastest lookup is PK
© 2017 Percona26
Indexing Best Practices
• Single index with multiple columns
• Left most first and each additional field in a composite key
• Composite indexes better a.k.a Covering indexes
• PK is already part of composite indexes
© 2017 Percona27
Indexing Best Practices
• Equality first, range next
• Ex:
select first_name, last_name, birth_date from
employees
where date_of_birth => to_date (?, `YYYY-MM-DD`)
and date_of_birth <= to_date (?, `YYYY-MM-DD`)
and branch_id = ?
© 2017 Percona28
Indexing Best Practices
• One index scan is faster than two
• Avoid duplicate indexes pt-duplicate-key-checker
© 2017 Percona29
Indexing Best Practices
• Data types matter. Numeric for numbers.
• Ex:
select …
from …
where numeric_value = `48`
© 2017 Percona30
Query Optimization Best Practices
• Negative clauses and subqueries aren’t as good as positive
clauses
• Ex:
• IS NOT
• IS NOT NULL
• NOT IN
• NOT LIKE
© 2017 Percona31
Query Optimization Best Practices
• User INNER instead of LEFT where you can
© 2017 Percona32
Query Optimization Best Practices
• UNION ALL is better than UNION
UNION
UNION ALL
© 2017 Percona33
Query Optimization Best Practices
• ORDER BY can be expensive
SELECT * FROM t1
ORDER BY idx_c1, idx_c2;
• Avoid while sorting small set of data (Use code)
cust
_id
first_na
me
last_
name
email
1 Billy Joel bb7@bluen
ot.com
2 Jane Fond
a
jf1950@yah
oo.com
3 Mark Welt
on
markW1912
@gmail.co
m
4 Linda Joey linda.joey@
yandex.com
5 Sidney Travo
r
sidney.travo
r@icloud.co
m
6 Jordan Velez jordanv@a
mazon.com
© 2017 Percona34
Query Optimization Best Practices
• Watch out those ORDER BY + LIMIT operations
• These usually return small set of data with big cost (filesort)
SELECT col1, ... FROM t1 ... ORDER BY name LIMIT
10;
SELECT col1, ... FROM t1 ... ORDER BY RAND()
LIMIT 15;
© 2017 Percona35
Query Optimization Best Practices
• Watch out those ORDER BY + LIMIT operations
• These usually return small set of data with big cost (filesort)
SELECT col1, ... FROM t1 ... ORDER BY name LIMIT
10;
SELECT col1, ... FROM t1 ... ORDER BY RAND()
LIMIT 15;
© 2017 Percona36
MySQL Index Types
• B-tree (Common)
• Fractal Tree
• LSM Tree
• R-Tree (Spatial)
• Hash (Memory)
• Engine-dependent
© 2017 Percona37
Advanced Indexing
• Optimizer hints
• Global: The hint affects the entire statement
• Query block: The hint affects a particular query block within a statement
• Table-level: The hint affects a particular table within a query block
• Index-level: The hint affects a particular index within a table
• Index hints
• SELECT * FROM t1 USE INDEX (i1) IGNORE INDEX FOR
ORDER BY (i2) ORDER BY a;
© 2017 Percona38
If indexes not enough
• Query Re-write
• ProxySQL
• https://www.percona.com/blog/2018/05/02/proxysql-query-rewrite-
use-case/
• MySQL 5.7: Query Rewrite Plugin
• Add hints
• Modify join order
© 2017 Percona39
Advanced Queries with ProxySQL – Query rewrite
engine
• Most wanted feature by DBAs
• Rewrite queries overloading the database on the fly.
Application A
ProxySQL
• Simply buy time until application can be modified
Application B
MySQL
Master
MySQL
Slave
MySQL
Slave
MySQL
Slave
Query
Rewriting
MySQL
Slave
MySQL
Slave
© 2017 Percona40
Final Thoughts
Optimizer is not smart as DBAs
• Help to choose best possible path
• Improve throughput
Add only indexes you need
• Avoid duplicate indexing
• Avoid overhead disk space, extra i/o ops
Stay on current version of MySQL
• Several bugs fixed
• Optimizer and Engine improvements in place
© 2017 Percona41
References and Credits
• Markus Winand (2018) - SQL Performance Explained (2018)
• Otstein Grovlen (2017- How to Analyze and Tune MySQL Queries for
Better Performance
• Sveta Smirnova (2018) – Introduction into MySQL Query Tuning
• Oracle Reference Manual
• Jeremy Cole (2013) - How does InnoDB behave without a Primary
Key?
• Tata McDaniel (2018) - Visualize This! MySQL Tools That Explain
Queries
• Reviewers: Daniel G Burgos, Tate McDaniel, Janos Ruszo
DATABASE PERFORMANCE
MATTERS
Database Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance Matters

More Related Content

Similar to Alkin Tezuysal "MySQL Query Optimization Best Practices and Indexing"

How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...
How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...
How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...
DataWorks Summit
 

Similar to Alkin Tezuysal "MySQL Query Optimization Best Practices and Indexing" (20)

[db tech showcase OSS 2017] A11: How Percona is Different, and How We Support...
[db tech showcase OSS 2017] A11: How Percona is Different, and How We Support...[db tech showcase OSS 2017] A11: How Percona is Different, and How We Support...
[db tech showcase OSS 2017] A11: How Percona is Different, and How We Support...
 
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
 
How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...
How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...
How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...
 
Implement DevOps Like a Unicorn—Even If You’re Not One
Implement DevOps Like a Unicorn—Even If You’re Not OneImplement DevOps Like a Unicorn—Even If You’re Not One
Implement DevOps Like a Unicorn—Even If You’re Not One
 
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
 
Novinky v Oracle Database 18c
Novinky v Oracle Database 18cNovinky v Oracle Database 18c
Novinky v Oracle Database 18c
 
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
 
Mysql ecosystem in 2018
Mysql ecosystem in 2018Mysql ecosystem in 2018
Mysql ecosystem in 2018
 
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
 
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim TkachenkoWebinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
 
How to upgrade like a boss to my sql 8.0?
How to upgrade like a boss to my sql 8.0?How to upgrade like a boss to my sql 8.0?
How to upgrade like a boss to my sql 8.0?
 
Workshop on Advanced Design Patterns for Amazon DynamoDB - DAT405 - re:Invent...
Workshop on Advanced Design Patterns for Amazon DynamoDB - DAT405 - re:Invent...Workshop on Advanced Design Patterns for Amazon DynamoDB - DAT405 - re:Invent...
Workshop on Advanced Design Patterns for Amazon DynamoDB - DAT405 - re:Invent...
 
Technical Introduction to PostgreSQL and PPAS
Technical Introduction to PostgreSQL and PPASTechnical Introduction to PostgreSQL and PPAS
Technical Introduction to PostgreSQL and PPAS
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizer
 
Optimizing Open Source for Greater Database Savings & Control
Optimizing Open Source for Greater Database Savings & ControlOptimizing Open Source for Greater Database Savings & Control
Optimizing Open Source for Greater Database Savings & Control
 
NoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
NoSQL on MySQL - MySQL Document Store by Vadim TkachenkoNoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
NoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
 
MySQL Replication — Advanced Features / Петр Зайцев (Percona)
MySQL Replication — Advanced Features / Петр Зайцев (Percona)MySQL Replication — Advanced Features / Петр Зайцев (Percona)
MySQL Replication — Advanced Features / Петр Зайцев (Percona)
 
Optimize with Open Source
Optimize with Open SourceOptimize with Open Source
Optimize with Open Source
 
Optimizing Open Source for Greater Database Savings and Control
Optimizing Open Source for Greater Database Savings and ControlOptimizing Open Source for Greater Database Savings and Control
Optimizing Open Source for Greater Database Savings and Control
 
Case Study: Sprinklr Uses Amazon EBS to Maximize Its NoSQL Deployment - DAT33...
Case Study: Sprinklr Uses Amazon EBS to Maximize Its NoSQL Deployment - DAT33...Case Study: Sprinklr Uses Amazon EBS to Maximize Its NoSQL Deployment - DAT33...
Case Study: Sprinklr Uses Amazon EBS to Maximize Its NoSQL Deployment - DAT33...
 

More from Fwdays

More from Fwdays (20)

"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y..."How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
 
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"What is a RAG system and how to build it",Dmytro Spodarets
"What is a RAG system and how to build it",Dmytro Spodarets"What is a RAG system and how to build it",Dmytro Spodarets
"What is a RAG system and how to build it",Dmytro Spodarets
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"Distributed graphs and microservices in Prom.ua", Maksym Kindritskyi
"Distributed graphs and microservices in Prom.ua",  Maksym Kindritskyi"Distributed graphs and microservices in Prom.ua",  Maksym Kindritskyi
"Distributed graphs and microservices in Prom.ua", Maksym Kindritskyi
 
"Rethinking the existing data loading and processing process as an ETL exampl...
"Rethinking the existing data loading and processing process as an ETL exampl..."Rethinking the existing data loading and processing process as an ETL exampl...
"Rethinking the existing data loading and processing process as an ETL exampl...
 
"How Ukrainian IT specialist can go on vacation abroad without crossing the T...
"How Ukrainian IT specialist can go on vacation abroad without crossing the T..."How Ukrainian IT specialist can go on vacation abroad without crossing the T...
"How Ukrainian IT specialist can go on vacation abroad without crossing the T...
 
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ..."The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
 
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu..."[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
 
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care..."[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
 
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"..."4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
 
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout", Anast...
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout",  Anast..."Reconnecting with Purpose: Rediscovering Job Interest after Burnout",  Anast...
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout", Anast...
 
"Mentoring 101: How to effectively invest experience in the success of others...
"Mentoring 101: How to effectively invest experience in the success of others..."Mentoring 101: How to effectively invest experience in the success of others...
"Mentoring 101: How to effectively invest experience in the success of others...
 
"Mission (im) possible: How to get an offer in 2024?", Oleksandra Myronova
"Mission (im) possible: How to get an offer in 2024?",  Oleksandra Myronova"Mission (im) possible: How to get an offer in 2024?",  Oleksandra Myronova
"Mission (im) possible: How to get an offer in 2024?", Oleksandra Myronova
 
"Why have we learned how to package products, but not how to 'package ourselv...
"Why have we learned how to package products, but not how to 'package ourselv..."Why have we learned how to package products, but not how to 'package ourselv...
"Why have we learned how to package products, but not how to 'package ourselv...
 
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin..."How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Alkin Tezuysal "MySQL Query Optimization Best Practices and Indexing"

  • 1. © 2017 Percona1 MySQL Query Optimization Best Practices and Indexing Alkin Tezuysal – Sr. Technical Manager Percona
  • 2. © 2017 Percona2 Who am I? @ask_dba
  • 3. © 2017 Percona3 About Percona Solutions for your success with MySQL , MongoDB and PostgreSQL Support, Managed Services, Software Our Software is 100% Open Source Support Broad Ecosystem – MySQL, MariaDB, Amazon RDS In Business for 12 years More than 3000 customers, including top Internet companies and enterprises
  • 4. © 2017 Percona4 About This Presentation Indexing Basics Finding and Identifying Slow Queries Utilizing Explain Plan Advanced Indexing Tooling and more
  • 5. © 2017 Percona5 Indexing Basics • What it does? • Increase speed of given lookup (SQL) • Access and maintain changes • Helps Optimizer to reach its goal
  • 6. © 2017 Percona6 Why do we need indexes? • Data persists on disks • Disks cheap but slow • Data can be in memory • Memory fast but expensive Index is the answer to access data fast. CREATE INDEX part_of_name ON customer (name(10));
  • 7. © 2017 Percona7 Traversal 1. Tree Traversal 2. Follow leaf node chain 3. Fetch the table data
  • 8. © 2017 Percona8 Leaf Nodes 1. Establish doubly linked list 2. Connect index leaf nodes 3. Indexed columns
  • 10. © 2017 Percona10 Slow Index Lookups • Low cardinality • Large data sets • Multiple index traversal • Index column used as argument • Looking for suffix • Non-leading column lookup • Data type mismatch • Character Set / Collation mismatch • MySQL Bug
  • 12. © 2017 Percona12 MySQL Optimizer • Cost based • Assign costs to select operations • Assign costs to partial or alternate plans • Seek for lowest cost Access Method Join Order Subquery Strategy
  • 14. © 2017 Percona14 Finding and Identifying Slow Queries • Slow Query Log • PMM/QAN • Network sniff • Others (Licensed) • MySQL EM • Vividcortex • Solarwinds • Monyog
  • 15. © 2017 Percona15 Slow Query Tools • Explain Plan • Tabular • JSON (5.7) • Visual (Workbench) • Running Query (5.7) • pt-query-digest • pt-visual-explain • Performance Schema • MySQL Sys Schema • Optimizer Trace • MySQL Workbench • Status Variables • show status like ‘Sort%’ • show status like ‘Handler%’
  • 19. © 2017 Percona19 PMM Demo - https://pmmdemo.percona.com/
  • 21. © 2017 Percona21 Explain Plan (JSON) > EXPLAIN format=JSON SELECT CONCAT(customer.last_name, ', ', customer.first_name) AS customer, address.phone, film.title 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 < CURRENT_DATE() LIMIT 5G *************************** 1. row *************************** EXPLAIN: { "query_block": { "select_id": 1, "nested_loop": [ { "table": { "table_name": "film", "access_type": "ALL", "possible_keys": [ "PRIMARY" ], "rows": 1000, "filtered": 100 } }, … …
  • 22. © 2017 Percona22 Explain Plan (pt-visual-explain) JOIN +- Bookmark lookup | +- Table | | table address | | possible_keys PRIMARY | +- Unique index lookup | key address->PRIMARY | possible_keys PRIMARY | key_len 2 | ref sakila.customer.address_id | rows 1 +- JOIN +- Bookmark lookup | +- Table | | table customer | | possible_keys PRIMARY,idx_fk_address_id | +- Unique index lookup | key customer->PRIMARY | possible_keys PRIMARY,idx_fk_address_id | key_len 2 | ref sakila.rental.customer_id | rows 1 ...
  • 23. © 2017 Percona23 Cost Based Access Method 1. Find the optimal method 2. Check if access method useful 3. Estimate the cost of using access method 4. Select low cost access method
  • 24. © 2017 Percona24 Query Execution Table Scan Index Scan Index Lookup Range Scan Index Merge Loose Index Scan
  • 25. © 2017 Percona25 Indexing Best Practices • Always have Primary Key • Physical order of table, if not created explicitly, MySQL will create hidden one (Global Mutex) • Fastest lookup is PK
  • 26. © 2017 Percona26 Indexing Best Practices • Single index with multiple columns • Left most first and each additional field in a composite key • Composite indexes better a.k.a Covering indexes • PK is already part of composite indexes
  • 27. © 2017 Percona27 Indexing Best Practices • Equality first, range next • Ex: select first_name, last_name, birth_date from employees where date_of_birth => to_date (?, `YYYY-MM-DD`) and date_of_birth <= to_date (?, `YYYY-MM-DD`) and branch_id = ?
  • 28. © 2017 Percona28 Indexing Best Practices • One index scan is faster than two • Avoid duplicate indexes pt-duplicate-key-checker
  • 29. © 2017 Percona29 Indexing Best Practices • Data types matter. Numeric for numbers. • Ex: select … from … where numeric_value = `48`
  • 30. © 2017 Percona30 Query Optimization Best Practices • Negative clauses and subqueries aren’t as good as positive clauses • Ex: • IS NOT • IS NOT NULL • NOT IN • NOT LIKE
  • 31. © 2017 Percona31 Query Optimization Best Practices • User INNER instead of LEFT where you can
  • 32. © 2017 Percona32 Query Optimization Best Practices • UNION ALL is better than UNION UNION UNION ALL
  • 33. © 2017 Percona33 Query Optimization Best Practices • ORDER BY can be expensive SELECT * FROM t1 ORDER BY idx_c1, idx_c2; • Avoid while sorting small set of data (Use code) cust _id first_na me last_ name email 1 Billy Joel bb7@bluen ot.com 2 Jane Fond a jf1950@yah oo.com 3 Mark Welt on markW1912 @gmail.co m 4 Linda Joey linda.joey@ yandex.com 5 Sidney Travo r sidney.travo r@icloud.co m 6 Jordan Velez jordanv@a mazon.com
  • 34. © 2017 Percona34 Query Optimization Best Practices • Watch out those ORDER BY + LIMIT operations • These usually return small set of data with big cost (filesort) SELECT col1, ... FROM t1 ... ORDER BY name LIMIT 10; SELECT col1, ... FROM t1 ... ORDER BY RAND() LIMIT 15;
  • 35. © 2017 Percona35 Query Optimization Best Practices • Watch out those ORDER BY + LIMIT operations • These usually return small set of data with big cost (filesort) SELECT col1, ... FROM t1 ... ORDER BY name LIMIT 10; SELECT col1, ... FROM t1 ... ORDER BY RAND() LIMIT 15;
  • 36. © 2017 Percona36 MySQL Index Types • B-tree (Common) • Fractal Tree • LSM Tree • R-Tree (Spatial) • Hash (Memory) • Engine-dependent
  • 37. © 2017 Percona37 Advanced Indexing • Optimizer hints • Global: The hint affects the entire statement • Query block: The hint affects a particular query block within a statement • Table-level: The hint affects a particular table within a query block • Index-level: The hint affects a particular index within a table • Index hints • SELECT * FROM t1 USE INDEX (i1) IGNORE INDEX FOR ORDER BY (i2) ORDER BY a;
  • 38. © 2017 Percona38 If indexes not enough • Query Re-write • ProxySQL • https://www.percona.com/blog/2018/05/02/proxysql-query-rewrite- use-case/ • MySQL 5.7: Query Rewrite Plugin • Add hints • Modify join order
  • 39. © 2017 Percona39 Advanced Queries with ProxySQL – Query rewrite engine • Most wanted feature by DBAs • Rewrite queries overloading the database on the fly. Application A ProxySQL • Simply buy time until application can be modified Application B MySQL Master MySQL Slave MySQL Slave MySQL Slave Query Rewriting MySQL Slave MySQL Slave
  • 40. © 2017 Percona40 Final Thoughts Optimizer is not smart as DBAs • Help to choose best possible path • Improve throughput Add only indexes you need • Avoid duplicate indexing • Avoid overhead disk space, extra i/o ops Stay on current version of MySQL • Several bugs fixed • Optimizer and Engine improvements in place
  • 41. © 2017 Percona41 References and Credits • Markus Winand (2018) - SQL Performance Explained (2018) • Otstein Grovlen (2017- How to Analyze and Tune MySQL Queries for Better Performance • Sveta Smirnova (2018) – Introduction into MySQL Query Tuning • Oracle Reference Manual • Jeremy Cole (2013) - How does InnoDB behave without a Primary Key? • Tata McDaniel (2018) - Visualize This! MySQL Tools That Explain Queries • Reviewers: Daniel G Burgos, Tate McDaniel, Janos Ruszo
  • 42. DATABASE PERFORMANCE MATTERS Database Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance Matters