SlideShare a Scribd company logo
1 of 23
Download to read offline
Indexes in MySQL
Aleksandr Kuzminsky
https://twindb.com
How to use indexes efficiently
Agenda
1. How data is organized
2. Data access
Who we are
Aleks:
● TwinDB co-founder
● Dropbox DBA
● ex-Percona consultant
Ovais:
● TwinDB co-founder
● Lithium lead DBA
● ex-Percona consultant
How data is organized
Table in MySQL (InnoDB)
CREATE TABLE `actor`(
`actor_id` SMALLINT(5) UNSIGNED NOT NULL,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`last_update` TIMESTAMP NOT NULL,
PRIMARY KEY (`actor_id`),
KEY `idx_actor_last_name` (`last_name`)
) ENGINE=InnoDB;
B+ Tree
● O(log(n))
● Shallow
● Data in leaf pages
sakila.actor
PRIMARY idx_actor_last_name
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-15 04:34:33
2 NICK WAHLBERG 2006-02-15 04:34:33
3 ED CHASE 2006-02-15 04:34:33
4 JENNIFER DAVIS 2006-02-15 04:34:33
5 JOHNNY WOOD 2006-02-15 04:34:33
... ... ... ...
last_name actor_id
AKROYD 58
AKROYD 92
AKROYD 182
ALLEN 118
ALLEN 145
... ...
Data Access
Fast if
accessing table
and
producing result
is
simultaneous
Point SELECT
SELECT * FROM actor WHERE actor_id = 3;
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-15 04:34:33
2 NICK WAHLBERG 2006-02-15 04:34:33
3 ED CHASE 2006-02-15 04:34:33
4 JENNIFER DAVIS 2006-02-15 04:34:33
5 JOHNNY WOOD 2006-02-15 04:34:33
... ... ... ...
SELECT by range of keys
SELECT * FROM actor WHERE actor_id > 3;
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-15 04:34:33
2 NICK WAHLBERG 2006-02-15 04:34:33
3 ED CHASE 2006-02-15 04:34:33
4 JENNIFER DAVIS 2006-02-15 04:34:33
5 JOHNNY WOOD 2006-02-15 04:34:33
... ... ... ...
Lookup by secondary key
actor_id first_name last_name last_update
117 RENEE TRACY 2006-02-15 04:34:33
118 CUBA ALLEN 2006-02-15 04:34:33
119 WARREN JACKMAN 2006-02-15 04:34:33
... ... ... ...
145 KIM ALLEN 2006-02-15 04:34:33
... ... ... ...
last_name actor_id
AKROYD 58
AKROYD 92
AKROYD 182
ALLEN 118
ALLEN 145
... ...
SELECT * FROM actor WHERE last_name = ‘ALLEN’;
Step 1 Step 2
Using index for data access
last_name actor_id
AKROYD 182
ALLEN 118
ALLEN 145
ALLEN 194
ASTAIRE 76
... ...
SELECT COUNT(*) FROM actor WHERE last_name = ‘ALLEN’;
Using index for data access
EXPLAIN SELECT COUNT(*) FROM actor WHERE last_name = ‘ALLEN’;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: ref
possible_keys: idx_actor_last_name
key: idx_actor_last_name
key_len: 137
ref: const
rows: 3
Extra: Using where; Using index
Covering indexes
ALTER TABLE actor ADD INDEX idx_last_first(last_name, first_name);
SELECT first_name FROM actor WHERE last_name = 'ALLEN'
last_name first_name actor_id
AKROYD KIRSTEN 182
ALLEN CUBA 118
ALLEN KIM 145
ALLEN MERYL 194
ASTAIRE ANGELINA 76
... ...
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: ref
possible_keys: idx_actor_last_name,
idx_last_first
key: idx_last_first
key_len: 137
ref: const
rows: 3
Extra: Using where; Using index
DISTINCT
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: index
possible_keys: idx_actor_last_name,
idx_last_first
key: idx_actor_last_name
key_len: 137
ref: NULL
rows: 200
Extra: Using index
last_name actor_id
AKROYD 182
ALLEN 118
ALLEN 145
ALLEN 194
ASTAIRE 76
... ...
SELECT DISTINCT last_name FROM actor
GROUP BY
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: index
possible_keys: idx_actor_last_name,idx_last_first
key: idx_actor_last_name
key_len: 137
ref: NULL
rows: 200
Extra: Using index
last_name actor_id
AKROYD 182
ALLEN 118
ALLEN 145
ALLEN 194
ASTAIRE 76
... ...
SELECT last_name, COUNT(*) FROM actor GROUP BY last_name
Loose index scan
ALTER TABLE actor ADD COLUMN rank INT;
UPDATE actor SET rank = ROUND(100 * RAND());
ALTER TABLE actor ADD INDEX idx_last_rank (last_name, rank);
last_name rank actor_id
AKROYD 40 58
AKROYD 42 92
AKROYD 95 182
ALLEN 19 194
ALLEN 35 118
... ...
Loose index scan
SELECT last_name, MIN(rank) FROM actor GROUP BY last_name
last_name rank actor_id
AKROYD 40 58
AKROYD 42 92
AKROYD 95 182
ALLEN 19 194
ALLEN 35 118
... ...
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: range
possible_keys: …, idx_last_rank
key: idx_last_rank
key_len: 137
ref: NULL
rows: 247
Extra: Using index for group-by
Sorting
SELECT * FROM actor WHERE last_name = 'AKROYD' ORDER BY rank
last_name rank actor_id
AKROYD 40 58
AKROYD 42 92
AKROYD 95 182
ALLEN 19 194
ALLEN 35 118
... ...
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: ref
possible_keys: …, idx_last_rank
key: idx_last_rank
key_len: 137
ref: const
rows: 3
Extra: Using where
Joining tables
SELECT title, first_name, last_name
FROM film
JOIN film_actor
ON film_actor.film_id = film.film_id
JOIN actor
ON actor.actor_id = film_actor.actor_id
ORDER BY title;
Joining tables
SELECT title, first_name, last_name
FROM film FORCE INDEX (`idx_title`)
JOIN film_actor
ON film_actor.film_id = film.film_id
JOIN actor
ON actor.actor_id = film_actor.actor_id
ORDER BY title;
How to compare efficiency
Q&A
Thank you!

