SlideShare uma empresa Scribd logo
1 de 46
Baixar para ler offline
Modern Query Optimisation
Features In MySQL 8


Karthik P R
CEO, Mydbops
Apr 24, 2021
Mydbops Webinar
Interested in Open Source Database technologies
11 Years of Experience with MySQL
Ex-Yahoo!
Tech Speaker/ Blogger 
CEO Mydbops
Karthik P R 
About Me
Database
Consulting
Services
Managed
Database
Services
Focuses on Top Opensource database MySQL,MariaDB,
MongoDB and PostgreSQL ON Premises and Cloud
Mydbops Services
MySQL 8 (A Major Leap)
Indexing features
Execution Plan Improvements
Optimiser Imporvements
Others Improvements
Agenda
MySQL 8 is a Major Leap
MySQL 8 is a Major Leap
MySQL 8
InnoDB Performance Enhancement
Faster Parallel Replication
Security Enhancements
Operational Improvements ( Shell,Clone Plugin )
CTE , Window Function and Lateral Derived tables
Indexing Features
Indexing Features
Functional Index
Descending Index
Invisible Index
Functional Index
Functional Index
MySQL do Support Functional Index 8.0.13
A functional key part of queries can be indexed
JSON based Queries benefits a lot
Functional Index ( Before )
EXPLAIN SELECT VERSION,TILE FROM NODES_BACKUP WHERE DATE(TIMESTAMP)='2021-04-04';

+----+-------------+--------------+------------+------+---------------+------+---------+------+----------+----------+-------------+

| ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA |

+----+-------------+--------------+------------+------+---------------+------+---------+------+----------+----------+-------------+

| 1 | SIMPLE | NODES_BACKUP | NULL | ALL | NULL | NULL | NULL | NULL | 47378283 | 100.00 | USING WHERE |

+----+-------------+--------------+------------+------+---------------+------+---------+------+----------+----------+-------------+

ROWS SCANNED	 : 47M
TYPE	 	 : FULL TABLE SCAN
Functional Index ( Possible Solutions )
Query Rewrites
	 Application Rewrites
Rewriter Plugin
ProxySQL Rewrites
Virtual Column ( Again needs a Rewrite )
Functional Index ( After )
CREATE INDEX IDX_FUN_DATE ON NODES_BACKUP((DATE(TIMESTAMP)))

QUERY OK, 0 ROWS AFFECTED (5 MIN 30.38 SEC)

RECORDS: 0 DUPLICATES: 0 WARNINGS: 0

SELECT VERSION,TILE FROM NODES_BACKUP WHERE DATE(TIMESTAMP)='2021-04-04';

+----+-------------+--------------+------------+------+---------------+--------------+---------+-------+------+----------+-------+

| ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA |

+----+-------------+--------------+------------+------+---------------+--------------+---------+-------+------+----------+-------+

| 1 | SIMPLE | NODES_BACKUP | NULL | REF | IDX_FUN_DATE | IDX_FUN_DATE | 4 | CONST | 423 | 100.00 | NULL |

+----+-------------+--------------+------------+------+---------------+--------------+---------+-------+------+----------+-------+

1 ROW IN SET, 1 WARNING (0.01 SEC)
Descending Index
Descending Index
MySQL do Support Functional Index 8.0
B+Tree Indexing ( InnoDB )
Index are generally Forward Scan
Queries with different sorting orders are benefited a lot
ASC / DESC Keywords must be used while indexing
Idenitify the queries for optimisation via "Backward index scan" in Extra
filed.
Descending Index
EXPLAIN SELECT EMAIL,DESCRIPTION FROM USERS ORDER BY CREATION_TIME DESC ,DISPLAY_NAME DESC LIMIT 10;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+

| ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+

| 1 | SIMPLE | USERS | NULL | ALL | NULL | NULL | NULL | NULL | 3941 | 100.00 | USING FILESORT |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+

CREATE INDEX IDX_CT_DN ON USERS(CREATION_TIME,DISPLAY_NAME);

QUERY OK, 0 ROWS AFFECTED (0.03 SEC)

EXPLAIN SELECT EMAIL,DESCRIPTION FROM USERS ORDER BY CREATION_TIME DESC ,DISPLAY_NAME DESC LIMIT 10;

+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+---------------------+

| ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA |

+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+---------------------+

