Movatterモバイル変換


[0]ホーム

URL:


Federico Razzoli, profile picture
Uploaded byFederico Razzoli
870 views

MySQL Query Optimisation 101

This document provides an overview of MySQL query optimization, highlighting the importance of databases, execution plans, and monitoring strategies for identifying impactful queries. It covers techniques for optimizing queries, such as using indexes effectively, understanding the costs of slowness, and the relationship between query performance and locking. Additionally, it elaborates on different query types, index usage rules, and performance considerations to improve database efficiency.

Related topics:

Embed presentation

Downloaded 28 times
MySQLQuery Optimisation101
€ whoami● Federico Razzoli● Freelance consultant● Writing SQL since MySQL 2.23hello@federico-razzoli.com● I love open source, sharing,Collaboration, win-win, etc● I love MariaDB, MySQL, Postgres, etc○ Even Db2, somehow
Why is the database important?
Remember the Von Neumann machine?
It’s always about Data● Since then, the purpose of hardware and software never changed:○ Receive data○ Process data○ Output data
A rose by any other name...● Feel free to use synonyms○ Validate○ Sanitise○ Parse○ Persist○ Normalise / Denormalise○ Cache○ Map / Reduce○ Print○ Ping○ ...
...would smell as sweet● The database of known stars is not a ping package● You use a DBMS to abstract data management as much as possible○ Persistence○ Consistence○ Queries○ Fast search○ …● That’s why “database is magic”
Busy● But it’s just a very busy person, performing many tasks concurrently● And each is:○ Important (must be reliable)○ Complex (must be fast)○ Expected (if something goes wrong,you will complain)In practice, the DBMS is usually the bottleneck.
TerminologyStatement: An SQL commandQuery: A statement that returns a resultset...or any other statement :)Resultset: output 0 or more rowsOptimiser / Query Planner: Component responsible of deciding a query’sexecution planOptimised query: A query whose execution plan is reasonably good...this doesn’t imply in any way that the query is fastDatabase: set of tables (schema)Instance / Server: running MySQL daemon (cluster)
Performance
When should a query be optimised?mysql> EXPLAIN SELECT * FROM t WHERE c < 10 G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tpartitions: NULLtype: ALLpossible_keys: idx_ckey: idx_ckey_len: 4ref: NULLrows: 213030filtered: 50.00Extra: Using where1 row in set, 1 warning (0.01 sec)
When should a query be optimised?mysql> SELECT * FROM performance_schema.events_statements_summary_by_digestWHERE DIGEST ='254a65744e661e072103b7a7630dee1c3a3b8e906f19889f7c796aebe7cdd4f8' G*************************** 1. row ***************************SCHEMA_NAME: testDIGEST: 254a65744e661e072103b7a7630dee1c3a3b8e906f19889f7c796aebe7cdd4f8DIGEST_TEXT: SELECT * FROM `t` WHERE `c` < ?COUNT_STAR: 1...SUM_ROWS_AFFECTED: 0SUM_ROWS_SENT: 57344SUM_ROWS_EXAMINED: 212992SUM_CREATED_TMP_DISK_TABLES: 0SUM_CREATED_TMP_TABLES: 0SUM_SELECT_FULL_JOIN: 0SUM_SELECT_FULL_RANGE_JOIN: 0SUM_SELECT_RANGE: 0SUM_SELECT_RANGE_CHECK: 0SUM_SELECT_SCAN: 1SUM_SORT_MERGE_PASSES: 0SUM_SORT_RANGE: 0SUM_SORT_ROWS: 0SUM_SORT_SCAN: 0SUM_NO_INDEX_USED: 1SUM_NO_GOOD_INDEX_USED: 0FIRST_SEEN: 2019-05-14 00:31:24.078967LAST_SEEN: 2019-05-14 00:31:24.078967...QUERY_SAMPLE_TEXT: SELECT * FROM t WHERE c < 10QUERY_SAMPLE_SEEN: 2019-05-14 00:31:24.078967QUERY_SAMPLE_TIMER_WAIT: 117493874000
But how do I find impacting queries?● It depends what you mean by “impacting”● There are several monitoring methods (USE, etc)● But 3 philosophies:
But how do I find impacting queries?● It depends what you mean by “impacting”● There are several monitoring methods (USE, etc)● But 3 philosophies:○ Panicking when you hear that something is down or slow
But how do I find impacting queries?● It depends what you mean by “impacting”● There are several monitoring methods (USE, etc)● But 3 philosophies:○ Panicking when you hear that something is down or slow○ System-centric monitoring
But how do I find impacting queries?● It depends what you mean by “impacting”● There are several monitoring methods (USE, etc)● But 3 philosophies:○ Panicking when you hear that something is down or slow○ System-centric monitoring○ User-centric
But how do I find impacting queries?● Panicking when you hear that something is down or slow● System-centric monitoring● User-centricYou can use them all.
Panicking● Simplest method● Do nothing do prevent anything○ Optionally, take a lot of actions to prevent imaginary problems inimaginary ways○ There is no evidence that your job is useless, so your boss will not fireyou
System-centric● pt-query-digest, PMM, etc● Merge queries into one, normalising its text and replacing parameters○ SELECT * FROM t WHERE b= 111 AND a = 0 -- comment○ Select * From t Where a = 24 and b=42;○ SELECT * FROM t WHERE a = ? AND b = ?● Sum execution time of each occurrence (Grand Total Time)● Optimise the queries with highest GTT
User-Centric● Calculate the cost of slowness (users don’t buy, maybe leave 4ever)● Cost of slowness is different for different○ URLs○ Number of users○ ...other variables that depend on your business■ (day of month, country, etc)● Set Service Level Objectives● Monitor the HTTP calls latency, and the involved services● Find out what’s slowing them down
Query Performance
What makes a query “important”?● How many times it’s executed● It’s locking
What makes a query slow?● Number of rows read○ Read != return○ Indexes are there to lower the number of reads● Number of rows written○ In-memory temp tables are not good○ On-disk temp tables are worse
How do I optimise a query?● Use indexes properly● Avoid creation of temp tables, if possible
What is an index?
Index Types● BTREE - ordered data structure● HASH - hash table● PostgreSQL has much more● Each storage engine can implement any of both● InnoDB uses BTREE and internally uses HASH when it thinks it’s better● The syntax CREATE INDEX USING [BTREE | HASH] is generally uselessWe will focus on BTREE indexes in InnoDB
Index Properties● Primary key: unique values, not null● UNIQUE● Multiple columns○ The order matters● Column prefix (only for strings)
InnoDB Indexes● InnoDB tables should always have a Primary Key● The table is stored ordered by primary key● The table itself is the primary key○ Columns “not part of the primary key” simply don’t affect the order ofrows
InnoDB IndexesTable columns: {a, b, c}Primary key: {a, b}A B C1 1 41 2 12 1 92 2 33 0 34 20 0
InnoDB Indexes● Secondary indexes are stored separately● They are ordered by the indexed column● Each entry contain a reference to a primary key entry
InnoDB IndexesPrimary key: {a, b}Index idx_c: {c}c a b0 4 201 1 23 2 23 3 04 1 19 2 1
Which queries will be faster?Table columns: {a, b, c, d, e}Primary key: {a, b}Index idx_c: {c}● SELECT * FROM t WHERE a = 1● SELECT * FROM t WHERE a = 1 AND b = 2● SELECT a, b FROM t WHERE c = 0● SELECT d, e FROM t WHERE c = 0
More performance considerations?
More performance considerations?● Big primary key = big indexes● Primary key should be append-onlyINTEGER UNSIGNED AUTO_INCREMENT● These indexes are duplicates: {a} - {a, b}● This index is wrong: {a, id}
Index implementation
https://github.com/jeremycole/innodb_diagrams
More performance considerations?
More performance considerations?● Writing to an index is relatively slow● Deleting many rows leaves fragmented indexes
WHERE is the index?
Phone Book● Indexes are ordered data structures● Think to them as a phone bookTable: {first_name, last_name, phone, address}Index: {last_name, first_name}
Phone Book● I will show you some queries, and you will tell me which can be solved byusing the index● You may not know, but your mind contains a pretty good SQL optimiserTable: {first_name, last_name, phone, address}Index: {last_name, first_name}
QueriesSELECT * FROM phone_book …WHERE last_name = 'Baker'WHERE last_name IN ('Hartnell','Baker', 'Whittaker')WHERE last_name > 'Baker'WHERE last_name >= 'Baker'WHERE last_name < 'Baker'WHERE last_name <= 'Baker'WHERE last_name <> 'Baker'
QueriesSELECT * FROM phone_book …WHERE last_name IS NULLWHERE last_name IS NOT NULL
Rule #1A BTREE can optimisepoint searches and ranges
QueriesWHERE last_name >= 'B' AND last_name < 'C'WHERE last_name BETWEEN 'B' AND 'C'WHERE last_name LIKE 'B%'
QueriesWHERE last_name LIKE 'B%'WHERE last_name LIKE '%B%'WHERE last_name LIKE '%B'WHERE last_name LIKE 'B_'WHERE last_name LIKE '_B_'WHERE last_name LIKE '_B'
Rule #2A LIKE conditionwhose second operand starts with a 'constant string'is a range
QueriesWHERE first_name = 'Tom'WHERE last_name = 'Baker'WHERE first_name = 'Tom' AND last_name = 'Baker'WHERE last_name = 'Baker' AND first_name = 'Tom'
Rule #3We can use a whole indexor its leftmost part
QueriesWHERE LEFT(last_name, 2) = 'Ba'WHERE last_name = CONCAT('Ba', 'ker')
Rule #4Optimiser cannot make assumptions on functions/expression results.However, wrapping a constant value into a function will produce anotherconstant value, which is mostly irrelevant for query optimisation.
QueriesWHERE last_name = first_name
Rule #5Comparing a column with another results in a comparisonwhose operands change at every row.The optimiser cannot filter out any row in advance.
QueriesWHERE last_name = 'Baker' AND phone = '+44 7739 427279'
Rule #6We can use an index to restrict the search to a set of rowsAnd search those rows in a non-optimised fashionDepending on this set’s size, this could be a brilliant or a terrible strategy
QueriesWHERE last_name = 'Baker' AND first_name > 'Tom'WHERE last_name > 'Baker' AND first_name = 'Tom'WHERE last_name > 'Baker' AND first_name > 'Tom'
QueriesWHERE last_name = 'Baker' AND first_name > 'Tom'WHERE first_name = 'Tom' AND last_name > 'Baker'WHERE first_name > 'Tom' AND last_name = 'Baker'WHERE last_name > 'Baker' AND first_name > 'Tom'Baker, ColinBaker, TomBaker, WalterCapaldi, AdaCapaldi, PeterWhittaker, JodyWhittaker, Vadim
Rule #7If we have a range condition on an index columnThe next index columns cannot be usedIf you prefer:Index usage stops at the first >
ORDER BY, GROUP BY
Mr Speaker talks to MySQL
QueriesORDER BY last_nameORDER BY first_nameORDER BY last_name, first_nameORDER BY first_name, last_name
QueriesGROUP BY last_nameGROUP BY first_nameGROUP BY last_name, first_nameGROUP BY first_name, last_name
Rule #8ORDER BY and GROUP BY can take advantage of an index orderor create an internal temp tableNote: GROUP BY optimisation also depends on the function we’re using(MAX, COUNT…).
QueriesWHERE last_name > 'Baker' ORDER BY last_nameWHERE last_name = 'Baker' ORDER BY first_nameWHERE last_name > 'Baker' ORDER BY first_name
Rule #9If we have an ORDER BY / GROUP BY on an index columnThe next index columns cannot be used
Multiple Indexes
QueriesTable: {id, a, b, c, d}idx_a: {a, d}idx_b: {b}WHERE a = 10 OR a = 20WHERE a = 24 OR c = 42WHERE a = 24 OR d = 42WHERE a = 24 AND b = 42WHERE a = 24 OR b = 42WHERE a = 24 ORDER BY bGROUP BY a ORDER BY b
Rule #10Using multiple indexes for AND or OR (intersect) is possible,but there is a benefit only if we read MANY rowsUsing different indexes in WHERE / GROUP BY / ORDER BYis not possible
Thank you kindly!https://federico-razzoli.cominfo@federico-razzoli.com
MySQL Query Optimisation 101