More Related Content

What's hot

Recovery of lost or corrupted inno db tables(mysql uc 2010)
Recovery of lost or corrupted inno db tables(mysql uc 2010)Recovery of lost or corrupted inno db tables(mysql uc 2010)
Recovery of lost or corrupted inno db tables(mysql uc 2010)Aleksandr Kuzminsky
 
B+Tree Indexes and InnoDB
B+Tree Indexes and InnoDBB+Tree Indexes and InnoDB
B+Tree Indexes and InnoDBOvais Tariq
 
Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres MonitoringDenish Patel
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HAharoonm
 
Streaming replication in practice
Streaming replication in practiceStreaming replication in practice
Streaming replication in practiceAlexey Lesovsky
 
JSONB型でpostgresをNoSQLっぽく使う
JSONB型でpostgresをNoSQLっぽく使うJSONB型でpostgresをNoSQLっぽく使う
JSONB型でpostgresをNoSQLっぽく使うYuki Takeichi
 
まずやっとくPostgreSQLチューニング
まずやっとくPostgreSQLチューニングまずやっとくPostgreSQLチューニング
まずやっとくPostgreSQLチューニングKosuke Kida
 
PostgreSQL運用管理入門
PostgreSQL運用管理入門PostgreSQL運用管理入門
PostgreSQL運用管理入門Yoshiyuki Asaba
 
Database on Kubernetes - HA,Replication and more -
Database on Kubernetes - HA,Replication and more -Database on Kubernetes - HA,Replication and more -
Database on Kubernetes - HA,Replication and more -t8kobayashi
 
EDB Failover Manager for Seamless Failover & Switchover
EDB Failover Manager for Seamless Failover & SwitchoverEDB Failover Manager for Seamless Failover & Switchover
EDB Failover Manager for Seamless Failover & SwitchoverAshnikbiz
 
InnoDB MVCC Architecture (by 권건우)
InnoDB MVCC Architecture (by 권건우)InnoDB MVCC Architecture (by 권건우)
InnoDB MVCC Architecture (by 권건우)I Goo Lee.
 
Oracle GoldenGate 18c - REST API Examples
Oracle GoldenGate 18c - REST API ExamplesOracle GoldenGate 18c - REST API Examples
Oracle GoldenGate 18c - REST API ExamplesBobby Curtis
 
