SlideShare a Scribd company logo
High Performance
MySQL
● Normalization of Schema.
● Indexes of Schema.
Schema optimization
B-Tree Indexes
CREATE TABLE PEOPLE (
Last_name varchar(50) NOT null
first_name varchar(50) NOT null
dob varchar(50) NOT null
gender enum (50) NOT null
Key( last_name,first_name,dob)
Allen
Cuba
1960-01-0
1
Astaire
Angelia
1980-03-04
Barrymore
julia
2000-05-16
akroyd
christan
1958-12-07
akroyd
Debbie
1990-03-18
Allen
Cuba
1960-01-01
Allen
Kim
1930-07-12
Barrymore
Julia
2000-05-16
Basinger
Viven
1976-12-08
Type of Indexes
Types of Queries that can use a B-Tree
Index
●
●
●
●
●
●
Limitations
●
●
Indexed key(‘last_name’, ’first_name’, ’dob’)
Hash Indexes
●
●
●
fname lname
Baron Lentz
Peter Schwartz
Vadim Zaitsev
Arjen Tkachenko
Ex: f(Arjen) = 2458
Slot lname
2323 Pointer to row 1
2458 Pointer to row 4
2537 Pointer to row 2
8784 Pointer to row 3
Indexing strategies for high performance
❏ Isolate the Column
● “Isolating” the column means, indexed column should not be a part of an expression or be inside a function in
the query.
● Example
Incorrect Correct
Select * from actor where action_id+1 =5 Select * from actor where action_id = 5
Select . . . where TO_DAYS( CURRENT_DATE) -
TO_DAYS( date_col) <= 10
Select . . . where date_col >=
DATE_SUB(CURRENT_DATE,INTERVAL 10 DAY
)
❏ Prefix & Suffix Index and Index selectivity
● For a column of very long length string we can use prefix of indexing.
● But what would be the length of prefix to maintain its good selectivity ?
Steps to calculate selectivity :
1. Find full column selectivity.
Ex : Select COUNT( DISTINCT(City)) / Count(*) from city_table (0.0312).
2. Now calculate selectivity of each length
Select COUNT( DISTINCT LEFT(City,3)) / Count(*) from city_table (0.0329).
Select COUNT( DISTINCT LEFT(City,4)) / Count(*) from city_table (0.0293).
Select COUNT( DISTINCT LEFT(City,7)) / Count(*) from city_table (0.0310).
City Count
London 65
Hiroshima 49
Mumbai 48
Delhi 48
Shanghai 48
Kolkata 47
Chennai 47
New York 45
7 length selectivity value is close to full column selectivity.
mysql> ALTER TABLE city Add key (city(7))
❏ Cluster Indexes
● Cluster indexing is special approach to data storage.
● Index row itself stored in the index leaf page.
● One cluster index per table.
11 21 91
Akroyd
Christian
1958-12-07
1
Akroyd
Debbie
1990-03-18
2
Allen
Cuba
1960-01-01
11
Allen
Kim
1930-07-12
12
Barrymore
Julia
2000-05-16
92
Basinger
Viven
1976-12-08
93
Advantages
●
●
Disadvantages
●
Comparison of InnoDB and MyISAM data layout
MyISAM InnoDB
It store the rows on disk in order they were
inserted
It store the rows on disk in cluster organization.
Primary index in leaf node store pointer to row Primary index in leaf node store row itself.
Secondary index in leaf node store pointer to
row.
Secondary index in leaf node store primary key
of row.
High Performance Mysql - Friday Tech Talks at Squareboat
❏ Covering Indexes
● Mysql use this index to retrieve a column data instead of row data.
● It is mostly used in InnoDB data layout
● It can dramatically improve the performance.
Example : let a comments table indexed with (post_id)
Without using covering Index With using covering index
Select * from comments where post_id =’1’
limit 10000,10
Select * from comments (Select id from
comments where post_id =’1’ limit
10000,10 ) t1 ,comments t2 where t1.id=
t2.id
Speeding Up ALter Table
How Alter table actually perform ?
●
●
Modifying the .frm file
Query Optimisation and Performance
Retrieving more
rows than you
need
Accessing more
column than you
need.
Analyze a poorly performing query
Query cost metrics :
●
●
Ways To Restructure Queries
❏ Complex vs simple queries
●
●
❏ Chopping Up a query
●
●
❏ Joins decomposition
> SELECT * FROM tag
JOIN tag_post ON tag_post.tag_id = tag.id
JOIN post ON tag_post.post_id = post.id
Where tag.tag =’mysql’
> SELECT * FROM tag WHERE tag =’’mysql’
> SELECT * FROM tag_post WHERE tag_id = 1234
> SELECT * FROM post WHERE post_id IN (123,456).
Query Execution Basics
Query Optimization Process
❏ Parser and the preprocessor
●
●
❏ Query Optimizer
●
●
●
●
MySQL join execution strategy
●
mysql> SELECT tbl1.col1, tbl2.col2
FROM tbl1 INNER JOIN tbl2
USING(col3) where tbl1.col1 IN (5,6)
outer_iter = iterator over tbl1 where col1 in (5,6)
outer _row = outer_iter.next
while outer_row
inner_iter = iterator over tbl2 where col3 =outer_row.col3
inner_row = inner_iter.next
while inner_row
output [outer_row.col1,inner_row.col2 ]
inner_row = inner_iter.next
end
outer_row = inner_iter.next
end
❏ Join Optimizer
Execution 1 Execution 2
TABLE actor :200 rows TABLE film : 951 rows
TABLE film_actor :1 row TABLE film_actor :1 row
TABLE film :1 row TABLE actor :1 row
actor film_actor film film film_actor actor
Limitation of MySQL Query Optimizer
❏ Correlated Subqueries :
How we think MySQl execute it:
But what optimizer actually do
film_actor.film_id = film_actor.film_id
❏ WildCards
●
●
❏ Push predicates into the OUTER JOIN
●
●
❏ Duplicate Constant Condition in JOIN
●
●
❏ Avoid sorting on 2 column of different table in JOIN
THANK
YOU

More Related Content

What's hot (18)

PDF
Numpy Meetup 07/02/2013
Francesco
 
PDF
Tile Menu Using Datawindow Object
zulmach .
 
PPTX
R seminar dplyr package
Muhammad Nabi Ahmad
 
PDF
Numpy python cheat_sheet
Nishant Upadhyay
 
PDF
Pandas Cheat Sheet
ACASH1011
 
PPTX
Cluto presentation
Roseline Antai
 
ODP
Mysql
merlin deepika
 
PDF
Metaprogramming
Dmitri Nesteruk
 
PPTX
Functional Programming, simplified
Naveenkumar Muguda
 
PDF
Data manipulation on r
Abhik Seal
 
PDF
Introduction to data.table in R
Paul Richards
 
PDF
Rsplit apply combine
Michelle Darling
 
PDF
Python matplotlib cheat_sheet
Nishant Upadhyay
 
PDF
NumPy Refresher
Lukasz Dobrzanski
 
PDF
Scientific Computing with Python - NumPy | WeiYuan
Wei-Yuan Chang
 
PPTX
ABAP 7.x New Features and Commands
Dr. Kerem Koseoglu
 
PDF
Data Manipulation Using R (& dplyr)
Ram Narasimhan
 
Numpy Meetup 07/02/2013
Francesco
 
Tile Menu Using Datawindow Object
zulmach .
 
R seminar dplyr package
Muhammad Nabi Ahmad
 
Numpy python cheat_sheet
Nishant Upadhyay
 
Pandas Cheat Sheet
ACASH1011
 
Cluto presentation
Roseline Antai
 
Metaprogramming
Dmitri Nesteruk
 
Functional Programming, simplified
Naveenkumar Muguda
 
Data manipulation on r
Abhik Seal
 
Introduction to data.table in R
Paul Richards
 
Rsplit apply combine
Michelle Darling
 
Python matplotlib cheat_sheet
Nishant Upadhyay
 
NumPy Refresher
Lukasz Dobrzanski
 
Scientific Computing with Python - NumPy | WeiYuan
Wei-Yuan Chang
 
ABAP 7.x New Features and Commands
Dr. Kerem Koseoglu
 
Data Manipulation Using R (& dplyr)
Ram Narasimhan
 

Similar to High Performance Mysql - Friday Tech Talks at Squareboat (20)

PPTX
MySQL performance tuning
Anurag Srivastava
 
PDF
Why Use EXPLAIN FORMAT=JSON?
Sveta Smirnova
 
PPT
Structure query language - Data definition language.ppt
munmunitjusl
 
PDF
Introduction into MySQL Query Tuning
Sveta Smirnova
 
PDF
Covering indexes
MYXPLAIN
 
PDF
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
anandology
 
PDF
SQL: Query optimization in practice
Jano Suchal
 
PDF
MySQL Query Optimisation 101
Federico Razzoli
 
PPT
How to leave the ORM at home and write SQL
MariaDB plc
 
PDF
MySQL Query tuning 101
Sveta Smirnova
 
PDF
My sql查询优化实践
ghostsun
 
PPTX
MYSQL single rowfunc-multirowfunc-groupby-having
Ahmed Farag
 
PDF
MySQL for beginners
Saeid Zebardast
 
PDF
Efficient Use of indexes in MySQL
Aleksandr Kuzminsky
 
PPTX
Dun ddd
Lyuben Todorov
 
PDF
PostgreSQL 9.5 Features
Saiful
 
PDF
Introduction to MySQL Query Tuning for Dev[Op]s
Sveta Smirnova
 
PPTX
Sql
Rahul Singh
 
PPTX
Apache Cassandra Data Modeling with Travis Price
DataStax Academy
 
PPTX
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 
MySQL performance tuning
Anurag Srivastava
 
Why Use EXPLAIN FORMAT=JSON?
Sveta Smirnova
 
Structure query language - Data definition language.ppt
munmunitjusl
 
Introduction into MySQL Query Tuning
Sveta Smirnova
 
Covering indexes
MYXPLAIN
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
anandology
 
SQL: Query optimization in practice
Jano Suchal
 
MySQL Query Optimisation 101
Federico Razzoli
 
How to leave the ORM at home and write SQL
MariaDB plc
 
MySQL Query tuning 101
Sveta Smirnova
 
My sql查询优化实践
ghostsun
 
MYSQL single rowfunc-multirowfunc-groupby-having
Ahmed Farag
 
MySQL for beginners
Saeid Zebardast
 
Efficient Use of indexes in MySQL
Aleksandr Kuzminsky
 
PostgreSQL 9.5 Features
Saiful
 
Introduction to MySQL Query Tuning for Dev[Op]s
Sveta Smirnova
 
Apache Cassandra Data Modeling with Travis Price
DataStax Academy
 
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 
Ad

More from Squareboat (20)

PDF
Squareboat Deck
Squareboat
 
PDF
Squareboat Branding Proposal
Squareboat
 
PDF
Squareboat Product Foundation Process
Squareboat
 
PDF
Squareboat Culture Deck
Squareboat
 
PDF
Squareboat Design Portfolio
Squareboat
 
PDF
Squareboat Crew Deck
Squareboat
 
PDF
CTA - Call to Attention
Squareboat
 
PDF
Tech talk on Tailwind CSS
Squareboat
 
PDF
What’s New in Apple World - WWDC19
Squareboat
 
PDF
Tech Talk on Microservices at Squareboat
Squareboat
 
PDF
Building Alexa Skills
Squareboat
 
PDF
Making Products to get users “Hooked”
Squareboat
 
PDF
Moving to Docker... Finally!
Squareboat
 
PDF
Color Theory
Squareboat
 
PDF
Continuous Delivery process
Squareboat
 
PDF
HTML and CSS architecture for 2025
Squareboat
 
PDF
Vue JS
Squareboat
 
PDF
The rise of Conversational User Interfaces
Squareboat
 
PDF
Thinking of growth as a feature
Squareboat
 
PDF
REST vs GraphQL
Squareboat
 
Squareboat Deck
Squareboat
 
Squareboat Branding Proposal
Squareboat
 
Squareboat Product Foundation Process
Squareboat
 
Squareboat Culture Deck
Squareboat
 
Squareboat Design Portfolio
Squareboat
 
Squareboat Crew Deck
Squareboat
 
CTA - Call to Attention
Squareboat
 
Tech talk on Tailwind CSS
Squareboat
 
What’s New in Apple World - WWDC19
Squareboat
 
Tech Talk on Microservices at Squareboat
Squareboat
 
Building Alexa Skills
Squareboat
 
Making Products to get users “Hooked”
Squareboat
 
Moving to Docker... Finally!
Squareboat
 
Color Theory
Squareboat
 
Continuous Delivery process
Squareboat
 
HTML and CSS architecture for 2025
Squareboat
 
Vue JS
Squareboat
 
The rise of Conversational User Interfaces
Squareboat
 
Thinking of growth as a feature
Squareboat
 
REST vs GraphQL
Squareboat
 
Ad

Recently uploaded (20)

PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 

High Performance Mysql - Friday Tech Talks at Squareboat

  • 2. ● Normalization of Schema. ● Indexes of Schema. Schema optimization
  • 3. B-Tree Indexes CREATE TABLE PEOPLE ( Last_name varchar(50) NOT null first_name varchar(50) NOT null dob varchar(50) NOT null gender enum (50) NOT null Key( last_name,first_name,dob) Allen Cuba 1960-01-0 1 Astaire Angelia 1980-03-04 Barrymore julia 2000-05-16 akroyd christan 1958-12-07 akroyd Debbie 1990-03-18 Allen Cuba 1960-01-01 Allen Kim 1930-07-12 Barrymore Julia 2000-05-16 Basinger Viven 1976-12-08 Type of Indexes
  • 4. Types of Queries that can use a B-Tree Index ● ● ● ● ● ● Limitations ● ● Indexed key(‘last_name’, ’first_name’, ’dob’)
  • 5. Hash Indexes ● ● ● fname lname Baron Lentz Peter Schwartz Vadim Zaitsev Arjen Tkachenko Ex: f(Arjen) = 2458 Slot lname 2323 Pointer to row 1 2458 Pointer to row 4 2537 Pointer to row 2 8784 Pointer to row 3
  • 6. Indexing strategies for high performance ❏ Isolate the Column ● “Isolating” the column means, indexed column should not be a part of an expression or be inside a function in the query. ● Example Incorrect Correct Select * from actor where action_id+1 =5 Select * from actor where action_id = 5 Select . . . where TO_DAYS( CURRENT_DATE) - TO_DAYS( date_col) <= 10 Select . . . where date_col >= DATE_SUB(CURRENT_DATE,INTERVAL 10 DAY )
  • 7. ❏ Prefix & Suffix Index and Index selectivity ● For a column of very long length string we can use prefix of indexing. ● But what would be the length of prefix to maintain its good selectivity ? Steps to calculate selectivity : 1. Find full column selectivity. Ex : Select COUNT( DISTINCT(City)) / Count(*) from city_table (0.0312). 2. Now calculate selectivity of each length Select COUNT( DISTINCT LEFT(City,3)) / Count(*) from city_table (0.0329). Select COUNT( DISTINCT LEFT(City,4)) / Count(*) from city_table (0.0293). Select COUNT( DISTINCT LEFT(City,7)) / Count(*) from city_table (0.0310). City Count London 65 Hiroshima 49 Mumbai 48 Delhi 48 Shanghai 48 Kolkata 47 Chennai 47 New York 45 7 length selectivity value is close to full column selectivity. mysql> ALTER TABLE city Add key (city(7))
  • 8. ❏ Cluster Indexes ● Cluster indexing is special approach to data storage. ● Index row itself stored in the index leaf page. ● One cluster index per table. 11 21 91 Akroyd Christian 1958-12-07 1 Akroyd Debbie 1990-03-18 2 Allen Cuba 1960-01-01 11 Allen Kim 1930-07-12 12 Barrymore Julia 2000-05-16 92 Basinger Viven 1976-12-08 93 Advantages ● ● Disadvantages ●
  • 9. Comparison of InnoDB and MyISAM data layout MyISAM InnoDB It store the rows on disk in order they were inserted It store the rows on disk in cluster organization. Primary index in leaf node store pointer to row Primary index in leaf node store row itself. Secondary index in leaf node store pointer to row. Secondary index in leaf node store primary key of row.
  • 11. ❏ Covering Indexes ● Mysql use this index to retrieve a column data instead of row data. ● It is mostly used in InnoDB data layout ● It can dramatically improve the performance. Example : let a comments table indexed with (post_id) Without using covering Index With using covering index Select * from comments where post_id =’1’ limit 10000,10 Select * from comments (Select id from comments where post_id =’1’ limit 10000,10 ) t1 ,comments t2 where t1.id= t2.id
  • 12. Speeding Up ALter Table How Alter table actually perform ? ● ● Modifying the .frm file
  • 13. Query Optimisation and Performance Retrieving more rows than you need Accessing more column than you need. Analyze a poorly performing query Query cost metrics : ● ●
  • 14. Ways To Restructure Queries ❏ Complex vs simple queries ● ● ❏ Chopping Up a query ● ● ❏ Joins decomposition > SELECT * FROM tag JOIN tag_post ON tag_post.tag_id = tag.id JOIN post ON tag_post.post_id = post.id Where tag.tag =’mysql’ > SELECT * FROM tag WHERE tag =’’mysql’ > SELECT * FROM tag_post WHERE tag_id = 1234 > SELECT * FROM post WHERE post_id IN (123,456).
  • 16. Query Optimization Process ❏ Parser and the preprocessor ● ● ❏ Query Optimizer ● ● ● ●
  • 17. MySQL join execution strategy ● mysql> SELECT tbl1.col1, tbl2.col2 FROM tbl1 INNER JOIN tbl2 USING(col3) where tbl1.col1 IN (5,6) outer_iter = iterator over tbl1 where col1 in (5,6) outer _row = outer_iter.next while outer_row inner_iter = iterator over tbl2 where col3 =outer_row.col3 inner_row = inner_iter.next while inner_row output [outer_row.col1,inner_row.col2 ] inner_row = inner_iter.next end outer_row = inner_iter.next end
  • 18. ❏ Join Optimizer Execution 1 Execution 2 TABLE actor :200 rows TABLE film : 951 rows TABLE film_actor :1 row TABLE film_actor :1 row TABLE film :1 row TABLE actor :1 row actor film_actor film film film_actor actor
  • 19. Limitation of MySQL Query Optimizer ❏ Correlated Subqueries : How we think MySQl execute it: But what optimizer actually do film_actor.film_id = film_actor.film_id
  • 20. ❏ WildCards ● ● ❏ Push predicates into the OUTER JOIN ● ●
  • 21. ❏ Duplicate Constant Condition in JOIN ● ● ❏ Avoid sorting on 2 column of different table in JOIN