Recommended

PDF
Advanced MySQL Query Tuning
PDF
Advanced MySQL Query and Schema Tuning
PDF
How MySQL can boost (or kill) your application v2
PDF
How MySQL can boost (or kill) your application
PDF
56 Query Optimization
PDF
Optimizing Queries with Explain
PPT
SQL212.2 Introduction to SQL using Oracle Module 2
PDF
0888 learning-mysql
PDF
Python for web security - beginner
PPTX
Optimizing MySQL Queries
PDF
Mysql query optimization
PDF
Efficient Pagination Using MySQL
PPTX
Optimizing queries MySQL
PPT
Intro To TSQL - Unit 1
 
PDF
45 Essential SQL Interview Questions
PPTX
Oracle basic queries
PPTX
Oraclesql
DOCX
My Sql concepts
PPTX
Subqueries, Backups, Users and Privileges
PPT
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
PPT
Intro To TSQL - Unit 4
 
PDF
Predicting Future Sale
PPT
Intro To TSQL - Unit 3
 
PDF
Explaining the MySQL Explain
PPTX
MySQL performance tuning
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
PPTX
Confoo 2021 - MySQL Indexes & Histograms
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
PPTX
MySQL Indexing - Best practices for MySQL 5.6
PDF
MySQL Performance Optimization