MySQL・PostgreSQLだけで作る高速でリッチな全文検索システム
MySQL・PostgreSQLだけで作る高速でリッチな全文検索システムMySQL・PostgreSQLだけで作る高速でリッチな全文検索システム
MySQL・PostgreSQLだけで作る高速でリッチな全文検索システムKouhei Sutou
 
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...ScaleGrid.io
 
PostgreSQL + pgpool構成におけるリカバリ
PostgreSQL + pgpool構成におけるリカバリPostgreSQL + pgpool構成におけるリカバリ
PostgreSQL + pgpool構成におけるリカバリhiroin0
 
Database Performance Tuning
Database Performance Tuning Database Performance Tuning
Database Performance Tuning Arno Huetter
 
Microsoft Graph APIを活用した社内アプリケーション開発
Microsoft Graph APIを活用した社内アプリケーション開発Microsoft Graph APIを活用した社内アプリケーション開発
Microsoft Graph APIを活用した社内アプリケーション開発Yuki Hattori
 
EmbulkとDigdagとデータ分析基盤と
EmbulkとDigdagとデータ分析基盤とEmbulkとDigdagとデータ分析基盤と
EmbulkとDigdagとデータ分析基盤とToru Takahashi
 
OLTP+OLAP=HTAP
 OLTP+OLAP=HTAP OLTP+OLAP=HTAP
OLTP+OLAP=HTAPEDB
 
SaaS としての IDM の役割
SaaS としての IDM の役割SaaS としての IDM の役割
SaaS としての IDM の役割junichi anno
 

What's hot (20)

Recovery of lost or corrupted inno db tables(mysql uc 2010)
Recovery of lost or corrupted inno db tables(mysql uc 2010)Recovery of lost or corrupted inno db tables(mysql uc 2010)
Recovery of lost or corrupted inno db tables(mysql uc 2010)
 
B+Tree Indexes and InnoDB
B+Tree Indexes and InnoDBB+Tree Indexes and InnoDB
B+Tree Indexes and InnoDB
 
Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HA
 
Streaming replication in practice
Streaming replication in practiceStreaming replication in practice
Streaming replication in practice
 
JSONB型でpostgresをNoSQLっぽく使う
JSONB型でpostgresをNoSQLっぽく使うJSONB型でpostgresをNoSQLっぽく使う
JSONB型でpostgresをNoSQLっぽく使う
 
まずやっとくPostgreSQLチューニング
まずやっとくPostgreSQLチューニングまずやっとくPostgreSQLチューニング
まずやっとくPostgreSQLチューニング
 
PostgreSQL運用管理入門
PostgreSQL運用管理入門PostgreSQL運用管理入門
PostgreSQL運用管理入門
 
Database on Kubernetes - HA,Replication and more -
Database on Kubernetes - HA,Replication and more -Database on Kubernetes - HA,Replication and more -
Database on Kubernetes - HA,Replication and more -
 
EDB Failover Manager for Seamless Failover & Switchover
EDB Failover Manager for Seamless Failover & SwitchoverEDB Failover Manager for Seamless Failover & Switchover
EDB Failover Manager for Seamless Failover & Switchover
 
InnoDB MVCC Architecture (by 권건우)
InnoDB MVCC Architecture (by 권건우)InnoDB MVCC Architecture (by 권건우)
InnoDB MVCC Architecture (by 권건우)
 
Oracle GoldenGate 18c - REST API Examples
Oracle GoldenGate 18c - REST API ExamplesOracle GoldenGate 18c - REST API Examples
Oracle GoldenGate 18c - REST API Examples
 
MySQL・PostgreSQLだけで作る高速でリッチな全文検索システム
MySQL・PostgreSQLだけで作る高速でリッチな全文検索システムMySQL・PostgreSQLだけで作る高速でリッチな全文検索システム
MySQL・PostgreSQLだけで作る高速でリッチな全文検索システム
 
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
 
PostgreSQL + pgpool構成におけるリカバリ
PostgreSQL + pgpool構成におけるリカバリPostgreSQL + pgpool構成におけるリカバリ
PostgreSQL + pgpool構成におけるリカバリ
 
Database Performance Tuning
Database Performance Tuning Database Performance Tuning
Database Performance Tuning
 
