Movatterモバイル変換


[0]ホーム

URL:


Alkin Tezuysal, profile picture
Uploaded byAlkin Tezuysal
PPTX, PDF724 views

Mysql query optimization best practices and indexing

The document outlines best practices for MySQL query optimization and indexing, emphasizing the importance of indexes for efficient data access. It discusses techniques for identifying slow queries, optimizing query execution plans, and the various types of indexes available in MySQL. Additionally, it provides tips on query structure and execution strategies to enhance performance while maintaining optimal index usage.

Embed presentation

Downloaded 29 times
© 2017 Percona1MySQL Query Optimization BestPracticesand IndexingAlkin Tezuysal – Sr. Technical ManagerPercona
© 2017 Percona2Who am I? @ask_dba
© 2017 Percona3About PerconaSolutions for your success with MySQL , MongoDB and PostgreSQLSupport, Managed Services, SoftwareOur Software is 100% Open SourceSupport Broad Ecosystem – MySQL, MariaDB, Amazon RDSIn Business for 12 yearsMore than 3000 customers, including top Internet companies and enterprises
© 2017 Percona4About This PresentationIndexing BasicsFinding andIdentifyingSlow QueriesUtilizing ExplainPlanAdvancedIndexingTooling andmore
© 2017 Percona5Indexing Basics• What it does?• Increase speed of given lookup (SQL)• Access and maintain changes• Helps Optimizer to reach its goal
© 2017 Percona6Why do we need indexes?• Data persists on disks• Disks cheap but slow• Data can be in memory• Memory fast but expensiveIndex is the answer to access data fast.CREATE INDEX part_of_name ON customer (name(10));
© 2017 Percona7Traversal1. Tree Traversal2. Follow leaf node chain3. Fetch the table data
© 2017 Percona8Leaf Nodes1. Establish doubly linked list2. Connect index leaf nodes3. Indexed columns
© 2017 Percona9B-tree Structure
© 2017 Percona10Slow 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 Percona11The Optimizer
© 2017 Percona12MySQL Optimizer• Cost based• Assign costs to select operations• Assign costs to partial or alternate plans• Seek for lowest costAccess Method Join Order Subquery Strategy
© 2017 Percona13Cost Model
© 2017 Percona14Finding and Identifying Slow Queries• Slow Query Log• PMM/QAN• Network sniff• Others (Licensed)• MySQL EM• Vividcortex• Solarwinds• Monyog• Others
© 2017 Percona15Slow 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 Percona16PMM/QAN
© 2017 Percona17PMM/QAN
© 2017 Percona18PMM/QAN
© 2017 Percona19PMM Demo - https://pmmdemo.percona.com/
© 2017 Percona20Explain Plan
© 2017 Percona21Explain 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 Percona22Explain 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 Percona23Cost Based Access Method1. Find the optimal method2. Check if access method useful3. Estimate the cost of using access method4. Select low cost access method
© 2017 Percona24Query ExecutionTable ScanIndex ScanIndexLookupRangeScanIndexMergeLooseIndex Scan
© 2017 Percona25Indexing Best Practices• Always have Primary Key• Physical order of table, if not created explicitly, MySQL will createhidden one (Global Mutex)• Fastest lookup is PK
© 2017 Percona26Indexing 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 Percona27Indexing Best Practices• Equality first, range next• Ex:select first_name, last_name, birth_date fromemployeeswhere date_of_birth => to_date (?, `YYYY-MM-DD`)and date_of_birth <= to_date (?, `YYYY-MM-DD`)and branch_id = ?
© 2017 Percona28Indexing Best Practices• One index scan is faster than two• Avoid duplicate indexes pt-duplicate-key-checker
© 2017 Percona29Indexing Best Practices• Data types matter. Numeric for numbers.• Ex:select …from …where numeric_value = `48`
© 2017 Percona30Query Optimization Best Practices• Negative clauses and subqueries aren’t as good as positiveclauses• Ex:• IS NOT• IS NOT NULL• NOT IN• NOT LIKE
© 2017 Percona31Query Optimization Best Practices• User INNER instead of LEFT where you can
© 2017 Percona32Query Optimization Best Practices• UNION ALL is better than UNIONUNIONUNION ALL
© 2017 Percona33Query Optimization Best Practices• ORDER BY can be expensiveSELECT * FROM t1ORDER BY idx_c1, idx_c2;• Avoid while sorting small set of data (Use code)cust_idfirst_namelast_nameemail1 Billy Joel bb7@bluenot.com2 Jane Fondajf1950@yahoo.com3 Mark WeltonmarkW1912@gmail.com4 Linda Joey linda.joey@yandex.com5 Sidney Travorsidney.travor@icloud.com6 Jordan Velez jordanv@amazon.com
© 2017 Percona34Query 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 LIMIT10;SELECT col1, ... FROM t1 ... ORDER BY RAND()LIMIT 15;
© 2017 Percona35Query 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 LIMIT10;SELECT col1, ... FROM t1 ... ORDER BY RAND()LIMIT 15;
© 2017 Percona36MySQL Index Types• B-tree (Common)• Fractal Tree• LSM Tree• R-Tree (Spatial)• Hash (Memory)• Engine-dependent
© 2017 Percona37Advanced 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 FORORDER BY (i2) ORDER BY a;
© 2017 Percona38If indexes not enoughQuery 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 Percona39Advanced Queries with ProxySQL – Query rewriteengine• Most wanted feature by DBAs• Rewrite queries overloading the database on the fly.Application AProxySQL• Simply buy time until application can be modifiedApplication BMySQLMasterMySQLSlaveMySQLSlaveMySQLSlaveQueryRewritingMySQLSlaveMySQLSlave
© 2017 Percona40Final ThoughtsOptimizer is not smart as DBAs• Help to choose best possible path• Improve throughputAdd only indexes you need• Avoid duplicate indexing• Avoid overhead disk space, extra i/o opsStay on current version of MySQL• Several bugs fixed• Optimizer and Engine improvements in place
© 2017 Percona41References and Credits• Markus Winand (2018) - SQL Performance Explained (2018)• Otstein Grovlen (2017- How to Analyze and Tune MySQL Queries forBetter Performance• Sveta Smirnova (2018) – Introduction into MySQL Query Tuning• Oracle Reference Manual• Jeremy Cole (2013) - How does InnoDB behave without a PrimaryKey?• Tata McDaniel (2018) - Visualize This! MySQL Tools That ExplainQueries• Reviewers: Daniel G Burgos, Tate McDaniel, Janos Ruszo
DATABASE PERFORMANCEMATTERSDatabase Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance Matters

Recommended

PDF
MLflow: Infrastructure for a Complete Machine Learning Life Cycle
PDF
Databricks Overview for MLOps
PPT
Chuong 2 - CSDL phân tán
 
PDF
Apache Arrow Flight: A New Gold Standard for Data Transport
PDF
Building an ML Platform with Ray and MLflow
PDF
(Presentation)NLP Pretraining models based on deeplearning -BERT, GPT, and BART
PPTX
Giới thiệu và triển khai private cloud
PPTX
Dash plotly data visualization
PPTX
Presto: SQL-on-anything
PPTX
Tìm hiểu về mongodb
PDF
Bài giảng chuyên đề - Lê Minh Hoàng
PPTX
Introduction to NoSQL
PDF
Machine learning workshop
PDF
MLflow: A Platform for Production Machine Learning
PDF
Graph Database Meetup in Korea #4. 그래프 이론을 적용한 그래프 데이터베이스 활용 사례
PDF
FixMatch:simplifying semi supervised learning with consistency and confidence
PPTX
Intro to elasticsearch
PDF
2.1.1.5 Packet Tracer - Create a Simple Network Using Packet Tracer (3).pdf
PDF
Đề tài: Triển khai DHCP Server trên mô hình mạng ba lớp, HOT
PDF
Faster, better, stronger: The new InnoDB
PPTX
Mapping Data Flows Training April 2021
PPTX
Bao cao thuc tap - Điện toán đám mây
PDF
[Đồ án môn học] - Đề tài: Tìm hiểu Git và Github
PPTX
Few shot learning/ one shot learning/ machine learning
PDF
Simplify Data Conversion from Spark to TensorFlow and PyTorch
PDF
Xây dựng chatbot bán hàng dựa trên mô hình sinh luận văn thạc sĩ công nghệ th...
PDF
Bayesian Model-Agnostic Meta-Learning
PPT
TechTalk #13 Grokking: Marrying Elasticsearch with NLP to solve real-world se...
PPTX
Alkin Tezuysal "MySQL Query Optimization Best Practices and Indexing"
 
PDF
Query Optimization with MySQL 5.6: Old and New Tricks

More Related Content

PDF
MLflow: Infrastructure for a Complete Machine Learning Life Cycle
PDF
Databricks Overview for MLOps
PPT
Chuong 2 - CSDL phân tán
 
PDF
Apache Arrow Flight: A New Gold Standard for Data Transport
PDF
Building an ML Platform with Ray and MLflow
PDF
(Presentation)NLP Pretraining models based on deeplearning -BERT, GPT, and BART
PPTX
Giới thiệu và triển khai private cloud
PPTX
Dash plotly data visualization
MLflow: Infrastructure for a Complete Machine Learning Life Cycle
Databricks Overview for MLOps
Chuong 2 - CSDL phân tán
 
Apache Arrow Flight: A New Gold Standard for Data Transport
Building an ML Platform with Ray and MLflow
(Presentation)NLP Pretraining models based on deeplearning -BERT, GPT, and BART
Giới thiệu và triển khai private cloud
Dash plotly data visualization

What's hot

PPTX
Presto: SQL-on-anything
PPTX
Tìm hiểu về mongodb
PDF
Bài giảng chuyên đề - Lê Minh Hoàng
PPTX
Introduction to NoSQL
PDF
Machine learning workshop
PDF
MLflow: A Platform for Production Machine Learning
PDF
Graph Database Meetup in Korea #4. 그래프 이론을 적용한 그래프 데이터베이스 활용 사례
PDF
FixMatch:simplifying semi supervised learning with consistency and confidence
PPTX
Intro to elasticsearch
PDF
2.1.1.5 Packet Tracer - Create a Simple Network Using Packet Tracer (3).pdf
PDF
Đề tài: Triển khai DHCP Server trên mô hình mạng ba lớp, HOT
PDF
Faster, better, stronger: The new InnoDB
PPTX
Mapping Data Flows Training April 2021
PPTX
Bao cao thuc tap - Điện toán đám mây
PDF
[Đồ án môn học] - Đề tài: Tìm hiểu Git và Github
PPTX
Few shot learning/ one shot learning/ machine learning
PDF
Simplify Data Conversion from Spark to TensorFlow and PyTorch
PDF
Xây dựng chatbot bán hàng dựa trên mô hình sinh luận văn thạc sĩ công nghệ th...
PDF
Bayesian Model-Agnostic Meta-Learning
PPT
TechTalk #13 Grokking: Marrying Elasticsearch with NLP to solve real-world se...
Presto: SQL-on-anything
Tìm hiểu về mongodb
Bài giảng chuyên đề - Lê Minh Hoàng
Introduction to NoSQL
Machine learning workshop
MLflow: A Platform for Production Machine Learning
Graph Database Meetup in Korea #4. 그래프 이론을 적용한 그래프 데이터베이스 활용 사례
FixMatch:simplifying semi supervised learning with consistency and confidence
Intro to elasticsearch
2.1.1.5 Packet Tracer - Create a Simple Network Using Packet Tracer (3).pdf
Đề tài: Triển khai DHCP Server trên mô hình mạng ba lớp, HOT
Faster, better, stronger: The new InnoDB
Mapping Data Flows Training April 2021
Bao cao thuc tap - Điện toán đám mây
[Đồ án môn học] - Đề tài: Tìm hiểu Git và Github
Few shot learning/ one shot learning/ machine learning
Simplify Data Conversion from Spark to TensorFlow and PyTorch
Xây dựng chatbot bán hàng dựa trên mô hình sinh luận văn thạc sĩ công nghệ th...
Bayesian Model-Agnostic Meta-Learning
TechTalk #13 Grokking: Marrying Elasticsearch with NLP to solve real-world se...

Similar to Mysql query optimization best practices and indexing

PPTX
Alkin Tezuysal "MySQL Query Optimization Best Practices and Indexing"
 
PDF
Query Optimization with MySQL 5.6: Old and New Tricks
PDF
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
PDF
Percona Live 2012PPT: MySQL Query optimization
PPTX
MySQL Indexing - Best practices for MySQL 5.6
PPTX
Работа с индексами - лучшие практики для MySQL 5.6, Петр Зайцев (Percona)
 
PDF
Introduction into MySQL Query Tuning for Dev[Op]s
PDF
How to Design Indexes, Really
PDF
Troubleshooting MySQL Performance
PDF
Webinar 2013 advanced_query_tuning
PDF
Mysql query optimization
PDF
query optimization
PDF
MySQL Query Optimisation 101
PPTX
MySQL performance tuning
PDF
Introduction into MySQL Query Tuning
ODP
Beyond php - it's not (just) about the code
PDF
Scaling MySQL Strategies for Developers
PDF
Zurich2007 MySQL Query Optimization
PDF
Zurich2007 MySQL Query Optimization
PPTX
Tunning sql query
Alkin Tezuysal "MySQL Query Optimization Best Practices and Indexing"
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Percona Live 2012PPT: MySQL Query optimization
MySQL Indexing - Best practices for MySQL 5.6
Работа с индексами - лучшие практики для MySQL 5.6, Петр Зайцев (Percona)
 
Introduction into MySQL Query Tuning for Dev[Op]s
How to Design Indexes, Really
Troubleshooting MySQL Performance
Webinar 2013 advanced_query_tuning
Mysql query optimization
query optimization
MySQL Query Optimisation 101
MySQL performance tuning
Introduction into MySQL Query Tuning
Beyond php - it's not (just) about the code
Scaling MySQL Strategies for Developers
Zurich2007 MySQL Query Optimization
Zurich2007 MySQL Query Optimization
Tunning sql query

More from Alkin Tezuysal

PDF
Running Non-Cloud-Native Databases in Cloud-Native Environments_ Challenges a...
PDF
AI-Powered Alert Analysis with ClickHouse Cloud Databases.pdf
PDF
From Source Code to Source of Truth_ 30 Years of Open Source Database Evolution
PDF
Unified Observability - Alkin Tezuysal - OpenTechDay Summit September 2025 F...
PDF
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
PDF
Unified Observability - Alkin Tezuysal - FOSSASIA Summit March 2025 .pdf
PDF
Boosting MySQL with Vector Search Scale22X 2025.pdf
PDF
Boosting MySQL with Vector Search Fosdem 2025.pdf
PDF
London MySQL Day - Lightning Talk Dec 2024.pdf
PDF
Design and Modeling with MySQL and PostgreSQL - Percona University Istanbul S...
PDF
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
PPTX
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
PDF
FOSSASIA - MySQL Cookbook 4e Journey APR 2023.pdf
PDF
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
PDF
How OLTP to OLAP Archival Demystified
PDF
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
PDF
My first 90 days with ClickHouse.pdf
PDF
KubeCon_NA_2021
PDF
Integrating best of breed open source tools to vitess orchestrator pleu21
PDF
Vitess: Scalable Database Architecture - Kubernetes Community Days Africa Ap...
Running Non-Cloud-Native Databases in Cloud-Native Environments_ Challenges a...
AI-Powered Alert Analysis with ClickHouse Cloud Databases.pdf
From Source Code to Source of Truth_ 30 Years of Open Source Database Evolution
Unified Observability - Alkin Tezuysal - OpenTechDay Summit September 2025 F...
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Unified Observability - Alkin Tezuysal - FOSSASIA Summit March 2025 .pdf
Boosting MySQL with Vector Search Scale22X 2025.pdf
Boosting MySQL with Vector Search Fosdem 2025.pdf
London MySQL Day - Lightning Talk Dec 2024.pdf
Design and Modeling with MySQL and PostgreSQL - Percona University Istanbul S...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
FOSSASIA - MySQL Cookbook 4e Journey APR 2023.pdf
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
How OLTP to OLAP Archival Demystified
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
My first 90 days with ClickHouse.pdf
KubeCon_NA_2021
Integrating best of breed open source tools to vitess orchestrator pleu21
Vitess: Scalable Database Architecture - Kubernetes Community Days Africa Ap...

Recently uploaded

PDF
Energy Storage Landscape Clean Energy Ministerial
PPTX
Cybercrime in the Digital Age: Risks, Impact & Protection
PDF
TrustArc Webinar - Looking Ahead: The 2026 Privacy Landscape
PPTX
Cloud-and-AI-Platform-FY26-Partner-Playbook.pptx
PDF
Security Technologys: Access Control, Firewall, VPN
PPTX
From Backup to Resilience: How MSPs Are Preparing for 2026
 
PPTX
Chapter 3 Introduction to number system.pptx
DOCX
iRobot Post‑Mortem and Alternative Paths - Discussion Document for Boards and...
PPTX
Kanban India 2025 | Daksh Gupta | Modeling the Models, Generative AI & Kanban
PPTX
Data Privacy and Protection: Safeguarding Information in a Connected World
PDF
Session 1 - Solving Semi-Structured Documents with Document Understanding
PPTX
AI's Impact on Cybersecurity - Challenges and Opportunities
PDF
Eredità digitale sugli smartphone: cosa resta di noi nei dispositivi mobili
PPTX
Software Analysis &Design ethiopia chap-2.pptx
DOCX
Introduction to the World of Computers (Hardware & Software)
PPTX
cybercrime in Information security .pptx
PPTX
Conversational Agents – Building Intelligent Assistants [Virtual Hands-on Wor...
PDF
Unser Jahresrückblick – MarvelClient in 2025
PPTX
THIS IS CYBER SECURITY NOTES USED IN CLASS ON VARIOUS TOPICS USED IN CYBERSEC...
PPT
software-security-intro in information security.ppt
Energy Storage Landscape Clean Energy Ministerial
Cybercrime in the Digital Age: Risks, Impact & Protection
TrustArc Webinar - Looking Ahead: The 2026 Privacy Landscape
Cloud-and-AI-Platform-FY26-Partner-Playbook.pptx
Security Technologys: Access Control, Firewall, VPN
From Backup to Resilience: How MSPs Are Preparing for 2026
 
Chapter 3 Introduction to number system.pptx
iRobot Post‑Mortem and Alternative Paths - Discussion Document for Boards and...
Kanban India 2025 | Daksh Gupta | Modeling the Models, Generative AI & Kanban
Data Privacy and Protection: Safeguarding Information in a Connected World
Session 1 - Solving Semi-Structured Documents with Document Understanding
AI's Impact on Cybersecurity - Challenges and Opportunities
Eredità digitale sugli smartphone: cosa resta di noi nei dispositivi mobili
Software Analysis &Design ethiopia chap-2.pptx
Introduction to the World of Computers (Hardware & Software)
cybercrime in Information security .pptx
Conversational Agents – Building Intelligent Assistants [Virtual Hands-on Wor...
Unser Jahresrückblick – MarvelClient in 2025
THIS IS CYBER SECURITY NOTES USED IN CLASS ON VARIOUS TOPICS USED IN CYBERSEC...
software-security-intro in information security.ppt

Mysql query optimization best practices and indexing

  • 1.
    © 2017 Percona1MySQLQuery Optimization BestPracticesand IndexingAlkin Tezuysal – Sr. Technical ManagerPercona
  • 2.
    © 2017 Percona2Whoam I? @ask_dba
  • 3.
    © 2017 Percona3AboutPerconaSolutions for your success with MySQL , MongoDB and PostgreSQLSupport, Managed Services, SoftwareOur Software is 100% Open SourceSupport Broad Ecosystem – MySQL, MariaDB, Amazon RDSIn Business for 12 yearsMore than 3000 customers, including top Internet companies and enterprises
  • 4.
    © 2017 Percona4AboutThis PresentationIndexing BasicsFinding andIdentifyingSlow QueriesUtilizing ExplainPlanAdvancedIndexingTooling andmore
  • 5.
    © 2017 Percona5IndexingBasics• What it does?• Increase speed of given lookup (SQL)• Access and maintain changes• Helps Optimizer to reach its goal
  • 6.
    © 2017 Percona6Whydo we need indexes?• Data persists on disks• Disks cheap but slow• Data can be in memory• Memory fast but expensiveIndex is the answer to access data fast.CREATE INDEX part_of_name ON customer (name(10));
  • 7.
    © 2017 Percona7Traversal1.Tree Traversal2. Follow leaf node chain3. Fetch the table data
  • 8.
    © 2017 Percona8LeafNodes1. Establish doubly linked list2. Connect index leaf nodes3. Indexed columns
  • 9.
  • 10.
    © 2017 Percona10SlowIndex 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
  • 11.
  • 12.
    © 2017 Percona12MySQLOptimizer• Cost based• Assign costs to select operations• Assign costs to partial or alternate plans• Seek for lowest costAccess Method Join Order Subquery Strategy
  • 13.
  • 14.
    © 2017 Percona14Findingand Identifying Slow Queries• Slow Query Log• PMM/QAN• Network sniff• Others (Licensed)• MySQL EM• Vividcortex• Solarwinds• Monyog• Others
  • 15.
    © 2017 Percona15SlowQuery 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%’
  • 16.
  • 17.
  • 18.
  • 19.
    © 2017 Percona19PMMDemo - https://pmmdemo.percona.com/
  • 20.
  • 21.
    © 2017 Percona21ExplainPlan (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 Percona22ExplainPlan (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 Percona23CostBased Access Method1. Find the optimal method2. Check if access method useful3. Estimate the cost of using access method4. Select low cost access method
  • 24.
    © 2017 Percona24QueryExecutionTable ScanIndex ScanIndexLookupRangeScanIndexMergeLooseIndex Scan
  • 25.
    © 2017 Percona25IndexingBest Practices• Always have Primary Key• Physical order of table, if not created explicitly, MySQL will createhidden one (Global Mutex)• Fastest lookup is PK
  • 26.
    © 2017 Percona26IndexingBest 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 Percona27IndexingBest Practices• Equality first, range next• Ex:select first_name, last_name, birth_date fromemployeeswhere date_of_birth => to_date (?, `YYYY-MM-DD`)and date_of_birth <= to_date (?, `YYYY-MM-DD`)and branch_id = ?
  • 28.
    © 2017 Percona28IndexingBest Practices• One index scan is faster than two• Avoid duplicate indexes pt-duplicate-key-checker
  • 29.
    © 2017 Percona29IndexingBest Practices• Data types matter. Numeric for numbers.• Ex:select …from …where numeric_value = `48`
  • 30.
    © 2017 Percona30QueryOptimization Best Practices• Negative clauses and subqueries aren’t as good as positiveclauses• Ex:• IS NOT• IS NOT NULL• NOT IN• NOT LIKE
  • 31.
    © 2017 Percona31QueryOptimization Best Practices• User INNER instead of LEFT where you can
  • 32.
    © 2017 Percona32QueryOptimization Best Practices• UNION ALL is better than UNIONUNIONUNION ALL
  • 33.
    © 2017 Percona33QueryOptimization Best Practices• ORDER BY can be expensiveSELECT * FROM t1ORDER BY idx_c1, idx_c2;• Avoid while sorting small set of data (Use code)cust_idfirst_namelast_nameemail1 Billy Joel bb7@bluenot.com2 Jane Fondajf1950@yahoo.com3 Mark WeltonmarkW1912@gmail.com4 Linda Joey linda.joey@yandex.com5 Sidney Travorsidney.travor@icloud.com6 Jordan Velez jordanv@amazon.com
  • 34.
    © 2017 Percona34QueryOptimization 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 LIMIT10;SELECT col1, ... FROM t1 ... ORDER BY RAND()LIMIT 15;
  • 35.
    © 2017 Percona35QueryOptimization 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 LIMIT10;SELECT col1, ... FROM t1 ... ORDER BY RAND()LIMIT 15;
  • 36.
    © 2017 Percona36MySQLIndex Types• B-tree (Common)• Fractal Tree• LSM Tree• R-Tree (Spatial)• Hash (Memory)• Engine-dependent
  • 37.
    © 2017 Percona37AdvancedIndexing• 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 FORORDER BY (i2) ORDER BY a;
  • 38.
    © 2017 Percona38Ifindexes not enoughQuery 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 Percona39AdvancedQueries with ProxySQL – Query rewriteengine• Most wanted feature by DBAs• Rewrite queries overloading the database on the fly.Application AProxySQL• Simply buy time until application can be modifiedApplication BMySQLMasterMySQLSlaveMySQLSlaveMySQLSlaveQueryRewritingMySQLSlaveMySQLSlave
  • 40.
    © 2017 Percona40FinalThoughtsOptimizer is not smart as DBAs• Help to choose best possible path• Improve throughputAdd only indexes you need• Avoid duplicate indexing• Avoid overhead disk space, extra i/o opsStay on current version of MySQL• Several bugs fixed• Optimizer and Engine improvements in place
  • 41.
    © 2017 Percona41Referencesand Credits• Markus Winand (2018) - SQL Performance Explained (2018)• Otstein Grovlen (2017- How to Analyze and Tune MySQL Queries forBetter Performance• Sveta Smirnova (2018) – Introduction into MySQL Query Tuning• Oracle Reference Manual• Jeremy Cole (2013) - How does InnoDB behave without a PrimaryKey?• Tata McDaniel (2018) - Visualize This! MySQL Tools That ExplainQueries• Reviewers: Daniel G Burgos, Tate McDaniel, Janos Ruszo
  • 42.
    DATABASE PERFORMANCEMATTERSDatabase PerformanceMattersDatabase Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance Matters

[8]ページ先頭

©2009-2025 Movatter.jp