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
• Others
© 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
Ad

Recommended

Top 10 python ide
Top 10 python ide
Saravanakumar viswanathan
 
Best Practices for implementing Database Security Comprehensive Database Secu...
Best Practices for implementing Database Security Comprehensive Database Secu...
Kal BO
 
BI, Reporting and Analytics on Apache Cassandra
BI, Reporting and Analytics on Apache Cassandra
Victor Coustenoble
 
Neo4j session
Neo4j session
Dr-Dipali Meher
 
Exadata architecture and internals presentation
Exadata architecture and internals presentation
Sanjoy Dasgupta
 
Microsoft Azure Data Factory Data Flow Scenarios
Microsoft Azure Data Factory Data Flow Scenarios
Mark Kromer
 
Tricks every ClickHouse designer should know, by Robert Hodges, Altinity CEO
Tricks every ClickHouse designer should know, by Robert Hodges, Altinity CEO
Altinity Ltd
 
Migrating on premises workload to azure sql database
Migrating on premises workload to azure sql database
PARIKSHIT SAVJANI
 
Demystifying Data Warehouse as a Service
Demystifying Data Warehouse as a Service
Snowflake Computing
 
Cassandra Day NY 2014: Apache Cassandra & Python for the The New York Times ⨍...
Cassandra Day NY 2014: Apache Cassandra & Python for the The New York Times ⨍...
DataStax Academy
 
Less17 moving data
Less17 moving data
Amit Bhalla
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
DataStax Academy
 
Snowflake Overview
Snowflake Overview
Snowflake Computing
 
MySQL Monitoring using Prometheus & Grafana
MySQL Monitoring using Prometheus & Grafana
YoungHeon (Roy) Kim
 
Introducing Scylla Cloud
Introducing Scylla Cloud
ScyllaDB
 
Cloud Monitoring with Prometheus
Cloud Monitoring with Prometheus
QAware GmbH
 
OSA Con 2022 - Apache Iceberg_ An Architectural Look Under the Covers - Alex ...
OSA Con 2022 - Apache Iceberg_ An Architectural Look Under the Covers - Alex ...
Altinity Ltd
 
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
DataStax
 
Achieving Continuous Availability for Your Applications with Oracle MAA
Achieving Continuous Availability for Your Applications with Oracle MAA
Markus Michalewicz
 
Technical Introduction to PostgreSQL and PPAS
Technical Introduction to PostgreSQL and PPAS
Ashnikbiz
 
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
The Hive
 
Hyperspace: An Indexing Subsystem for Apache Spark
Hyperspace: An Indexing Subsystem for Apache Spark
Databricks
 
SOUG Day Oracle 21c New Security Features
SOUG Day Oracle 21c New Security Features
Stefan Oehrli
 
Introducing the Snowflake Computing Cloud Data Warehouse
Introducing the Snowflake Computing Cloud Data Warehouse
Snowflake Computing
 
HDFS Selective Wire Encryption
HDFS Selective Wire Encryption
Konstantin V. Shvachko
 
Indexes and Indexing in Oracle 12c
Indexes and Indexing in Oracle 12c
Oren Nakdimon
 
Presentation implementing oracle asm successfully
Presentation implementing oracle asm successfully
xKinAnx
 
Spark Streaming | Twitter Sentiment Analysis Example | Apache Spark Training ...
Spark Streaming | Twitter Sentiment Analysis Example | Apache Spark Training ...
Edureka!
 
Why are we excited about MySQL 8? / Петр Зайцев (Percona)
Why are we excited about MySQL 8? / Петр Зайцев (Percona)
Ontico
 
