SlideShare a Scribd company logo
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?
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
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
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
My MySQL SQL Presentation
Finding bad indexes
Slow query log
Slow query log
Slow query log
Explain
Explain
Explain

More Related Content

PPTX
Introduction to lists
PPTX
Dictionaries
PPTX
PA1 lists
PPTX
PPTX
Lists and loops
PPTX
Intro to digests
PPTX
Lists in Python
PPTX
Lesson3#Scientific Research Skills
Introduction to lists
Dictionaries
PA1 lists
Lists and loops
Intro to digests
Lists in Python
Lesson3#Scientific Research Skills

Viewers also liked (8)

PPT
PPT
Mysql Indexing
PPTX
View, Store Procedure & Function and Trigger in MySQL - Thaipt
PDF
MySQL: Indexing for Better Performance
ODP
Knowledgebase vs Database
PPTX
Group decision support systems (gdss)
PPT
MySql slides (ppt)
PDF
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Mysql Indexing
View, Store Procedure & Function and Trigger in MySQL - Thaipt
MySQL: Indexing for Better Performance
Knowledgebase vs Database
Group decision support systems (gdss)
MySql slides (ppt)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Ad

Similar to My MySQL SQL Presentation (20)

PDF
MySQL Indexing
PDF
Mysql Optimization
PPTX
MySQL Indexes
PPTX
Optimizing MySQL queries
PDF
High Performance Mysql - Friday Tech Talks at Squareboat
PPT
Indexing
PDF
Introduction to Databases - query optimizations for MySQL
PPTX
PHP UK 2020 Tutorial: MySQL Indexes, Histograms And other ways To Speed Up Yo...
PDF
Advanced MySQL Query Optimizations
PPTX
MySQL Indexing - Best practices for MySQL 5.6
PDF
MySQL INDEXES
PDF
Covering indexes
PDF
MySQL Query Optimisation 101
PDF
Need for Speed: MySQL Indexing
PPTX
Работа с индексами - лучшие практики для MySQL 5.6, Петр Зайцев (Percona)
PDF
Mysql query optimization
PDF
MySQL Performance Optimization
PDF
Are You Getting the Best of your MySQL Indexes
PDF
Introduction to MySQL Query Tuning for Dev[Op]s
PPTX
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
MySQL Indexing
Mysql Optimization
MySQL Indexes
Optimizing MySQL queries
High Performance Mysql - Friday Tech Talks at Squareboat
Indexing
Introduction to Databases - query optimizations for MySQL
PHP UK 2020 Tutorial: MySQL Indexes, Histograms And other ways To Speed Up Yo...
Advanced MySQL Query Optimizations
MySQL Indexing - Best practices for MySQL 5.6
MySQL INDEXES
Covering indexes
MySQL Query Optimisation 101
Need for Speed: MySQL Indexing
Работа с индексами - лучшие практики для MySQL 5.6, Петр Зайцев (Percona)
Mysql query optimization
MySQL Performance Optimization
Are You Getting the Best of your MySQL Indexes
Introduction to MySQL Query Tuning for Dev[Op]s
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Ad

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Machine Learning_overview_presentation.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
Machine learning based COVID-19 study performance prediction
Machine Learning_overview_presentation.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
sap open course for s4hana steps from ECC to s4
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25-Week II
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx

My MySQL SQL Presentation