Microsoft Graph APIを活用した社内アプリケーション開発
Microsoft Graph APIを活用した社内アプリケーション開発Microsoft Graph APIを活用した社内アプリケーション開発
Microsoft Graph APIを活用した社内アプリケーション開発
 
EmbulkとDigdagとデータ分析基盤と
EmbulkとDigdagとデータ分析基盤とEmbulkとDigdagとデータ分析基盤と
EmbulkとDigdagとデータ分析基盤と
 
OLTP+OLAP=HTAP
 OLTP+OLAP=HTAP OLTP+OLAP=HTAP
OLTP+OLAP=HTAP
 
SaaS としての IDM の役割
SaaS としての IDM の役割SaaS としての IDM の役割
SaaS としての IDM の役割
 

Similar to Efficient Use of indexes in MySQL

Covering indexes
Covering indexesCovering indexes
Covering indexesMYXPLAIN
 
High Performance Mysql - Friday Tech Talks at Squareboat
High Performance Mysql - Friday Tech Talks at SquareboatHigh Performance Mysql - Friday Tech Talks at Squareboat
High Performance Mysql - Friday Tech Talks at SquareboatSquareboat
 
Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWorkhorse Computing
 
Postgres can do THAT?
Postgres can do THAT?Postgres can do THAT?
Postgres can do THAT?alexbrasetvik
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101Sveta Smirnova
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query TuningSveta Smirnova
 
Troubleshooting MySQL Performance
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL PerformanceSveta Smirnova
 
Lean React - Patterns for High Performance [ploneconf2017]
Lean React - Patterns for High Performance [ploneconf2017]Lean React - Patterns for High Performance [ploneconf2017]
Lean React - Patterns for High Performance [ploneconf2017]Devon Bernard
 
Modify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdfModify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdfadityaenterprise32
 
Cassandra 3.0 Awesomeness
Cassandra 3.0 AwesomenessCassandra 3.0 Awesomeness
Cassandra 3.0 AwesomenessJon Haddad
 
SQL: tricks and cheats
SQL: tricks and cheatsSQL: tricks and cheats
SQL: tricks and cheatsMikalai Sitsko
 
OpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersOpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersConnor McDonald
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query TuningAlexander Rubin
 
Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with ExplainMYXPLAIN
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain ExplainedJeremy Coates
 
AI02_Python (cont.).pptx
AI02_Python (cont.).pptxAI02_Python (cont.).pptx
AI02_Python (cont.).pptxNguyễn Tiến
 

Similar to Efficient Use of indexes in MySQL (20)

Covering indexes
Covering indexesCovering indexes
Covering indexes
 
High Performance Mysql - Friday Tech Talks at Squareboat
High Performance Mysql - Friday Tech Talks at SquareboatHigh Performance Mysql - Friday Tech Talks at Squareboat
High Performance Mysql - Friday Tech Talks at Squareboat
 
Sql
SqlSql
Sql
 
Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility Modules
 
Postgres can do THAT?
Postgres can do THAT?Postgres can do THAT?
Postgres can do THAT?
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
 
Troubleshooting MySQL Performance
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL Performance
 
Lean React - Patterns for High Performance [ploneconf2017]
Lean React - Patterns for High Performance [ploneconf2017]Lean React - Patterns for High Performance [ploneconf2017]
Lean React - Patterns for High Performance [ploneconf2017]
 
Modify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdfModify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdf
 
greenDAO
greenDAOgreenDAO
greenDAO
 
Cassandra 3.0 Awesomeness
Cassandra 3.0 AwesomenessCassandra 3.0 Awesomeness
Cassandra 3.0 Awesomeness
 
SQL: tricks and cheats
SQL: tricks and cheatsSQL: tricks and cheats
SQL: tricks and cheats
 
Operator overload rr
Operator overload  rrOperator overload  rr
Operator overload rr
 
OpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersOpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developers
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
MySQL performance tuning
MySQL performance tuningMySQL performance tuning
MySQL performance tuning
 
Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with Explain
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain Explained
 
AI02_Python (cont.).pptx
AI02_Python (cont.).pptxAI02_Python (cont.).pptx
AI02_Python (cont.).pptx
 

More from Aleksandr Kuzminsky

More from Aleksandr Kuzminsky (7)

ProxySQL at Scale on AWS.pdf
ProxySQL at Scale on AWS.pdfProxySQL at Scale on AWS.pdf
ProxySQL at Scale on AWS.pdf
 
