efficient indexes in mysql
TRANSCRIPT
![Page 1: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/1.jpg)
Efficient Indexes in MySQL
https://twindb.com Ovais Tariq & Aleksandr Kuzminsky
![Page 2: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/2.jpg)
Who we are Aleks: ● TwinDB co-founder ● Dropbox MySQL SRE ● ex-Percona consultant
Ovais: ● TwinDB co-founder ● Lithium Lead SRE ● ex-Percona consultant
![Page 3: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/3.jpg)
Agenda
1. How is data organized 2. How is data accessed
![Page 4: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/4.jpg)
![Page 5: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/5.jpg)
How data is organized
![Page 6: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/6.jpg)
B+ Tree
O(log(n))
![Page 7: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/7.jpg)
B+Tree Characteristics
• Leaf node contains data
• Doubly linked list of leaf nodes
• Keys stored in sorted order
• All leaf nodes at the same height
![Page 8: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/8.jpg)
Few Advantages
• Reduced I/O
• Reduced Rebalancing
• Extremely efficient range scans
• Implicit sorting
![Page 9: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/9.jpg)
Index Height
h is the height of the tree n is the number of rows in a table p is the branching factor of the tree p = page size in bytes/key length in bytes
h=⎡ logn/logp⎤
![Page 10: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/10.jpg)
Index Height
# records Height with INT Primary Key
Height with CHAR(36) Primary Key
Increase in IO Operations
1,000 1 2 +100%
1,000,000 2 3 +50%
1,000,000,000 3 4 +33%
1,000,000,000,000 4 5 +25%
![Page 11: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/11.jpg)
Table in MySQL (InnoDB)
CREATETABLE`actor`(`actor_id`SMALLINT(5)UNSIGNEDNOTNULL,`first_name`VARCHAR(45)NOTNULL,`last_name`VARCHAR(45)NOTNULL,`last_update`TIMESTAMPNOTNULL,PRIMARYKEY(`actor_id`),KEY`idx_actor_last_name`(`last_name`))ENGINE=InnoDB;
![Page 12: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/12.jpg)
sakila.actor
PRIMARY
idx_actor_last_name actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-1504:34:33
2 NICK WAHLBERG 2006-02-1504:34:33
3 ED CHASE 2006-02-1504:34:33
4 JENNIFER DAVIS 2006-02-1504:34:33
5 JOHNNY WOOD 2006-02-1504:34:33
... ... ... ...
last_name actor_id
AKROYD 58
AKROYD 92
AKROYD 182
ALLEN 118
ALLEN 145
... ...
![Page 13: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/13.jpg)
How Data is Accessed
Fast if accessing table
and producing result
is simultaneous
![Page 14: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/14.jpg)
Point SELECT SELECT*FROMactorWHEREactor_id=3;
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-1504:34:33
2 NICK WAHLBERG 2006-02-1504:34:33
3 ED CHASE 2006-02-1504:34:33
4 JENNIFER DAVIS 2006-02-1504:34:33
5 JOHNNY WOOD 2006-02-1504:34:33
... ... ... ...
![Page 15: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/15.jpg)
SELECT by range of keys SELECT*FROMactorWHEREactor_id>3;
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-1504:34:33
2 NICK WAHLBERG 2006-02-1504:34:33
3 ED CHASE 2006-02-1504:34:33
4 JENNIFER DAVIS 2006-02-1504:34:33
5 JOHNNY WOOD 2006-02-1504:34:33
... ... ... ...
![Page 16: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/16.jpg)
Lookup by secondary key
actor_id first_name last_name last_update
117 RENEE TRACY 2006-02-1504:34:33
118 CUBA ALLEN 2006-02-1504:34:33
119 WARREN JACKMAN 2006-02-1504:34:33
... ... ... ...
145 KIM ALLEN 2006-02-1504:34:33
... ... ... ...
last_name actor_id
AKROYD 58
AKROYD 92
AKROYD 182
ALLEN 118
ALLEN 145
... ...
SELECT*FROMactorWHERElast_name=‘ALLEN’;
Step 1 Step 2
![Page 17: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/17.jpg)
Using index for data access
last_name actor_id
AKROYD 182
ALLEN 118
ALLEN 145
ALLEN 194
ASTAIRE 76
... ...
SELECTCOUNT(*)FROMactorWHERElast_name=‘ALLEN’;
![Page 18: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/18.jpg)
Using index for data access EXPLAINSELECTCOUNT(*)FROMactorWHERElast_name=‘ALLEN’;
***************************1.row***************************id:1select_type:SIMPLEtable:actortype:refpossible_keys:idx_actor_last_namekey:idx_actor_last_namekey_len:137ref:constrows:3Extra:Usingwhere;Usingindex
![Page 19: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/19.jpg)
Covering indexes ALTERTABLEactorADDINDEXidx_last_first(last_name,first_name);SELECTfirst_nameFROMactorWHERElast_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:1select_type:SIMPLEtable:actortype:refpossible_keys:idx_actor_last_name,idx_last_firstkey:idx_last_firstkey_len:137ref:constrows:3Extra:Usingwhere;Usingindex
![Page 20: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/20.jpg)
DISTINCT
***************************1.row***************************
id:1select_type:SIMPLEtable:actortype:indexpossible_keys:idx_actor_last_name,idx_last_firstkey:idx_actor_last_namekey_len:137ref:NULLrows:200Extra:Usingindex
last_name actor_id
AKROYD 182
ALLEN 118
ALLEN 145
ALLEN 194
ASTAIRE 76
... ...
SELECTDISTINCTlast_nameFROMactor
![Page 21: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/21.jpg)
GROUP BY
***************************1.row***************************
id:1select_type:SIMPLEtable:actortype:indexpossible_keys:idx_actor_last_name,idx_last_first key:idx_actor_last_namekey_len:137ref:NULLrows:200Extra:Usingindex
last_name actor_id
AKROYD 182
ALLEN 118
ALLEN 145
ALLEN 194
ASTAIRE 76
... ...
SELECTlast_name,COUNT(*)FROMactorGROUPBYlast_name
![Page 22: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/22.jpg)
Loose index scan ALTERTABLEactorADDCOLUMNrankINT;UPDATEactorSETrank=ROUND(100*RAND());ALTERTABLEactorADDINDEXidx_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
... ...
![Page 23: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/23.jpg)
Loose index scan SELECTlast_name,MIN(rank)FROMactorGROUPBYlast_name
last_name rank actor_id
AKROYD 40 58
AKROYD 42 92
AKROYD 95 182
ALLEN 19 194
ALLEN 35 118
... ...
***************************1.row***************************
id:1select_type:SIMPLEtable:actortype:rangepossible_keys:…,idx_last_rank key:idx_last_rankkey_len:137ref:NULLrows:247Extra:Usingindexforgroup-by
![Page 24: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/24.jpg)
Sorting SELECT*FROMactorWHERElast_name='AKROYD'ORDERBYrank
last_name rank actor_id
AKROYD 40 58
AKROYD 42 92
AKROYD 95 182
ALLEN 19 194
ALLEN 35 118
... ...
***************************1.row***************************
id:1select_type:SIMPLEtable:actortype:refpossible_keys:…,idx_last_rank key:idx_last_rankkey_len:137ref:constrows:3Extra:Usingwhere
![Page 25: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/25.jpg)
Joining tables SELECTtitle,first_name,last_nameFROMfilmJOINfilm_actorONfilm_actor.film_id=film.film_idJOINactorONactor.actor_id=film_actor.actor_idORDERBYtitle;
Response time: 25ms
![Page 26: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/26.jpg)
Joining tables SELECTtitle,first_name,last_nameFROMfilmFORCEINDEX(`idx_title`)JOINfilm_actorONfilm_actor.film_id=film.film_idJOINactorONactor.actor_id=film_actor.actor_idORDERBYtitle;
Response time: 5ms (5 times faster!)
![Page 27: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/27.jpg)
How to compare efficiency
![Page 28: Efficient Indexes in MySQL](https://reader031.vdocument.in/reader031/viewer/2022021923/5a6d624c7f8b9a0a428b553d/html5/thumbnails/28.jpg)
Q&A
Thank you!