| 1 | SIMPLE | USERS | NULL | INDEX | NULL | IDX_CT_DN | 772 | NULL | 10 | 100.00 | BACKWARD INDEX SCAN |

+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+---------------------+
Descending Index
Query uses the Composite Index ( Creation Time , Display name )
No ordering is mentioned in while Indexing
Query uses Backward Scan ( Explain Plan )
But Forwards scan will have 15% Performance gain
Descending Index
CREATE INDEX IDX_CT_DN_DESC ON USERS(CREATION_TIME DESC,DISPLAY_NAME DESC);

QUERY OK, 0 ROWS AFFECTED (0.03 SEC)

RECORDS: 0 DUPLICATES: 0 WARNINGS: 0

EXPLAIN SELECT EMAIL,DESCRIPTION FROM USERS ORDER BY CREATION_TIME DESC ,DISPLAY_NAME DESC LIMIT 10;

+----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-------+

| ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA |

+----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-------+

| 1 | SIMPLE | USERS | NULL | INDEX | NULL | IDX_CT_DN_DESC | 772 | NULL | 10 | 100.00 | NULL |

+----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-------+
Descending Index ( Limitation )
Only Supports InnoDB Engine
Full text is not supported
They are not Buffered in Change Buffer
Invisible Index
Indexing for Performance ( Process followed Before )
Find the Query for optimisation
Create the index ( Affects Optimiser )
Evaluate the Query Performane
Retain the Index if it benefits queries
Drop the Index if it is not much benefit
Indexing for Performance ( After )
Note : Can be followed for dropping an unused index too.
Find the Query for optimisation
Create the index using Invisible as Keyword
Turn on Switch 'use_invisible-indexes=on' ( session )
Evaluate the Query Performane now.
Make the index Visible if it benefits.
Drop the Index if it is not much benefit.
Execution Plan Improvements
Execution Plan Improvements
Explain Format in Tree
Explain Analyze
Explain Format=Tree
Explain Format=Tree
Explain Format in Tree was introduced in MySQL 8.0.16
Shows Query Plans and Cost estimations
Easy understanding on internal operations
Identation helps understanding the execution orders
Explain ( Traditional / Tabular )
explain SELECT 'node' as type, node_id as id FROM node_tags WHERE k='bridge' and v='memorial' UNION SELECT 'way' as type, way_id as id FROM
way_tags WHERE k='lanes' and v='cafe' UNION SELECT 'relation' as type, relation_id as id FROM relation_tags WHERE k='tunnel' and
v='Barkingside';
+----+--------------+---------------+------------+------+---------------+------+---------+------+---------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+---------------+------------+------+---------------+------+---------+------+---------+----------+-----------------+
| 1 | PRIMARY | node_tags | NULL | ALL | NULL | NULL | NULL | NULL | 822783 | 1.00 | Using where |
| 2 | UNION | way_tags | NULL | ALL | NULL | NULL | NULL | NULL | 1347360 | 1.00 | Using where |
| 3 | UNION | relation_tags | NULL | ALL | NULL | NULL | NULL | NULL | 62797 | 1.00 | Using where |
|NULL| UNION RESULT | <union1,2,3> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------+---------------+------------+------+---------------+------+---------+------+---------+----------+-----------------+
Explain Format=Tree
explain format=tree SELECT 'node' as type, node_id as id FROM node_tags WHERE k='bridge' and v='memorial' UNION SELECT 'way' as type, way_id
as id FROM
way_tags WHERE k='lanes' and v='cafe' UNION SELECT 'relation' as type, relation_id as id FROM relation_tags WHERE k='tunnel' and
v='Barkingside'G
*************************** 1. row ***************************
EXPLAIN: -> Table scan on <union temporary> (cost=0.01..281.61 rows=22329)
-> Union materialize with deduplication (cost=227556.45..227838.05 rows=22329)
-> Filter: ((node_tags.k = 'bridge') and (node_tags.v = 'memorial')) (cost=83072.55 rows=8228)
-> Table scan on node_tags (cost=83072.55 rows=822783)
-> Filter: ((way_tags.k = 'lanes') and (way_tags.v = 'cafe')) (cost=135899.00 rows=13474)
-> Table scan on way_tags (cost=135899.00 rows=1347360)
-> Filter: ((relation_tags.k = 'tunnel') and (relation_tags.v = 'Barkingside')) (cost=6351.95 rows=628)
-> Table scan on relation_tags (cost=6351.95 rows=62797)
Explain Analyze
Explain Analyze
Explain Analyse was introduced in MySQL 8.0.18
Actual Time spent on each Iterator.
Time to fetch first row
Time to fetch all rows
Better than Optimiser Trace
Explain Analyze
explain analyze SELECT 'node' as type, node_id as id FROM node_tags WHERE k='bridge' and v='memorial' UNION SELECT 'way' as type, way_id as
id FROM way_tags WHERE k='lanes' and v='cafe' UNION SELECT 'relation' as type, relation_id as id FROM relation_tags WHERE k='tunnel' and
v='Barkingside'G
*************************** 1. row ***************************
EXPLAIN: -> Table scan on <union temporary> (cost=0.01..281.61 rows=22329) (actual time=0.002..0.002 rows=0 loops=1)
-> Union materialize with deduplication (cost=227556.45..227838.05 rows=22329) (actual time=636.728..636.728 rows=0 loops=1)
-> Filter: ((node_tags.k = 'bridge') and (node_tags.v = 'memorial')) (cost=83072.55 rows=8228) (actual time=233.393..233.393 rows=0
loops=1)
-> Table scan on node_tags (cost=83072.55 rows=822783) (actual time=0.039..182.413 rows=833423 loops=1)
-> Filter: ((way_tags.k = 'lanes') and (way_tags.v = 'cafe')) (cost=135899.00 rows=13474) (actual time=385.278..385.278 rows=0
loops=1)
-> Table scan on way_tags (cost=135899.00 rows=1347360) (actual time=0.039..299.565 rows=1343477 loops=1)
-> Filter: ((relation_tags.k = 'tunnel') and (relation_tags.v = 'Barkingside')) (cost=6351.95 rows=628) (actual time=18.046..18.046
rows=0 loops=1)
-> Table scan on relation_tags (cost=6351.95 rows=62797) (actual time=0.039..14.101 rows=63220 loops=1)
Explain Analyze
Explain Tree ( Cost ) Analyze (time in ms)
Primary 83072 233
UNION 135899 385
UNION 6352 18
 UNION Result 227556  636 