[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...
Insight Technology, Inc.
 

More Related Content

What's hot (20)

Demystifying Data Warehouse as a Service
Demystifying Data Warehouse as a Service
Snowflake Computing
 
Cassandra Day NY 2014: Apache Cassandra & Python for the The New York Times ⨍...
Cassandra Day NY 2014: Apache Cassandra & Python for the The New York Times ⨍...
DataStax Academy
 
Less17 moving data
Less17 moving data
Amit Bhalla
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
DataStax Academy
 
Snowflake Overview
Snowflake Overview
Snowflake Computing
 
MySQL Monitoring using Prometheus & Grafana
MySQL Monitoring using Prometheus & Grafana
YoungHeon (Roy) Kim
 
Introducing Scylla Cloud
Introducing Scylla Cloud
ScyllaDB
 
Cloud Monitoring with Prometheus
Cloud Monitoring with Prometheus
QAware GmbH
 
OSA Con 2022 - Apache Iceberg_ An Architectural Look Under the Covers - Alex ...
OSA Con 2022 - Apache Iceberg_ An Architectural Look Under the Covers - Alex ...
Altinity Ltd
 
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
DataStax
 
Achieving Continuous Availability for Your Applications with Oracle MAA
Achieving Continuous Availability for Your Applications with Oracle MAA
Markus Michalewicz
 
Technical Introduction to PostgreSQL and PPAS
Technical Introduction to PostgreSQL and PPAS
Ashnikbiz
 
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
The Hive
 
Hyperspace: An Indexing Subsystem for Apache Spark
Hyperspace: An Indexing Subsystem for Apache Spark
Databricks
 
SOUG Day Oracle 21c New Security Features
SOUG Day Oracle 21c New Security Features
Stefan Oehrli
 
Introducing the Snowflake Computing Cloud Data Warehouse
Introducing the Snowflake Computing Cloud Data Warehouse
Snowflake Computing
 
HDFS Selective Wire Encryption
HDFS Selective Wire Encryption
Konstantin V. Shvachko
 
Indexes and Indexing in Oracle 12c
Indexes and Indexing in Oracle 12c
Oren Nakdimon
 
Presentation implementing oracle asm successfully
Presentation implementing oracle asm successfully
xKinAnx
 
Spark Streaming | Twitter Sentiment Analysis Example | Apache Spark Training ...
Spark Streaming | Twitter Sentiment Analysis Example | Apache Spark Training ...
Edureka!
 
Demystifying Data Warehouse as a Service
Demystifying Data Warehouse as a Service
Snowflake Computing
 
Cassandra Day NY 2014: Apache Cassandra & Python for the The New York Times ⨍...
Cassandra Day NY 2014: Apache Cassandra & Python for the The New York Times ⨍...
DataStax Academy
 
Less17 moving data
Less17 moving data
Amit Bhalla
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
DataStax Academy
 
MySQL Monitoring using Prometheus & Grafana
MySQL Monitoring using Prometheus & Grafana
YoungHeon (Roy) Kim
 
Introducing Scylla Cloud
Introducing Scylla Cloud
ScyllaDB
 
Cloud Monitoring with Prometheus
Cloud Monitoring with Prometheus
QAware GmbH
 
OSA Con 2022 - Apache Iceberg_ An Architectural Look Under the Covers - Alex ...
OSA Con 2022 - Apache Iceberg_ An Architectural Look Under the Covers - Alex ...
Altinity Ltd
 
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
DataStax
 
Achieving Continuous Availability for Your Applications with Oracle MAA
Achieving Continuous Availability for Your Applications with Oracle MAA
Markus Michalewicz
 
Technical Introduction to PostgreSQL and PPAS
Technical Introduction to PostgreSQL and PPAS
Ashnikbiz
 
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
The Hive
 
Hyperspace: An Indexing Subsystem for Apache Spark
Hyperspace: An Indexing Subsystem for Apache Spark
Databricks
 
SOUG Day Oracle 21c New Security Features
SOUG Day Oracle 21c New Security Features
Stefan Oehrli
 
Introducing the Snowflake Computing Cloud Data Warehouse
Introducing the Snowflake Computing Cloud Data Warehouse
Snowflake Computing
 
Indexes and Indexing in Oracle 12c
Indexes and Indexing in Oracle 12c
Oren Nakdimon
 
Presentation implementing oracle asm successfully
Presentation implementing oracle asm successfully
xKinAnx
 
Spark Streaming | Twitter Sentiment Analysis Example | Apache Spark Training ...
Spark Streaming | Twitter Sentiment Analysis Example | Apache Spark Training ...
Edureka!
 

Similar to Mysql query optimization best practices and indexing (20)

Why are we excited about MySQL 8? / Петр Зайцев (Percona)
Why are we excited about MySQL 8? / Петр Зайцев (Percona)
Ontico
 
[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...
Insight Technology, Inc.
 
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
Fwdays
 
Implement DevOps Like a Unicorn—Even If You’re Not One
Implement DevOps Like a Unicorn—Even If You’re Not One
TechWell
 
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
 
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
OracleMySQL
 
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
SolarWinds
 
Novinky v Oracle Database 18c
Novinky v Oracle Database 18c
MarketingArrowECS_CZ
 
Mysql ecosystem in 2018
Mysql ecosystem in 2018
Alkin Tezuysal
 
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
Dakiry
 
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
Altinity Ltd
 
How to upgrade like a boss to my sql 8.0?
How to upgrade like a boss to my sql 8.0?
Alkin Tezuysal
 
Optimizing Open Source for Greater Database Savings & Control
Optimizing Open Source for Greater Database Savings & Control
EDB
 
Beginners guide to_optimizer
Beginners guide to_optimizer
Maria Colgan
 
NoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
NoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
Data Con LA
 
Optimize with Open Source
Optimize with Open Source
EDB
 
Optimizing Open Source for Greater Database Savings and Control
Optimizing Open Source for Greater Database Savings and Control
EDB
 
MySQL Replication — Advanced Features / Петр Зайцев (Percona)
MySQL Replication — Advanced Features / Петр Зайцев (Percona)
Ontico
 
MongoDB Evenings Chicago - Find Your Way in MongoDB 3.2: Compass and Beyond
MongoDB Evenings Chicago - Find Your Way in MongoDB 3.2: Compass and Beyond
MongoDB
 
Webminar - Novedades de MongoDB 3.2
Webminar - Novedades de MongoDB 3.2
Sam_Francis
 
Why are we excited about MySQL 8? / Петр Зайцев (Percona)
Why are we excited about MySQL 8? / Петр Зайцев (Percona)
Ontico
 
[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...
Insight Technology, Inc.
 
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
Роман Новиков "Best Practices for MySQL Performance & Troubleshooting with th...
Fwdays
 
Implement DevOps Like a Unicorn—Even If You’re Not One
Implement DevOps Like a Unicorn—Even If You’re Not One
TechWell
 
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
 
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
OracleMySQL
 
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
SolarWinds
 
Mysql ecosystem in 2018
Mysql ecosystem in 2018
Alkin Tezuysal
 
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
ROMA NOVIKOV, BAQ, "Prometheus + grafana based monitoring"
Dakiry
 
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
Webinar 2017. Supercharge your analytics with ClickHouse. Vadim Tkachenko
Altinity Ltd
 
How to upgrade like a boss to my sql 8.0?
How to upgrade like a boss to my sql 8.0?
Alkin Tezuysal
 
Optimizing Open Source for Greater Database Savings & Control
Optimizing Open Source for Greater Database Savings & Control
EDB
 
Beginners guide to_optimizer
Beginners guide to_optimizer
Maria Colgan
 
NoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
NoSQL on MySQL - MySQL Document Store by Vadim Tkachenko
Data Con LA
 
Optimize with Open Source
Optimize with Open Source
EDB
 
Optimizing Open Source for Greater Database Savings and Control
Optimizing Open Source for Greater Database Savings and Control
EDB
 
MySQL Replication — Advanced Features / Петр Зайцев (Percona)
MySQL Replication — Advanced Features / Петр Зайцев (Percona)
Ontico
 
MongoDB Evenings Chicago - Find Your Way in MongoDB 3.2: Compass and Beyond
MongoDB Evenings Chicago - Find Your Way in MongoDB 3.2: Compass and Beyond
MongoDB
 
Webminar - Novedades de MongoDB 3.2
Webminar - Novedades de MongoDB 3.2
Sam_Francis
 
Ad

More from Alkin Tezuysal (20)

Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Unified Observability - Alkin Tezuysal - FOSSASIA Summit March 2025 .pdf
Unified Observability - Alkin Tezuysal - FOSSASIA Summit March 2025 .pdf
Alkin Tezuysal
 
Boosting MySQL with Vector Search Scale22X 2025.pdf
Boosting MySQL with Vector Search Scale22X 2025.pdf
Alkin Tezuysal
 
Boosting MySQL with Vector Search Fosdem 2025.pdf
Boosting MySQL with Vector Search Fosdem 2025.pdf
Alkin Tezuysal
 
London MySQL Day - Lightning Talk Dec 2024.pdf
London MySQL Day - Lightning Talk Dec 2024.pdf
Alkin Tezuysal
 
Design and Modeling with MySQL and PostgreSQL - Percona University Istanbul S...
Design and Modeling with MySQL and PostgreSQL - Percona University Istanbul S...
Alkin Tezuysal
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Alkin Tezuysal
 
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Alkin Tezuysal
 
FOSSASIA - MySQL Cookbook 4e Journey APR 2023.pdf
FOSSASIA - MySQL Cookbook 4e Journey APR 2023.pdf
Alkin Tezuysal
 
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
Alkin Tezuysal
 
How OLTP to OLAP Archival Demystified
How OLTP to OLAP Archival Demystified
Alkin Tezuysal
 
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
Alkin Tezuysal
 
My first 90 days with ClickHouse.pdf
My first 90 days with ClickHouse.pdf
Alkin Tezuysal
 
KubeCon_NA_2021
KubeCon_NA_2021
Alkin Tezuysal
 
Integrating best of breed open source tools to vitess orchestrator pleu21
Integrating best of breed open source tools to vitess orchestrator pleu21
Alkin Tezuysal
 
Vitess: Scalable Database Architecture - Kubernetes Community Days Africa Ap...
Vitess: Scalable Database Architecture - Kubernetes Community Days Africa Ap...
Alkin Tezuysal
 
How to shard MariaDB like a pro - FOSDEM 2021
How to shard MariaDB like a pro - FOSDEM 2021
Alkin Tezuysal
 
Vitess - Data on Kubernetes
Vitess - Data on Kubernetes
Alkin Tezuysal
 
MySQL Ecosystem in 2020
MySQL Ecosystem in 2020
Alkin Tezuysal
 
Introduction to Vitess on Kubernetes for MySQL - Webinar
Introduction to Vitess on Kubernetes for MySQL - Webinar
Alkin Tezuysal
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Unified Observability - Alkin Tezuysal - FOSSASIA Summit March 2025 .pdf
Unified Observability - Alkin Tezuysal - FOSSASIA Summit March 2025 .pdf
Alkin Tezuysal
 
Boosting MySQL with Vector Search Scale22X 2025.pdf
Boosting MySQL with Vector Search Scale22X 2025.pdf
Alkin Tezuysal
 
Boosting MySQL with Vector Search Fosdem 2025.pdf
Boosting MySQL with Vector Search Fosdem 2025.pdf
Alkin Tezuysal
 
London MySQL Day - Lightning Talk Dec 2024.pdf
London MySQL Day - Lightning Talk Dec 2024.pdf
Alkin Tezuysal
 
Design and Modeling with MySQL and PostgreSQL - Percona University Istanbul S...
Design and Modeling with MySQL and PostgreSQL - Percona University Istanbul S...
Alkin Tezuysal
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Alkin Tezuysal
 
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Alkin Tezuysal
 
FOSSASIA - MySQL Cookbook 4e Journey APR 2023.pdf
FOSSASIA - MySQL Cookbook 4e Journey APR 2023.pdf
Alkin Tezuysal
 
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
Alkin Tezuysal
 
How OLTP to OLAP Archival Demystified
How OLTP to OLAP Archival Demystified
Alkin Tezuysal
 
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
Alkin Tezuysal
 
My first 90 days with ClickHouse.pdf
My first 90 days with ClickHouse.pdf
Alkin Tezuysal
 
Integrating best of breed open source tools to vitess orchestrator pleu21
Integrating best of breed open source tools to vitess orchestrator pleu21
Alkin Tezuysal
 
Vitess: Scalable Database Architecture - Kubernetes Community Days Africa Ap...
Vitess: Scalable Database Architecture - Kubernetes Community Days Africa Ap...
Alkin Tezuysal
 
How to shard MariaDB like a pro - FOSDEM 2021
How to shard MariaDB like a pro - FOSDEM 2021
Alkin Tezuysal
 
Vitess - Data on Kubernetes
Vitess - Data on Kubernetes
Alkin Tezuysal
 
MySQL Ecosystem in 2020
MySQL Ecosystem in 2020
Alkin Tezuysal
 
Introduction to Vitess on Kubernetes for MySQL - Webinar
Introduction to Vitess on Kubernetes for MySQL - Webinar
Alkin Tezuysal
 
Ad

Recently uploaded (20)

Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 

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 • Others
  • 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