Omnibus as a Solution for Dependency Hell
Omnibus as a Solution for Dependency HellOmnibus as a Solution for Dependency Hell
Omnibus as a Solution for Dependency Hell
 
Efficient Indexes in MySQL
Efficient Indexes in MySQLEfficient Indexes in MySQL
Efficient Indexes in MySQL
 
Netstore overview
Netstore overviewNetstore overview
Netstore overview
 
Undrop for InnoDB
Undrop for InnoDBUndrop for InnoDB
Undrop for InnoDB
 
Undrop for InnoDB
Undrop for InnoDBUndrop for InnoDB
Undrop for InnoDB
 
Data recovery talk on PLUK
Data recovery talk on PLUKData recovery talk on PLUK
Data recovery talk on PLUK
 

Recently uploaded

VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...SUHANI PANDEY
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...SUHANI PANDEY
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...SUHANI PANDEY
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirtrahman018755
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...Escorts Call Girls
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...SUHANI PANDEY
 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...tanu pandey
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Servicegwenoracqe6
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...tanu pandey
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...Neha Pandey
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445ruhi
 
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceReal Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceEscorts Call Girls
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls DubaiEscorts Call Girls
 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceDelhi Call girls
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...SUHANI PANDEY
 

Recently uploaded (20)

VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
Russian Call Girls in %(+971524965298  )#  Call Girls in DubaiRussian Call Girls in %(+971524965298  )#  Call Girls in Dubai
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceReal Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
 