Note : Analyze result is 15-30% slower than actual time
Optimiser Improvements
Optimiser Improvements
Optimiser is the brain of any RDBMS , better algorithms and better statistics will
make its intelligence better.
Histogram
Hash Joins
Histogram
Histogram
Syntax :
analyze table table_name update/drop histogram with N buckets ;
The Data distribution is not uniform
Histogram helps in Better stat to DB.
MySQL has Histogram from 8.0.3 ( before GA )
Histogram can be applied for single column / multi columns.
Histogram
Title
0
500
1,000
1,500
2,000
2,500
3,000
3,500
4,000
0 1​ 2​
​
series1
Count value
1 1 0
2 3874 1
3 116 2
Data Distribution
Histogram (Before)
Optimizer Guesstimate the row filtering is common
Histogram (After)
Row filtering has improved post Histogram
Histogram
Histograms are better than indexes at cases.
Lesser maintenance over head.
Controlled by condition_fanout_filter.
1024 is the maximum buckets allowed.
Histogram stats for table can be visualised on
COLUMN_STATISTICS ( IS table ).
Hash Join
Optimiser Improvements
Hash Join
MySQL Support Hash Join from 8.0.18
Improved further in 8.0.20 ( Left Join )
HASH Join algorithm help in better Join Optimisation than BNL.
Equi Joins are much benefitted
BNL Support was removed from MySQL 8.0.20
Hash Join
In memory Hash table
Join Buffer Size Prevents over flow
Optimiser switch hash_join=ON (can't be disabled )
Other Optimisation
	 	 	 Perfer_orderding_index
	 	 	 sub_query_to-derived
CTE ( Common Table expression )
Lateral Derived Tables
Window Function
Switches
Reach Us : Info@mydbops.com
Thank You

Mais conteúdo relacionado

Mais procurados

PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
elliando dias
 

Mais procurados (20)

Tuning Autovacuum in Postgresql
Tuning Autovacuum in PostgresqlTuning Autovacuum in Postgresql
Tuning Autovacuum in Postgresql
 
Maxscale_메뉴얼
Maxscale_메뉴얼Maxscale_메뉴얼
Maxscale_메뉴얼
 
MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바
 
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdfMySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
MySQL Ecosystem in 2023 - FOSSASIA'23 - Alkin.pptx.pdf
 
Postgresql database administration volume 1
Postgresql database administration volume 1Postgresql database administration volume 1
Postgresql database administration volume 1
 
ProxySQL in the Cloud
ProxySQL in the CloudProxySQL in the Cloud
ProxySQL in the Cloud
 
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdfProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
 
MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바
 
InnoDB Performance Optimisation
InnoDB Performance OptimisationInnoDB Performance Optimisation
InnoDB Performance Optimisation
 
Troubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationTroubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming Replication
 
Percona Toolkit for Effective MySQL Administration
Percona Toolkit for Effective MySQL AdministrationPercona Toolkit for Effective MySQL Administration
Percona Toolkit for Effective MySQL Administration
 
Advanced MySql Data-at-Rest Encryption in Percona Server
Advanced MySql Data-at-Rest Encryption in Percona ServerAdvanced MySql Data-at-Rest Encryption in Percona Server
Advanced MySql Data-at-Rest Encryption in Percona Server
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용
 
PostgreSQL replication
PostgreSQL replicationPostgreSQL replication
PostgreSQL replication
 
Patroni - HA PostgreSQL made easy
Patroni - HA PostgreSQL made easyPatroni - HA PostgreSQL made easy
Patroni - HA PostgreSQL made easy
 
Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
MySQL High Availability Solutions
MySQL High Availability SolutionsMySQL High Availability Solutions
MySQL High Availability Solutions
 

Semelhante a Modern query optimisation features in MySQL 8.

Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System Presentation
Richard Crowley
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
Jussi Pohjolainen
 

Semelhante a Modern query optimisation features in MySQL 8. (20)

New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12
 
MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7 Tutorial Dutch PHP Conference 2015MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7 Tutorial Dutch PHP Conference 2015
 
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
MySQL 5.7. Tutorial - Dutch PHP Conference 2015MySQL 5.7. Tutorial - Dutch PHP Conference 2015
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
 
Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System Presentation
 
MySQL SQL Tutorial
MySQL SQL TutorialMySQL SQL Tutorial
MySQL SQL Tutorial
 
Explain
ExplainExplain
Explain
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
Parallel Query in AWS Aurora MySQL
Parallel Query in AWS Aurora MySQLParallel Query in AWS Aurora MySQL
Parallel Query in AWS Aurora MySQL
 
Oracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor formatOracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor format
 
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
MySQL Cookbook: Recipes for Developers, Alkin Tezuysal and Sveta Smirnova - P...
 
MySQL Cookbook: Recipes for Developers
MySQL Cookbook: Recipes for DevelopersMySQL Cookbook: Recipes for Developers
MySQL Cookbook: Recipes for Developers
 
The Current State of Table API in 2022
The Current State of Table API in 2022The Current State of Table API in 2022
The Current State of Table API in 2022
 
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL Indexing
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
 
Do You Know The 11g Plan?
Do You Know The 11g Plan?Do You Know The 11g Plan?
Do You Know The 11g Plan?
 
Top 10 tips for Oracle performance
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performance
 
Window functions in MariaDB 10.2
Window functions in MariaDB 10.2Window functions in MariaDB 10.2
Window functions in MariaDB 10.2
 

Mais de Mydbops

Efficient MySQL Indexing and what's new in MySQL Explain
Efficient MySQL Indexing and what's new in MySQL ExplainEfficient MySQL Indexing and what's new in MySQL Explain
Efficient MySQL Indexing and what's new in MySQL Explain
Mydbops
 

Mais de Mydbops (20)

Efficient MySQL Indexing and what's new in MySQL Explain
Efficient MySQL Indexing and what's new in MySQL ExplainEfficient MySQL Indexing and what's new in MySQL Explain
Efficient MySQL Indexing and what's new in MySQL Explain
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
PostgreSQL Schema Changes with pg-osc - Mydbops @ PGConf India 2024
PostgreSQL Schema Changes with pg-osc - Mydbops @ PGConf India 2024PostgreSQL Schema Changes with pg-osc - Mydbops @ PGConf India 2024
PostgreSQL Schema Changes with pg-osc - Mydbops @ PGConf India 2024
 
Choosing the Right Database: Exploring MySQL Alternatives for Modern Applicat...
Choosing the Right Database: Exploring MySQL Alternatives for Modern Applicat...Choosing the Right Database: Exploring MySQL Alternatives for Modern Applicat...
Choosing the Right Database: Exploring MySQL Alternatives for Modern Applicat...
 
Mastering Aurora PostgreSQL Clusters for Disaster Recovery
Mastering Aurora PostgreSQL Clusters for Disaster RecoveryMastering Aurora PostgreSQL Clusters for Disaster Recovery
Mastering Aurora PostgreSQL Clusters for Disaster Recovery
 
Navigating Transactions: ACID Complexity in Modern Databases- Mydbops Open So...
Navigating Transactions: ACID Complexity in Modern Databases- Mydbops Open So...Navigating Transactions: ACID Complexity in Modern Databases- Mydbops Open So...
Navigating Transactions: ACID Complexity in Modern Databases- Mydbops Open So...
 
AWS RDS in MySQL 2023 Vinoth Kanna @ Mydbops OpenSource Database Meetup 15
AWS RDS in MySQL 2023 Vinoth Kanna @ Mydbops OpenSource Database Meetup 15AWS RDS in MySQL 2023 Vinoth Kanna @ Mydbops OpenSource Database Meetup 15
AWS RDS in MySQL 2023 Vinoth Kanna @ Mydbops OpenSource Database Meetup 15
 
Data-at-scale-with-TIDB Mydbops Co-Founder Kabilesh PR at LSPE Event
Data-at-scale-with-TIDB Mydbops Co-Founder Kabilesh PR at LSPE EventData-at-scale-with-TIDB Mydbops Co-Founder Kabilesh PR at LSPE Event
Data-at-scale-with-TIDB Mydbops Co-Founder Kabilesh PR at LSPE Event
 
MySQL Transformation Case Study: 80% Cost Savings & Uninterrupted Availabilit...
MySQL Transformation Case Study: 80% Cost Savings & Uninterrupted Availabilit...MySQL Transformation Case Study: 80% Cost Savings & Uninterrupted Availabilit...
MySQL Transformation Case Study: 80% Cost Savings & Uninterrupted Availabilit...
 
Scaling-MongoDB-with-Horizontal-and-Vertical-Sharding Mydbops Opensource Data...
Scaling-MongoDB-with-Horizontal-and-Vertical-Sharding Mydbops Opensource Data...Scaling-MongoDB-with-Horizontal-and-Vertical-Sharding Mydbops Opensource Data...
Scaling-MongoDB-with-Horizontal-and-Vertical-Sharding Mydbops Opensource Data...
 
Mastering MongoDB Atlas: Essentials of Diagnostics and Debugging in the Cloud...
Mastering MongoDB Atlas: Essentials of Diagnostics and Debugging in the Cloud...Mastering MongoDB Atlas: Essentials of Diagnostics and Debugging in the Cloud...
Mastering MongoDB Atlas: Essentials of Diagnostics and Debugging in the Cloud...
 
Data Organisation: Table Partitioning in PostgreSQL
Data Organisation: Table Partitioning in PostgreSQLData Organisation: Table Partitioning in PostgreSQL
Data Organisation: Table Partitioning in PostgreSQL
 
Navigating MongoDB's Queryable Encryption for Ultimate Security - Mydbops
Navigating MongoDB's Queryable Encryption for Ultimate Security - MydbopsNavigating MongoDB's Queryable Encryption for Ultimate Security - Mydbops
Navigating MongoDB's Queryable Encryption for Ultimate Security - Mydbops
 
Data High Availability With TIDB
Data High Availability With TIDBData High Availability With TIDB
Data High Availability With TIDB
 
Mastering Database Migration_ Native replication (8.0) to InnoDB Cluster (8.0...
Mastering Database Migration_ Native replication (8.0) to InnoDB Cluster (8.0...Mastering Database Migration_ Native replication (8.0) to InnoDB Cluster (8.0...
Mastering Database Migration_ Native replication (8.0) to InnoDB Cluster (8.0...
 
Enhancing Security of MySQL Connections using SSL certificates
Enhancing Security of MySQL Connections using SSL certificatesEnhancing Security of MySQL Connections using SSL certificates
Enhancing Security of MySQL Connections using SSL certificates
 
Exploring the Fundamentals of YugabyteDB - Mydbops
Exploring the Fundamentals of YugabyteDB - Mydbops Exploring the Fundamentals of YugabyteDB - Mydbops
Exploring the Fundamentals of YugabyteDB - Mydbops
 
Time series in MongoDB - Mydbops
Time series in MongoDB - Mydbops Time series in MongoDB - Mydbops
Time series in MongoDB - Mydbops
 
TiDB in a Nutshell - Power of Open-Source Distributed SQL Database - Mydbops
TiDB in a Nutshell - Power of Open-Source Distributed SQL Database - MydbopsTiDB in a Nutshell - Power of Open-Source Distributed SQL Database - Mydbops
TiDB in a Nutshell - Power of Open-Source Distributed SQL Database - Mydbops
 
Achieving High Availability in PostgreSQL
Achieving High Availability in PostgreSQLAchieving High Availability in PostgreSQL
Achieving High Availability in PostgreSQL
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Modern query optimisation features in MySQL 8.

  • 1. Modern Query Optimisation Features In MySQL 8 Karthik P R CEO, Mydbops Apr 24, 2021 Mydbops Webinar
  • 2. Interested in Open Source Database technologies 11 Years of Experience with MySQL Ex-Yahoo! Tech Speaker/ Blogger  CEO Mydbops Karthik P R  About Me
  • 3. Database Consulting Services Managed Database Services Focuses on Top Opensource database MySQL,MariaDB, MongoDB and PostgreSQL ON Premises and Cloud Mydbops Services
  • 4. MySQL 8 (A Major Leap) Indexing features Execution Plan Improvements Optimiser Imporvements Others Improvements Agenda
  • 5. MySQL 8 is a Major Leap
  • 6. MySQL 8 is a Major Leap MySQL 8 InnoDB Performance Enhancement Faster Parallel Replication Security Enhancements Operational Improvements ( Shell,Clone Plugin ) CTE , Window Function and Lateral Derived tables
  • 10. Functional Index MySQL do Support Functional Index 8.0.13 A functional key part of queries can be indexed JSON based Queries benefits a lot
  • 11. Functional Index ( Before ) EXPLAIN SELECT VERSION,TILE FROM NODES_BACKUP WHERE DATE(TIMESTAMP)='2021-04-04'; +----+-------------+--------------+------------+------+---------------+------+---------+------+----------+----------+-------------+ | ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA | +----+-------------+--------------+------------+------+---------------+------+---------+------+----------+----------+-------------+ | 1 | SIMPLE | NODES_BACKUP | NULL | ALL | NULL | NULL | NULL | NULL | 47378283 | 100.00 | USING WHERE | +----+-------------+--------------+------------+------+---------------+------+---------+------+----------+----------+-------------+ ROWS SCANNED : 47M TYPE : FULL TABLE SCAN
  • 12. Functional Index ( Possible Solutions ) Query Rewrites Application Rewrites Rewriter Plugin ProxySQL Rewrites Virtual Column ( Again needs a Rewrite )
  • 13. Functional Index ( After ) CREATE INDEX IDX_FUN_DATE ON NODES_BACKUP((DATE(TIMESTAMP))) QUERY OK, 0 ROWS AFFECTED (5 MIN 30.38 SEC) RECORDS: 0 DUPLICATES: 0 WARNINGS: 0 SELECT VERSION,TILE FROM NODES_BACKUP WHERE DATE(TIMESTAMP)='2021-04-04'; +----+-------------+--------------+------------+------+---------------+--------------+---------+-------+------+----------+-------+ | ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA | +----+-------------+--------------+------------+------+---------------+--------------+---------+-------+------+----------+-------+ | 1 | SIMPLE | NODES_BACKUP | NULL | REF | IDX_FUN_DATE | IDX_FUN_DATE | 4 | CONST | 423 | 100.00 | NULL | +----+-------------+--------------+------------+------+---------------+--------------+---------+-------+------+----------+-------+ 1 ROW IN SET, 1 WARNING (0.01 SEC)
  • 15. Descending Index MySQL do Support Functional Index 8.0 B+Tree Indexing ( InnoDB ) Index are generally Forward Scan Queries with different sorting orders are benefited a lot ASC / DESC Keywords must be used while indexing Idenitify the queries for optimisation via "Backward index scan" in Extra filed.
  • 16. Descending Index EXPLAIN SELECT EMAIL,DESCRIPTION FROM USERS ORDER BY CREATION_TIME DESC ,DISPLAY_NAME DESC LIMIT 10; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ | ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ | 1 | SIMPLE | USERS | NULL | ALL | NULL | NULL | NULL | NULL | 3941 | 100.00 | USING FILESORT | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ CREATE INDEX IDX_CT_DN ON USERS(CREATION_TIME,DISPLAY_NAME); QUERY OK, 0 ROWS AFFECTED (0.03 SEC) EXPLAIN SELECT EMAIL,DESCRIPTION FROM USERS ORDER BY CREATION_TIME DESC ,DISPLAY_NAME DESC LIMIT 10; +----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+---------------------+ | ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA | +----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+---------------------+ | 1 | SIMPLE | USERS | NULL | INDEX | NULL | IDX_CT_DN | 772 | NULL | 10 | 100.00 | BACKWARD INDEX SCAN | +----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+---------------------+
  • 17. Descending Index Query uses the Composite Index ( Creation Time , Display name ) No ordering is mentioned in while Indexing Query uses Backward Scan ( Explain Plan ) But Forwards scan will have 15% Performance gain
  • 18. Descending Index CREATE INDEX IDX_CT_DN_DESC ON USERS(CREATION_TIME DESC,DISPLAY_NAME DESC); QUERY OK, 0 ROWS AFFECTED (0.03 SEC) RECORDS: 0 DUPLICATES: 0 WARNINGS: 0 EXPLAIN SELECT EMAIL,DESCRIPTION FROM USERS ORDER BY CREATION_TIME DESC ,DISPLAY_NAME DESC LIMIT 10; +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-------+ | ID | SELECT_TYPE | TABLE | PARTITIONS | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | FILTERED | EXTRA | +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-------+ | 1 | SIMPLE | USERS | NULL | INDEX | NULL | IDX_CT_DN_DESC | 772 | NULL | 10 | 100.00 | NULL | +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-------+
  • 19. Descending Index ( Limitation ) Only Supports InnoDB Engine Full text is not supported They are not Buffered in Change Buffer
  • 21. Indexing for Performance ( Process followed Before ) Find the Query for optimisation Create the index ( Affects Optimiser ) Evaluate the Query Performane Retain the Index if it benefits queries Drop the Index if it is not much benefit
  • 22. Indexing for Performance ( After ) Note : Can be followed for dropping an unused index too. Find the Query for optimisation Create the index using Invisible as Keyword Turn on Switch 'use_invisible-indexes=on' ( session ) Evaluate the Query Performane now. Make the index Visible if it benefits. Drop the Index if it is not much benefit.
  • 24. Execution Plan Improvements Explain Format in Tree Explain Analyze
  • 26. Explain Format=Tree Explain Format in Tree was introduced in MySQL 8.0.16 Shows Query Plans and Cost estimations Easy understanding on internal operations Identation helps understanding the execution orders
  • 27. Explain ( Traditional / Tabular ) explain SELECT 'node' as type, node_id as id FROM node_tags WHERE k='bridge' and v='memorial' UNION SELECT 'way' as type, way_id as id FROM way_tags WHERE k='lanes' and v='cafe' UNION SELECT 'relation' as type, relation_id as id FROM relation_tags WHERE k='tunnel' and v='Barkingside'; +----+--------------+---------------+------------+------+---------------+------+---------+------+---------+----------+-----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------+---------------+------------+------+---------------+------+---------+------+---------+----------+-----------------+ | 1 | PRIMARY | node_tags | NULL | ALL | NULL | NULL | NULL | NULL | 822783 | 1.00 | Using where | | 2 | UNION | way_tags | NULL | ALL | NULL | NULL | NULL | NULL | 1347360 | 1.00 | Using where | | 3 | UNION | relation_tags | NULL | ALL | NULL | NULL | NULL | NULL | 62797 | 1.00 | Using where | |NULL| UNION RESULT | <union1,2,3> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------+---------------+------------+------+---------------+------+---------+------+---------+----------+-----------------+
  • 28. Explain Format=Tree explain format=tree SELECT 'node' as type, node_id as id FROM node_tags WHERE k='bridge' and v='memorial' UNION SELECT 'way' as type, way_id as id FROM way_tags WHERE k='lanes' and v='cafe' UNION SELECT 'relation' as type, relation_id as id FROM relation_tags WHERE k='tunnel' and v='Barkingside'G *************************** 1. row *************************** EXPLAIN: -> Table scan on <union temporary> (cost=0.01..281.61 rows=22329) -> Union materialize with deduplication (cost=227556.45..227838.05 rows=22329) -> Filter: ((node_tags.k = 'bridge') and (node_tags.v = 'memorial')) (cost=83072.55 rows=8228) -> Table scan on node_tags (cost=83072.55 rows=822783) -> Filter: ((way_tags.k = 'lanes') and (way_tags.v = 'cafe')) (cost=135899.00 rows=13474) -> Table scan on way_tags (cost=135899.00 rows=1347360) -> Filter: ((relation_tags.k = 'tunnel') and (relation_tags.v = 'Barkingside')) (cost=6351.95 rows=628) -> Table scan on relation_tags (cost=6351.95 rows=62797)
  • 30. Explain Analyze Explain Analyse was introduced in MySQL 8.0.18 Actual Time spent on each Iterator. Time to fetch first row Time to fetch all rows Better than Optimiser Trace
  • 31. Explain Analyze explain analyze SELECT 'node' as type, node_id as id FROM node_tags WHERE k='bridge' and v='memorial' UNION SELECT 'way' as type, way_id as id FROM way_tags WHERE k='lanes' and v='cafe' UNION SELECT 'relation' as type, relation_id as id FROM relation_tags WHERE k='tunnel' and v='Barkingside'G *************************** 1. row *************************** EXPLAIN: -> Table scan on <union temporary> (cost=0.01..281.61 rows=22329) (actual time=0.002..0.002 rows=0 loops=1) -> Union materialize with deduplication (cost=227556.45..227838.05 rows=22329) (actual time=636.728..636.728 rows=0 loops=1) -> Filter: ((node_tags.k = 'bridge') and (node_tags.v = 'memorial')) (cost=83072.55 rows=8228) (actual time=233.393..233.393 rows=0 loops=1) -> Table scan on node_tags (cost=83072.55 rows=822783) (actual time=0.039..182.413 rows=833423 loops=1) -> Filter: ((way_tags.k = 'lanes') and (way_tags.v = 'cafe')) (cost=135899.00 rows=13474) (actual time=385.278..385.278 rows=0 loops=1) -> Table scan on way_tags (cost=135899.00 rows=1347360) (actual time=0.039..299.565 rows=1343477 loops=1) -> Filter: ((relation_tags.k = 'tunnel') and (relation_tags.v = 'Barkingside')) (cost=6351.95 rows=628) (actual time=18.046..18.046 rows=0 loops=1) -> Table scan on relation_tags (cost=6351.95 rows=62797) (actual time=0.039..14.101 rows=63220 loops=1)
  • 32. Explain Analyze Explain Tree ( Cost ) Analyze (time in ms) Primary 83072 233 UNION 135899 385 UNION 6352 18  UNION Result 227556  636  Note : Analyze result is 15-30% slower than actual time
  • 34. Optimiser Improvements Optimiser is the brain of any RDBMS , better algorithms and better statistics will make its intelligence better. Histogram Hash Joins
  • 36. Histogram Syntax : analyze table table_name update/drop histogram with N buckets ; The Data distribution is not uniform Histogram helps in Better stat to DB. MySQL has Histogram from 8.0.3 ( before GA ) Histogram can be applied for single column / multi columns.
  • 38. Histogram (Before) Optimizer Guesstimate the row filtering is common
  • 39. Histogram (After) Row filtering has improved post Histogram
  • 40. Histogram Histograms are better than indexes at cases. Lesser maintenance over head. Controlled by condition_fanout_filter. 1024 is the maximum buckets allowed. Histogram stats for table can be visualised on COLUMN_STATISTICS ( IS table ).
  • 43. Hash Join MySQL Support Hash Join from 8.0.18 Improved further in 8.0.20 ( Left Join ) HASH Join algorithm help in better Join Optimisation than BNL. Equi Joins are much benefitted BNL Support was removed from MySQL 8.0.20
  • 44. Hash Join In memory Hash table Join Buffer Size Prevents over flow Optimiser switch hash_join=ON (can't be disabled )
  • 45. Other Optimisation Perfer_orderding_index sub_query_to-derived CTE ( Common Table expression ) Lateral Derived Tables Window Function Switches
  • 46. Reach Us : Info@mydbops.com Thank You