Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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
...
How data is organized
Table in MySQL (InnoDB)
CREATE TABLE `actor`(
`actor_id` SMALLINT(5) UNSIGNED NOT NULL,
`first_name` VARCHAR(45) NOT NULL,...
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
...
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-...
SELECT by range of keys
SELECT * FROM actor WHERE actor_id > 3;
actor_id first_name last_name last_update
1 PENELOPE GUINE...
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-...
Using index for data access
last_name actor_id
AKROYD 182
ALLEN 118
ALLEN 145
ALLEN 194
ASTAIRE 76
... ...
SELECT COUNT(*)...
Using index for data access
EXPLAIN SELECT COUNT(*) FROM actor WHERE last_name = ‘ALLEN’;
*************************** 1. r...
Covering indexes
ALTER TABLE actor ADD INDEX idx_last_first(last_name, first_name);
SELECT first_name FROM actor WHERE las...
DISTINCT
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: index...
GROUP BY
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: index...
Loose index scan
ALTER TABLE actor ADD COLUMN rank INT;
UPDATE actor SET rank = ROUND(100 * RAND());
ALTER TABLE actor ADD...
Loose index scan
SELECT last_name, MIN(rank) FROM actor GROUP BY last_name
last_name rank actor_id
AKROYD 40 58
AKROYD 42 ...
Sorting
SELECT * FROM actor WHERE last_name = 'AKROYD' ORDER BY rank
last_name rank actor_id
AKROYD 40 58
AKROYD 42 92
AKR...
Joining tables
SELECT title, first_name, last_name
FROM film
JOIN film_actor
ON film_actor.film_id = film.film_id
JOIN act...
Joining tables
SELECT title, first_name, last_name
FROM film FORCE INDEX (`idx_title`)
JOIN film_actor
ON film_actor.film_...
How to compare efficiency
Q&A
Thank you!
Upcoming SlideShare
Loading in …5
×

Efficient Use of indexes in MySQL

2,841 views

Published on

The presentation reveals how MySQL/InnoDB organizes primary and secondary indexes and how MySQL uses the indexes to execute user queries

Published in: Internet

Efficient Use of indexes in MySQL

  1. 1. Indexes in MySQL Aleksandr Kuzminsky https://twindb.com How to use indexes efficiently
  2. 2. Agenda 1. How data is organized 2. Data access
  3. 3. Who we are Aleks: ● TwinDB co-founder ● Dropbox DBA ● ex-Percona consultant Ovais: ● TwinDB co-founder ● Lithium lead DBA ● ex-Percona consultant
  4. 4. How data is organized
  5. 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. 6. B+ Tree ● O(log(n)) ● Shallow ● Data in leaf pages
  7. 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. 8. Data Access Fast if accessing table and producing result is simultaneous
  9. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 22. How to compare efficiency
  23. 23. Q&A Thank you!

×