SlideShare a Scribd company logo
1 of 77
Download to read offline
Indexing
with MySQL
Most of our data lives in MySQL

We want to get it as efficiently as possible

How?
Small tables?
Small tables? Not always practical
Small tables? Not always practical
Indexes?
Small tables? Not always practical
Indexes? OK, but how?
First things first:
First things first:
What are indexes used for?
How do indexes work?
How do indexes work?
Storage Types
BTree
BTree
Ordered key-value map
BTree
Ordered key-value map
Most common storage type
BTree
Ordered key-value map
Most common storage type
Quickly find a given key and can be scanned in
order
BTree
Ordered key-value map
Most common storage type
Quickly find a given key and can be scanned in
order
Works well with ranges
All records between 50 and 100
All records starting with 'R'
Other storage types
Other storage types
RTree / Spatial Index
- Identify 'close' values in 2+ dimensions
- Useful for geographic databases
Other storage types
RTree / Spatial Index
- Identify 'close' values in 2+ dimensions
- Useful for geographic databases
Hash
- Unordered key/value map
- Faster than BTree, but terrible for ranges
How do indexes work?
Pointers
Pointers
Pointers
1

Charles

Mata

Engineer

2

Aaron

Macy

Engineer

3

Kevin

Clement

QA

4

Jeremy

Tan

Manager
Pointers
1

Charles

Mata

Engineer

2

Aaron

Macy

Engineer

3

Kevin

Clement

QA

4

Jeremy

Tan

Manager

Aaron

2

Charles

1

Jeremy

4

Kevin

3
Pointers
1

Charles

Mata

Engineer

2

Aaron

Macy

Engineer

3

Kevin

Clement

QA

4

Jeremy

Tan

Manager

Aaron

2

Charles

1

Jeremy

4

Kevin

3
Index Types
Single column
Single column
Composite (multiple column)
Composite (multiple column)
Covering
Covering
Covering
Partial
Partial
Partial
Downsides?
Indexes take up

SPACE
Indexes slow down

INSERTS
Indexes confuse

OPTIMIZATION
Selectivity
Selectivity of a column is
the ratio between the number of
distinct values and the
number of total values
Selectivity of a column is
the ratio between the number of
distinct values and the
number of total values

Primary Keys and Unique Columns
Always Have Selectivity of 1
Selectivity Tips
Selectivity Tips
Always aim for >15%
Selectivity Tips
Always aim for >15%
Joins on columns with low selectivity are
expensive
Selectivity Tips
Always aim for >15%
Joins on columns with low selectivity are
expensive
Watch out for columns like `status`, `gender`,
and `active`
What makes a good index?
Small
Use smallest data type possible
Consider partial indexes on varchar/char to
increase selectivity
Filter Columns
Try to identify columns that will be used to
filter data
Join Columns
Identify columns that will often be joined
on/to from other tables
Covering Indexes
A query that can use a covering index will
be substantially faster than one that has to
read from the table
Group/Sort
Identify columns that will be used for
grouping and sorting
Redundant Indexes
MySQL cannot use an index
if the columns do not form
a leftmost prefix of the index
Finding bad indexes
Slow query log
Slow query log
Slow query log
Explain
Explain
Explain

More Related Content

Viewers also liked

View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - Thaipt
Framgia Vietnam
 

Viewers also liked (8)

MySQL Views
MySQL ViewsMySQL Views
MySQL Views
 
Mysql Indexing
Mysql IndexingMysql Indexing
Mysql Indexing
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - Thaipt
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
 
Knowledgebase vs Database
Knowledgebase vs DatabaseKnowledgebase vs Database
Knowledgebase vs Database
 
Group decision support systems (gdss)
Group decision support systems (gdss)Group decision support systems (gdss)
Group decision support systems (gdss)
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
 

Similar to My MySQL SQL Presentation

09c-DataStructuresListsArrays.ppt
09c-DataStructuresListsArrays.ppt09c-DataStructuresListsArrays.ppt
09c-DataStructuresListsArrays.ppt
NagarajuNaveena1
 
Sql and mysql database concepts
Sql and mysql database conceptsSql and mysql database concepts
Sql and mysql database concepts
Selamawit Feleke
 
Intro to tsql unit 4
Intro to tsql   unit 4Intro to tsql   unit 4
Intro to tsql unit 4
Syed Asrarali
 

Similar to My MySQL SQL Presentation (20)

dotnetMALAGA - Sql query tuning guidelines
dotnetMALAGA - Sql query tuning guidelinesdotnetMALAGA - Sql query tuning guidelines
dotnetMALAGA - Sql query tuning guidelines
 
Intro to Data warehousing lecture 12
Intro to Data warehousing   lecture 12Intro to Data warehousing   lecture 12
Intro to Data warehousing lecture 12
 
Indexing
IndexingIndexing
Indexing
 
SQL Server 2012 Best Practices
SQL Server 2012 Best PracticesSQL Server 2012 Best Practices
SQL Server 2012 Best Practices
 
Introduction to Data Warehousing
Introduction to Data WarehousingIntroduction to Data Warehousing
Introduction to Data Warehousing
 
Geek Sync | Understand Indexes to Write Better Queries
Geek Sync | Understand Indexes to Write Better QueriesGeek Sync | Understand Indexes to Write Better Queries
Geek Sync | Understand Indexes to Write Better Queries
 
Moyez Dreamforce 2017 presentation on Large Data Volumes in Salesforce
Moyez Dreamforce 2017 presentation on Large Data Volumes in SalesforceMoyez Dreamforce 2017 presentation on Large Data Volumes in Salesforce
Moyez Dreamforce 2017 presentation on Large Data Volumes in Salesforce
 
PPT_DATA STRUCTURE.ppt
PPT_DATA STRUCTURE.pptPPT_DATA STRUCTURE.ppt
PPT_DATA STRUCTURE.ppt
 
Working with Large Lists in SharePoint 2013 - Why You Need to Pay Attention t...
Working with Large Lists in SharePoint 2013 - Why You Need to Pay Attention t...Working with Large Lists in SharePoint 2013 - Why You Need to Pay Attention t...
Working with Large Lists in SharePoint 2013 - Why You Need to Pay Attention t...
 
09c-DataStructuresListsArrays.ppt
09c-DataStructuresListsArrays.ppt09c-DataStructuresListsArrays.ppt
09c-DataStructuresListsArrays.ppt
 
Data Structures
Data StructuresData Structures
Data Structures
 
09c-DataStructuresListsArrays.ppt
09c-DataStructuresListsArrays.ppt09c-DataStructuresListsArrays.ppt
09c-DataStructuresListsArrays.ppt
 
DATABASE-1.pptx
DATABASE-1.pptxDATABASE-1.pptx
DATABASE-1.pptx
 
Star schema
Star schemaStar schema
Star schema
 
Sql and mysql database concepts
Sql and mysql database conceptsSql and mysql database concepts
Sql and mysql database concepts
 
1. access
1. access1. access
1. access
 
data structures
data structuresdata structures
data structures
 
Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
 
Intro To TSQL - Unit 4
Intro To TSQL - Unit 4Intro To TSQL - Unit 4
Intro To TSQL - Unit 4
 
Intro to tsql unit 4
Intro to tsql   unit 4Intro to tsql   unit 4
Intro to tsql unit 4
 

Recently uploaded

Recently uploaded (20)

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
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, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
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...
 
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...
 
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
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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...
 

My MySQL SQL Presentation