Efficient Use of indexes in MySQL

  • 1. Indexes in MySQL Aleksandr Kuzminsky https://twindb.com How to use indexes efficiently
  • 2. Agenda 1. How data is organized 2. Data access
  • 3. Who we are Aleks: ● TwinDB co-founder ● Dropbox DBA ● ex-Percona consultant Ovais: ● TwinDB co-founder ● Lithium lead DBA ● ex-Percona consultant
  • 4. How data is organized
  • 5. Table in MySQL (InnoDB) CREATE TABLE `actor`( `actor_id` SMALLINT(5) UNSIGNED NOT NULL, `first_name` VARCHAR(45) NOT NULL, `last_name` VARCHAR(45) NOT NULL, `last_update` TIMESTAMP NOT NULL, PRIMARY KEY (`actor_id`), KEY `idx_actor_last_name` (`last_name`) ) ENGINE=InnoDB;
  • 6. B+ Tree ● O(log(n)) ● Shallow ● Data in leaf pages
  • 7. sakila.actor PRIMARY idx_actor_last_name actor_id first_name last_name last_update 1 PENELOPE GUINESS 2006-02-15 04:34:33 2 NICK WAHLBERG 2006-02-15 04:34:33 3 ED CHASE 2006-02-15 04:34:33 4 JENNIFER DAVIS 2006-02-15 04:34:33 5 JOHNNY WOOD 2006-02-15 04:34:33 ... ... ... ... last_name actor_id AKROYD 58 AKROYD 92 AKROYD 182 ALLEN 118 ALLEN 145 ... ...
  • 8. Data Access Fast if accessing table and producing result is simultaneous
  • 9. Point SELECT SELECT * FROM actor WHERE actor_id = 3; actor_id first_name last_name last_update 1 PENELOPE GUINESS 2006-02-15 04:34:33 2 NICK WAHLBERG 2006-02-15 04:34:33 3 ED CHASE 2006-02-15 04:34:33 4 JENNIFER DAVIS 2006-02-15 04:34:33 5 JOHNNY WOOD 2006-02-15 04:34:33 ... ... ... ...
  • 10. SELECT by range of keys SELECT * FROM actor WHERE actor_id > 3; actor_id first_name last_name last_update 1 PENELOPE GUINESS 2006-02-15 04:34:33 2 NICK WAHLBERG 2006-02-15 04:34:33 3 ED CHASE 2006-02-15 04:34:33 4 JENNIFER DAVIS 2006-02-15 04:34:33 5 JOHNNY WOOD 2006-02-15 04:34:33 ... ... ... ...
  • 11. Lookup by secondary key actor_id first_name last_name last_update 117 RENEE TRACY 2006-02-15 04:34:33 118 CUBA ALLEN 2006-02-15 04:34:33 119 WARREN JACKMAN 2006-02-15 04:34:33 ... ... ... ... 145 KIM ALLEN 2006-02-15 04:34:33 ... ... ... ... last_name actor_id AKROYD 58 AKROYD 92 AKROYD 182 ALLEN 118 ALLEN 145 ... ... SELECT * FROM actor WHERE last_name = ‘ALLEN’; Step 1 Step 2
  • 12. Using index for data access last_name actor_id AKROYD 182 ALLEN 118 ALLEN 145 ALLEN 194 ASTAIRE 76 ... ... SELECT COUNT(*) FROM actor WHERE last_name = ‘ALLEN’;
  • 13. Using index for data access EXPLAIN SELECT COUNT(*) FROM actor WHERE last_name = ‘ALLEN’; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: actor type: ref possible_keys: idx_actor_last_name key: idx_actor_last_name key_len: 137 ref: const rows: 3 Extra: Using where; Using index
  • 14. Covering indexes ALTER TABLE actor ADD INDEX idx_last_first(last_name, first_name); SELECT first_name FROM actor WHERE last_name = 'ALLEN' last_name first_name actor_id AKROYD KIRSTEN 182 ALLEN CUBA 118 ALLEN KIM 145 ALLEN MERYL 194 ASTAIRE ANGELINA 76 ... ... *************************** 1. row *************************** id: 1 select_type: SIMPLE table: actor type: ref possible_keys: idx_actor_last_name, idx_last_first key: idx_last_first key_len: 137 ref: const rows: 3 Extra: Using where; Using index
  • 15. DISTINCT *************************** 1. row *************************** id: 1 select_type: SIMPLE table: actor type: index possible_keys: idx_actor_last_name, idx_last_first key: idx_actor_last_name key_len: 137 ref: NULL rows: 200 Extra: Using index last_name actor_id AKROYD 182 ALLEN 118 ALLEN 145 ALLEN 194 ASTAIRE 76 ... ... SELECT DISTINCT last_name FROM actor
  • 16. GROUP BY *************************** 1. row *************************** id: 1 select_type: SIMPLE table: actor type: index possible_keys: idx_actor_last_name,idx_last_first key: idx_actor_last_name key_len: 137 ref: NULL rows: 200 Extra: Using index last_name actor_id AKROYD 182 ALLEN 118 ALLEN 145 ALLEN 194 ASTAIRE 76 ... ... SELECT last_name, COUNT(*) FROM actor GROUP BY last_name
  • 17. Loose index scan ALTER TABLE actor ADD COLUMN rank INT; UPDATE actor SET rank = ROUND(100 * RAND()); ALTER TABLE actor ADD INDEX idx_last_rank (last_name, rank); last_name rank actor_id AKROYD 40 58 AKROYD 42 92 AKROYD 95 182 ALLEN 19 194 ALLEN 35 118 ... ...
  • 18. Loose index scan SELECT last_name, MIN(rank) FROM actor GROUP BY last_name last_name rank actor_id AKROYD 40 58 AKROYD 42 92 AKROYD 95 182 ALLEN 19 194 ALLEN 35 118 ... ... *************************** 1. row *************************** id: 1 select_type: SIMPLE table: actor type: range possible_keys: …, idx_last_rank key: idx_last_rank key_len: 137 ref: NULL rows: 247 Extra: Using index for group-by
  • 19. Sorting SELECT * FROM actor WHERE last_name = 'AKROYD' ORDER BY rank last_name rank actor_id AKROYD 40 58 AKROYD 42 92 AKROYD 95 182 ALLEN 19 194 ALLEN 35 118 ... ... *************************** 1. row *************************** id: 1 select_type: SIMPLE table: actor type: ref possible_keys: …, idx_last_rank key: idx_last_rank key_len: 137 ref: const rows: 3 Extra: Using where
  • 20. Joining tables SELECT title, first_name, last_name FROM film JOIN film_actor ON film_actor.film_id = film.film_id JOIN actor ON actor.actor_id = film_actor.actor_id ORDER BY title;
  • 21. Joining tables SELECT title, first_name, last_name FROM film FORCE INDEX (`idx_title`) JOIN film_actor ON film_actor.film_id = film.film_id JOIN actor ON actor.actor_id = film_actor.actor_id ORDER BY title;
  • 22. How to compare efficiency