More Related Content

PDF
Advanced MySQL Query Tuning
PDF
Advanced MySQL Query and Schema Tuning
PDF
How MySQL can boost (or kill) your application v2
PDF
How MySQL can boost (or kill) your application
PDF
56 Query Optimization
PDF
Optimizing Queries with Explain
PPT
SQL212.2 Introduction to SQL using Oracle Module 2
PDF
0888 learning-mysql
Advanced MySQL Query Tuning
Advanced MySQL Query and Schema Tuning
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application
56 Query Optimization
Optimizing Queries with Explain
SQL212.2 Introduction to SQL using Oracle Module 2
0888 learning-mysql

What's hot

PDF
Python for web security - beginner
PPTX
Optimizing MySQL Queries
PDF
Mysql query optimization
PDF
Efficient Pagination Using MySQL
PPTX
Optimizing queries MySQL
PPT
Intro To TSQL - Unit 1
 
PDF
45 Essential SQL Interview Questions
PPTX
Oracle basic queries
PPTX
Oraclesql
DOCX
My Sql concepts
PPTX
Subqueries, Backups, Users and Privileges
PPT
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
PPT
Intro To TSQL - Unit 4
 
PDF
Predicting Future Sale
PPT
Intro To TSQL - Unit 3
 
PDF
Explaining the MySQL Explain
Python for web security - beginner
Optimizing MySQL Queries
Mysql query optimization
Efficient Pagination Using MySQL
Optimizing queries MySQL
Intro To TSQL - Unit 1
 
