+ All Categories
Home > Internet > Efficient Indexes in MySQL

Efficient Indexes in MySQL

Date post: 28-Jan-2018
Category:
Upload: aleksandr-kuzminsky
View: 487 times
Download: 1 times
Share this document with a friend
28
Ecient Indexes in MySQL https://twindb.com Ovais Tariq & Aleksandr Kuzminsky
Transcript
Page 1: Efficient Indexes in MySQL

Efficient Indexes in MySQL

https://twindb.com Ovais Tariq & Aleksandr Kuzminsky

Page 2: Efficient Indexes in MySQL

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

Agenda

1. How is data organized 2. How is data accessed

Page 4: Efficient Indexes in MySQL
Page 5: Efficient Indexes in MySQL

How data is organized

Page 6: Efficient Indexes in MySQL

B+ Tree

O(log(n))

Page 7: Efficient Indexes in MySQL

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

Few Advantages

•  Reduced I/O

•  Reduced Rebalancing

•  Extremely efficient range scans

•  Implicit sorting

Page 9: Efficient Indexes in MySQL

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

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

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

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

How Data is Accessed

Fast if accessing table

and producing result

is simultaneous

Page 14: Efficient Indexes in MySQL

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

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

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

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

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

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

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

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

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

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

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

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

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

How to compare efficiency

Page 28: Efficient Indexes in MySQL

Q&A

Thank you!


Recommended