SlideShare a Scribd company logo
© 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" (20)

PDF
[db tech showcase OSS 2017] A11: How Percona is Different, and How We Support...
Insight Technology, Inc.
 
PDF
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
Fwdays
 
PPTX
How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...
DataWorks Summit
 
PDF
Implement DevOps Like a Unicorn—Even If You’re Not One
TechWell
 
PPTX
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
OracleMySQL
 
PDF
Novinky v Oracle Database 18c
MarketingArrowECS_CZ
 
PPTX
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
SolarWinds
 
PPTX
Mysql ecosystem in 2018
Alkin Tezuysal
 
PDF
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
Dakiry
 
PPTX
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
Altinity Ltd
 
PPTX
How to upgrade like a boss to my sql 8.0?
Alkin Tezuysal
 
PDF
Technical Introduction to PostgreSQL and PPAS
Ashnikbiz
 
PPTX
Beginners guide to_optimizer
Maria Colgan
 
PDF
Optimizing Open Source for Greater Database Savings & Control
EDB
 
PPTX
NoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
Data Con LA
 
PPTX
MySQL Replication — Advanced Features / Петр Зайцев (Percona)
Ontico
 
PDF
Optimize with Open Source
EDB
 
PDF
Optimizing Open Source for Greater Database Savings and Control
EDB
 
PPTX
Webinar - Macy’s: Why Your Database Decision Directly Impacts Customer Experi...
DataStax
 
PPTX
MongoDB Evenings Chicago - Find Your Way in MongoDB 3.2: Compass and Beyond
MongoDB
 
[db tech showcase OSS 2017] A11: How Percona is Different, and How We Support...
Insight Technology, Inc.
 
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
Fwdays
 
How to Use Innovative Data Handling and Processing Techniques to Drive Alpha ...
DataWorks Summit
 
Implement DevOps Like a Unicorn—Even If You’re Not One
TechWell
 
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
OracleMySQL
 
Novinky v Oracle Database 18c
MarketingArrowECS_CZ
 
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
SolarWinds
 
Mysql ecosystem in 2018
Alkin Tezuysal
 
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
Dakiry
 
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
Altinity Ltd
 
How to upgrade like a boss to my sql 8.0?
Alkin Tezuysal
 
Technical Introduction to PostgreSQL and PPAS
Ashnikbiz
 
Beginners guide to_optimizer
Maria Colgan
 
Optimizing Open Source for Greater Database Savings & Control
EDB
 
NoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
Data Con LA
 
MySQL Replication — Advanced Features / Петр Зайцев (Percona)
Ontico
 
Optimize with Open Source
EDB
 
Optimizing Open Source for Greater Database Savings and Control
EDB
 
Webinar - Macy’s: Why Your Database Decision Directly Impacts Customer Experi...
DataStax
 
MongoDB Evenings Chicago - Find Your Way in MongoDB 3.2: Compass and Beyond
MongoDB
 

More from Fwdays (20)

PPTX
"Як ми переписали Сільпо на Angular", Євген Русаков
Fwdays
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
"Validation and Observability of AI Agents", Oleksandr Denisyuk
Fwdays
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
"Co-Authoring with a Machine: What I Learned from Writing a Book on Generativ...
Fwdays
 
PPTX
"Human-AI Collaboration Models for Better Decisions, Faster Workflows, and Cr...
Fwdays
 
PDF
"AI is already here. What will happen to your team (and your role) tomorrow?"...
Fwdays
 
PPTX
"Is it worth investing in AI in 2025?", Alexander Sharko
Fwdays
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
PDF
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
PDF
"Scaling in space and time with Temporal", Andriy Lupa .pdf
Fwdays
 
PPTX
"Provisioning via DOT-Chain: from catering to drone marketplaces", Volodymyr ...
Fwdays
 
PPTX
" Observability with Elasticsearch: Best Practices for High-Load Platform", A...
Fwdays
 
PPTX
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
PPTX
"Istio Ambient Mesh in production: our way from Sidecar to Sidecar-less",Hlib...
Fwdays
 
PPTX
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
PPTX
"Confidential AI: zero trust concept", Hennadiy Karpov
Fwdays
 
PPTX
"Choosing Tensor Accelerators for Specific Tasks: Compute vs Memory Bound Mod...
Fwdays
 
"Як ми переписали Сільпо на Angular", Євген Русаков
Fwdays
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
"Validation and Observability of AI Agents", Oleksandr Denisyuk
Fwdays
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
"Co-Authoring with a Machine: What I Learned from Writing a Book on Generativ...
Fwdays
 
"Human-AI Collaboration Models for Better Decisions, Faster Workflows, and Cr...
Fwdays
 
"AI is already here. What will happen to your team (and your role) tomorrow?"...
Fwdays
 
"Is it worth investing in AI in 2025?", Alexander Sharko
Fwdays
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
"Scaling in space and time with Temporal", Andriy Lupa .pdf
Fwdays
 
"Provisioning via DOT-Chain: from catering to drone marketplaces", Volodymyr ...
Fwdays
 
" Observability with Elasticsearch: Best Practices for High-Load Platform", A...
Fwdays
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
"Istio Ambient Mesh in production: our way from Sidecar to Sidecar-less",Hlib...
Fwdays
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
"Confidential AI: zero trust concept", Hennadiy Karpov
Fwdays
 
"Choosing Tensor Accelerators for Specific Tasks: Compute vs Memory Bound Mod...
Fwdays
 
Ad

Recently uploaded (20)

PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
July Patch Tuesday
Ivanti
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
July Patch Tuesday
Ivanti
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Biography of Daniel Podor.pdf
Daniel Podor
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Ad

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 [email protected] 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