45 Essential SQL Interview Questions
Oracle basic queries
Oraclesql
My Sql concepts
Subqueries, Backups, Users and Privileges
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
Intro To TSQL - Unit 4
 
Predicting Future Sale
Intro To TSQL - Unit 3
 
Explaining the MySQL Explain

Similar to MySQL Query Optimisation 101

PPTX
MySQL performance tuning
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
PPTX
Confoo 2021 - MySQL Indexes & Histograms
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
PPTX
MySQL Indexing - Best practices for MySQL 5.6
PDF
MySQL Performance Optimization
PPTX
Optimizing MySQL queries
PPTX
MySQL Indexes
PPTX
MySQL index optimization techniques
PDF
Scaling MySQL Strategies for Developers
PPTX
Работа с индексами - лучшие практики для MySQL 5.6, Петр Зайцев (Percona)
 
PPTX
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
PPTX
PHP UK 2020 Tutorial: MySQL Indexes, Histograms And other ways To Speed Up Yo...
PDF
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
PDF
Introduction into MySQL Query Tuning
PDF
Introduction to MySQL Query Tuning for Dev[Op]s
PDF
High Performance Mysql - Friday Tech Talks at Squareboat
PDF
Zurich2007 MySQL Query Optimization
PDF
Zurich2007 MySQL Query Optimization
PDF
query optimization
MySQL performance tuning
MySQL Indexes and Histograms - RMOUG Training Days 2022
Confoo 2021 - MySQL Indexes & Histograms
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
MySQL Indexing - Best practices for MySQL 5.6
MySQL Performance Optimization
Optimizing MySQL queries
MySQL Indexes
MySQL index optimization techniques
Scaling MySQL Strategies for Developers
Работа с индексами - лучшие практики для MySQL 5.6, Петр Зайцев (Percona)
 
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
PHP UK 2020 Tutorial: MySQL Indexes, Histograms And other ways To Speed Up Yo...
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Introduction into MySQL Query Tuning
Introduction to MySQL Query Tuning for Dev[Op]s
High Performance Mysql - Friday Tech Talks at Squareboat
Zurich2007 MySQL Query Optimization
Zurich2007 MySQL Query Optimization
query optimization

More from Federico Razzoli

PDF
Database Design most common pitfalls
PDF
MariaDB stored procedures and why they should be improved
PDF
MariaDB/MySQL_: Developing Scalable Applications
PDF
Advanced MariaDB features that developers love.pdf
PDF
MariaDB 10.11 key features overview for DBAs
PDF
Recent MariaDB features to learn for a happy life
PDF
MariaDB Data Protection: Backup Strategies for the Real World
PDF
MariaDB Security Best Practices
PDF
A first look at MariaDB 11.x features and ideas on how to use them
PDF
MariaDB Temporal Tables
PDF
Playing with the CONNECT storage engine
PDF
Webinar - MariaDB Temporal Tables: a demonstration
PDF
Webinar - Unleash AI power with MySQL and MindsDB
PDF
MariaDB, MySQL and Ansible: automating database infrastructures
PDF
High-level architecture of a complete MariaDB deployment
PDF
Webinar: Designing a schema for a Data Warehouse
PDF
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
PDF
Automate MariaDB Galera clusters deployments with Ansible
PDF
MySQL and MariaDB Backups
PDF
Creating Vagrant development machines with MariaDB
Database Design most common pitfalls
MariaDB stored procedures and why they should be improved
MariaDB/MySQL_: Developing Scalable Applications
Advanced MariaDB features that developers love.pdf
MariaDB 10.11 key features overview for DBAs
Recent MariaDB features to learn for a happy life
MariaDB Data Protection: Backup Strategies for the Real World
MariaDB Security Best Practices
A first look at MariaDB 11.x features and ideas on how to use them
MariaDB Temporal Tables
Playing with the CONNECT storage engine
Webinar - MariaDB Temporal Tables: a demonstration
Webinar - Unleash AI power with MySQL and MindsDB
MariaDB, MySQL and Ansible: automating database infrastructures
High-level architecture of a complete MariaDB deployment
Webinar: Designing a schema for a Data Warehouse
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
Automate MariaDB Galera clusters deployments with Ansible
MySQL and MariaDB Backups
Creating Vagrant development machines with MariaDB

Recently uploaded

PDF
Advanced Prompt Engineering: The Art and Science
PDF
What Is A Woman (WIAW) Token – Smart Contract Security Audit Report by EtherA...
PDF
nsfconvertersoftwaretoconvertNSFtoPST.pdf
PPTX
Lecture 3 - Scheduling - Operating System
PDF
How Does AI Improve Location-Based Mobile App Development for Businesses.pdf
PPTX
Why Your Business Needs Snowflake Consulting_ From Data Silos to AI-Ready Cloud
 
PDF
Combinatorial Interview Problems with Backtracking Solutions - From Imperativ...
PPT
This-Project-Demonstrates-How-to-Create.ppt
PDF
Database Management Systems(DBMS):UNIT-II Relational Data Model BCA SEP SEM ...
PPTX
Managed Splunk Partner vs In-House: Cost, Risk & Value Comparison
DOCX
How to Change Classic SharePoint to Modern SharePoint (An Updated Guide)
PDF
Why Zoho Notebook’s AI-Fueled Upgrade Matters for Knowledge Workers in 2026
PDF
Manual vs Automated Accessibility Testing – What to Choose in 2025.pdf
PPTX
Struggling with Pentaho Limitations How Helical Insight Solves Them.pptx
PPTX
Modern Claims Automation Solutions for Operational Agility
PDF
Cloud-Based Underwriting Software for Insurance
PPTX
Magnet-AXIOM_overview_tool_cyber_tool.pptx
PDF
Navigating SEC Regulations for Crypto Exchanges Preparing for a Compliant Fut...
PDF
Virtual Study Circles Innovative Ways to Collaborate Online.pdf
PPTX
AI Clinic Management Software for Otolaryngology Clinics Bringing Precision, ...
Advanced Prompt Engineering: The Art and Science
What Is A Woman (WIAW) Token – Smart Contract Security Audit Report by EtherA...
nsfconvertersoftwaretoconvertNSFtoPST.pdf
Lecture 3 - Scheduling - Operating System
How Does AI Improve Location-Based Mobile App Development for Businesses.pdf
Why Your Business Needs Snowflake Consulting_ From Data Silos to AI-Ready Cloud
 
Combinatorial Interview Problems with Backtracking Solutions - From Imperativ...
This-Project-Demonstrates-How-to-Create.ppt
Database Management Systems(DBMS):UNIT-II Relational Data Model BCA SEP SEM ...
Managed Splunk Partner vs In-House: Cost, Risk & Value Comparison
How to Change Classic SharePoint to Modern SharePoint (An Updated Guide)
Why Zoho Notebook’s AI-Fueled Upgrade Matters for Knowledge Workers in 2026
Manual vs Automated Accessibility Testing – What to Choose in 2025.pdf
Struggling with Pentaho Limitations How Helical Insight Solves Them.pptx
Modern Claims Automation Solutions for Operational Agility
Cloud-Based Underwriting Software for Insurance
Magnet-AXIOM_overview_tool_cyber_tool.pptx
Navigating SEC Regulations for Crypto Exchanges Preparing for a Compliant Fut...
Virtual Study Circles Innovative Ways to Collaborate Online.pdf
AI Clinic Management Software for Otolaryngology Clinics Bringing Precision, ...

MySQL Query Optimisation 101

  • 1.
  • 2.
    € whoami● FedericoRazzoli● Freelance consultant● Writing SQL since MySQL 2.23hello@federico-razzoli.com● I love open source, sharing,Collaboration, win-win, etc● I love MariaDB, MySQL, Postgres, etc○ Even Db2, somehow
  • 3.
    Why is thedatabase important?
  • 4.
    Remember the VonNeumann machine?
  • 5.
    It’s always aboutData● Since then, the purpose of hardware and software never changed:○ Receive data○ Process data○ Output data
  • 6.
    A rose byany other name...● Feel free to use synonyms○ Validate○ Sanitise○ Parse○ Persist○ Normalise / Denormalise○ Cache○ Map / Reduce○ Print○ Ping○ ...
  • 7.
    ...would smell assweet● The database of known stars is not a ping package● You use a DBMS to abstract data management as much as possible○ Persistence○ Consistence○ Queries○ Fast search○ …● That’s why “database is magic”
  • 8.
    Busy● But it’sjust a very busy person, performing many tasks concurrently● And each is:○ Important (must be reliable)○ Complex (must be fast)○ Expected (if something goes wrong,you will complain)In practice, the DBMS is usually the bottleneck.
  • 9.
    TerminologyStatement: An SQLcommandQuery: A statement that returns a resultset...or any other statement :)Resultset: output 0 or more rowsOptimiser / Query Planner: Component responsible of deciding a query’sexecution planOptimised query: A query whose execution plan is reasonably good...this doesn’t imply in any way that the query is fastDatabase: set of tables (schema)Instance / Server: running MySQL daemon (cluster)
  • 10.
  • 11.
    When should aquery be optimised?mysql> EXPLAIN SELECT * FROM t WHERE c < 10 G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tpartitions: NULLtype: ALLpossible_keys: idx_ckey: idx_ckey_len: 4ref: NULLrows: 213030filtered: 50.00Extra: Using where1 row in set, 1 warning (0.01 sec)
  • 12.
    When should aquery be optimised?mysql> SELECT * FROM performance_schema.events_statements_summary_by_digestWHERE DIGEST ='254a65744e661e072103b7a7630dee1c3a3b8e906f19889f7c796aebe7cdd4f8' G*************************** 1. row ***************************SCHEMA_NAME: testDIGEST: 254a65744e661e072103b7a7630dee1c3a3b8e906f19889f7c796aebe7cdd4f8DIGEST_TEXT: SELECT * FROM `t` WHERE `c` < ?COUNT_STAR: 1...SUM_ROWS_AFFECTED: 0SUM_ROWS_SENT: 57344SUM_ROWS_EXAMINED: 212992SUM_CREATED_TMP_DISK_TABLES: 0SUM_CREATED_TMP_TABLES: 0SUM_SELECT_FULL_JOIN: 0SUM_SELECT_FULL_RANGE_JOIN: 0SUM_SELECT_RANGE: 0SUM_SELECT_RANGE_CHECK: 0SUM_SELECT_SCAN: 1SUM_SORT_MERGE_PASSES: 0SUM_SORT_RANGE: 0SUM_SORT_ROWS: 0SUM_SORT_SCAN: 0SUM_NO_INDEX_USED: 1SUM_NO_GOOD_INDEX_USED: 0FIRST_SEEN: 2019-05-14 00:31:24.078967LAST_SEEN: 2019-05-14 00:31:24.078967...QUERY_SAMPLE_TEXT: SELECT * FROM t WHERE c < 10QUERY_SAMPLE_SEEN: 2019-05-14 00:31:24.078967QUERY_SAMPLE_TIMER_WAIT: 117493874000
  • 13.
    But how doI find impacting queries?● It depends what you mean by “impacting”● There are several monitoring methods (USE, etc)● But 3 philosophies:
  • 14.
    But how doI find impacting queries?● It depends what you mean by “impacting”● There are several monitoring methods (USE, etc)● But 3 philosophies:○ Panicking when you hear that something is down or slow
  • 15.
    But how doI find impacting queries?● It depends what you mean by “impacting”● There are several monitoring methods (USE, etc)● But 3 philosophies:○ Panicking when you hear that something is down or slow○ System-centric monitoring
  • 16.
    But how doI find impacting queries?● It depends what you mean by “impacting”● There are several monitoring methods (USE, etc)● But 3 philosophies:○ Panicking when you hear that something is down or slow○ System-centric monitoring○ User-centric
  • 17.
    But how doI find impacting queries?● Panicking when you hear that something is down or slow● System-centric monitoring● User-centricYou can use them all.
  • 18.
    Panicking● Simplest method●Do nothing do prevent anything○ Optionally, take a lot of actions to prevent imaginary problems inimaginary ways○ There is no evidence that your job is useless, so your boss will not fireyou
  • 19.
    System-centric● pt-query-digest, PMM,etc● Merge queries into one, normalising its text and replacing parameters○ SELECT * FROM t WHERE b= 111 AND a = 0 -- comment○ Select * From t Where a = 24 and b=42;○ SELECT * FROM t WHERE a = ? AND b = ?● Sum execution time of each occurrence (Grand Total Time)● Optimise the queries with highest GTT
  • 20.
    User-Centric● Calculate thecost of slowness (users don’t buy, maybe leave 4ever)● Cost of slowness is different for different○ URLs○ Number of users○ ...other variables that depend on your business■ (day of month, country, etc)● Set Service Level Objectives● Monitor the HTTP calls latency, and the involved services● Find out what’s slowing them down
  • 21.
  • 22.
    What makes aquery “important”?● How many times it’s executed● It’s locking
  • 23.
    What makes aquery slow?● Number of rows read○ Read != return○ Indexes are there to lower the number of reads● Number of rows written○ In-memory temp tables are not good○ On-disk temp tables are worse
  • 24.
    How do Ioptimise a query?● Use indexes properly● Avoid creation of temp tables, if possible
  • 25.
  • 26.
    Index Types● BTREE- ordered data structure● HASH - hash table● PostgreSQL has much more● Each storage engine can implement any of both● InnoDB uses BTREE and internally uses HASH when it thinks it’s better● The syntax CREATE INDEX USING [BTREE | HASH] is generally uselessWe will focus on BTREE indexes in InnoDB
  • 27.
    Index Properties● Primarykey: unique values, not null● UNIQUE● Multiple columns○ The order matters● Column prefix (only for strings)
  • 28.
    InnoDB Indexes● InnoDBtables should always have a Primary Key● The table is stored ordered by primary key● The table itself is the primary key○ Columns “not part of the primary key” simply don’t affect the order ofrows
  • 29.
    InnoDB IndexesTable columns:{a, b, c}Primary key: {a, b}A B C1 1 41 2 12 1 92 2 33 0 34 20 0
  • 30.
    InnoDB Indexes● Secondaryindexes are stored separately● They are ordered by the indexed column● Each entry contain a reference to a primary key entry
  • 31.
    InnoDB IndexesPrimary key:{a, b}Index idx_c: {c}c a b0 4 201 1 23 2 23 3 04 1 19 2 1
  • 32.
    Which queries willbe faster?Table columns: {a, b, c, d, e}Primary key: {a, b}Index idx_c: {c}● SELECT * FROM t WHERE a = 1● SELECT * FROM t WHERE a = 1 AND b = 2● SELECT a, b FROM t WHERE c = 0● SELECT d, e FROM t WHERE c = 0
  • 33.
  • 34.
    More performance considerations?●Big primary key = big indexes● Primary key should be append-onlyINTEGER UNSIGNED AUTO_INCREMENT● These indexes are duplicates: {a} - {a, b}● This index is wrong: {a, id}
  • 35.
  • 36.
  • 37.
  • 38.
    More performance considerations?●Writing to an index is relatively slow● Deleting many rows leaves fragmented indexes
  • 39.
  • 40.
    Phone Book● Indexesare ordered data structures● Think to them as a phone bookTable: {first_name, last_name, phone, address}Index: {last_name, first_name}
  • 41.
    Phone Book● Iwill show you some queries, and you will tell me which can be solved byusing the index● You may not know, but your mind contains a pretty good SQL optimiserTable: {first_name, last_name, phone, address}Index: {last_name, first_name}
  • 42.
    QueriesSELECT * FROMphone_book …WHERE last_name = 'Baker'WHERE last_name IN ('Hartnell','Baker', 'Whittaker')WHERE last_name > 'Baker'WHERE last_name >= 'Baker'WHERE last_name < 'Baker'WHERE last_name <= 'Baker'WHERE last_name <> 'Baker'
  • 43.
    QueriesSELECT * FROMphone_book …WHERE last_name IS NULLWHERE last_name IS NOT NULL
  • 44.
    Rule #1A BTREEcan optimisepoint searches and ranges
  • 45.
    QueriesWHERE last_name >='B' AND last_name < 'C'WHERE last_name BETWEEN 'B' AND 'C'WHERE last_name LIKE 'B%'
  • 46.
    QueriesWHERE last_name LIKE'B%'WHERE last_name LIKE '%B%'WHERE last_name LIKE '%B'WHERE last_name LIKE 'B_'WHERE last_name LIKE '_B_'WHERE last_name LIKE '_B'
  • 47.
    Rule #2A LIKEconditionwhose second operand starts with a 'constant string'is a range
  • 48.
    QueriesWHERE first_name ='Tom'WHERE last_name = 'Baker'WHERE first_name = 'Tom' AND last_name = 'Baker'WHERE last_name = 'Baker' AND first_name = 'Tom'
  • 49.
    Rule #3We canuse a whole indexor its leftmost part
  • 50.
    QueriesWHERE LEFT(last_name, 2)= 'Ba'WHERE last_name = CONCAT('Ba', 'ker')
  • 51.
    Rule #4Optimiser cannotmake assumptions on functions/expression results.However, wrapping a constant value into a function will produce anotherconstant value, which is mostly irrelevant for query optimisation.
  • 52.
  • 53.
    Rule #5Comparing acolumn with another results in a comparisonwhose operands change at every row.The optimiser cannot filter out any row in advance.
  • 54.
    QueriesWHERE last_name ='Baker' AND phone = '+44 7739 427279'
  • 55.
    Rule #6We canuse an index to restrict the search to a set of rowsAnd search those rows in a non-optimised fashionDepending on this set’s size, this could be a brilliant or a terrible strategy
  • 56.
    QueriesWHERE last_name ='Baker' AND first_name > 'Tom'WHERE last_name > 'Baker' AND first_name = 'Tom'WHERE last_name > 'Baker' AND first_name > 'Tom'
  • 57.
    QueriesWHERE last_name ='Baker' AND first_name > 'Tom'WHERE first_name = 'Tom' AND last_name > 'Baker'WHERE first_name > 'Tom' AND last_name = 'Baker'WHERE last_name > 'Baker' AND first_name > 'Tom'Baker, ColinBaker, TomBaker, WalterCapaldi, AdaCapaldi, PeterWhittaker, JodyWhittaker, Vadim
  • 58.
    Rule #7If wehave a range condition on an index columnThe next index columns cannot be usedIf you prefer:Index usage stops at the first >
  • 59.
  • 60.
  • 61.
    QueriesORDER BY last_nameORDERBY first_nameORDER BY last_name, first_nameORDER BY first_name, last_name
  • 62.
    QueriesGROUP BY last_nameGROUPBY first_nameGROUP BY last_name, first_nameGROUP BY first_name, last_name
  • 63.
    Rule #8ORDER BYand GROUP BY can take advantage of an index orderor create an internal temp tableNote: GROUP BY optimisation also depends on the function we’re using(MAX, COUNT…).
  • 64.
    QueriesWHERE last_name >'Baker' ORDER BY last_nameWHERE last_name = 'Baker' ORDER BY first_nameWHERE last_name > 'Baker' ORDER BY first_name
  • 65.
    Rule #9If wehave an ORDER BY / GROUP BY on an index columnThe next index columns cannot be used
  • 66.
  • 67.
    QueriesTable: {id, a,b, c, d}idx_a: {a, d}idx_b: {b}WHERE a = 10 OR a = 20WHERE a = 24 OR c = 42WHERE a = 24 OR d = 42WHERE a = 24 AND b = 42WHERE a = 24 OR b = 42WHERE a = 24 ORDER BY bGROUP BY a ORDER BY b
  • 68.
    Rule #10Using multipleindexes for AND or OR (intersect) is possible,but there is a benefit only if we read MANY rowsUsing different indexes in WHERE / GROUP BY / ORDER BYis not possible
  • 69.

[8]ページ先頭

©2009-2025 Movatter.jp