(Go: >> BACK << -|- >> HOME <<)

SlideShare a Scribd company logo
MySQL Query Optimization
Query 
Optimization 
Morgan 
Tocker 
MySQL 
Community 
Manager 
October, 
2014 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
|
Safe 
Harbor 
Statement 
The 
following 
is 
intended 
to 
outline 
our 
general 
product 
direction. 
It 
is 
intended 
for 
information 
purposes 
only, 
and 
may 
not 
be 
incorporated 
into 
any 
contract. 
It 
is 
not 
a 
commitment 
to 
deliver 
any 
material, 
code, 
or 
functionality, 
and 
should 
not 
be 
relied 
upon 
in 
making 
purchasing 
decisions. 
The 
development, 
release, 
and 
timing 
of 
any 
features 
or 
functionality 
described 
for 
Oracle’s 
products 
remains 
at 
the 
sole 
discretion 
of 
Oracle. 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
3
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
4 
Today’s 
Agenda 
Introduction 
World 
Schema 
IMDB 
Schema 
Advanced 
Topics 
1 
2 
3 
4
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Introduction 
• SQL 
is 
declarative. 
• You 
say 
what 
you 
want, 
not 
how 
to 
process. 
• Can’t 
sight 
check 
a 
query 
to 
understand 
how 
it 
executes. 
• Database 
management 
chooses 
the 
best 
possible 
way 
-­‐ 
think 
like 
a 
GPS 
navigator. 
5
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
GPS 
Software 
6
MySQL 
Optimizer 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
7
Diagnostic 
Commands 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• EXPLAIN 
(all 
versions) 
• EXPLAIN 
FORMAT=JSON 
(MySQL 
5.6+) 
• Workbench 
supports 
visual 
format. 
• OPTIMIZER 
TRACE 
(MySQL 
5.6+) 
8
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
9 
Today’s 
Agenda 
Introduction 
World 
Schema 
IMDB 
Schema 
Advanced 
Topics 
1 
2 
3 
4
The 
World 
Schema 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Contains 
cities, 
countries 
and 
language 
statistics. 
• Download 
From: 
• http://dev.mysql.com/doc/index-­‐other.html 
• Very 
small 
data 
set 
-­‐ 
good 
for 
learning. 
• Not 
perfect 
for 
explaining 
performance 
differences 
10
Find 
Countries 
in 
Asia 
with 
Population 
>5M 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
11 
mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 
'Asia' AND population > 5000000G 
********************** 1. row ********************** 
id: 1 
select_type: SIMPLE 
table: Country 
partitions: NULL 
type: ALL 
possible_keys: NULL 
key: NULL 
key_len: NULL 
ref: NULL 
rows: 239 
filtered: 4.76 
Extra: Using where 
1 row in set, 1 warning (0.00 sec)
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 12 
EXPLAIN: { 
"query_block": { 
"select_id": 1, 
"cost_info": { 
"query_cost": "53.80" 
}, 
"table": { 
"table_name": "Country", 
"access_type": "ALL", 
"rows_examined_per_scan": 239, 
"rows_produced_per_join": 11, 
"filtered": 4.7614, 
"cost_info": { 
"read_cost": "51.52", 
"eval_cost": "2.28", 
"prefix_cost": "53.80", 
"data_read_per_join": "2K" 
}, 
"used_columns": [ 
"Code", 
.. 
"Capital", 
"Code2" 
], 
"attached_condition": "((`world`.`country`.`Continent` = 'Asia') and 
(`world`.`country`.`Population` > 5000000))" 
} 
} 
} 
FORMAT=JSON 
Available 
since 
MySQL 
5.6. 
Expanded 
in 
5.7 
to 
add 
cost 
information.
What 
indexes 
will 
make 
this 
query 
faster? 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Some 
suggestions: 
• Index 
on 
(population) 
• Index 
on 
(continent) 
• Index 
on 
(population, 
continent) 
• Index 
on 
(continent, 
population) 
13 
SELECT * FROM Country WHERE Continent = 
'Asia' AND population > 5000000;
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Next 
Question 
• Given 
so 
many 
choices, 
which 
is 
the 
best 
choice? 
• A 
good 
GPS 
navigator 
understands 
traffic 
and 
finds 
the 
fastest 
route. 
• A 
good 
query 
optimizer 
does 
similar. 
14
Query 
Optimizer 
Strategy 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Pick 
the 
plan 
that 
consumes 
the 
least 
amount 
of 
resources 
(CPU, 
IO). 
15
Query 
Optimizer 
Strategy 
(cont.) 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Use 
statistics 
available 
to 
identify 
good 
and 
bad 
plans. 
• Decisions 
may 
include: 
• Which 
indexes 
are 
good/bad. 
• Which 
order 
to 
join 
tables 
in. 
• If 
indexes 
should 
be 
read 
in 
a 
particular 
order 
to 
avoid 
sorting, 
or 
if 
sorting 
is 
not 
expensive. 
16
Using 
single 
column 
indexes 
first…. 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• p 
(population) 
• c 
(continent) 
17
mysql> ALTER TABLE Country ADD INDEX p (population); 
Query OK, 0 rows affected (0.04 sec) 
Records: 0 Duplicates: 0 Warnings: 0 
mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 
'Asia' AND population > 5000000G 
************************ 1. row ************************ 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 18 
id: 1 
select_type: SIMPLE 
table: Country 
partitions: NULL 
type: ALL 
possible_keys: p 
key: NULL 
key_len: NULL 
ref: NULL 
rows: 239 
filtered: 6.46 
Extra: Using where 
1 row in set, 1 warning (0.00 sec)
Why 
would 
an 
index 
not 
be 
used? 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
19 
mysql> EXPLAIN FORMAT=JSON 
SELECT * FROM Country FORCE 
INDEX (p) WHERE Continent = 
'Asia' AND population > 
5000000G 
******* 1. row ******* 
EXPLAIN: { 
"query_block": { 
"select_id": 1, 
"cost_info": { 
"query_cost": "152.21" 
}, 
"table": { 
"table_name": "Country", 
"access_type": "range", 
"possible_keys": [ 
"p" 
], 
Using 
MySQL 
5.7+ 
mysql> EXPLAIN FORMAT=JSON 
SELECT * FROM Country WHERE 
Continent = 'Asia' AND 
population > 5000000G 
******* 1. row ******* 
EXPLAIN: { 
"query_block": { 
"select_id": 1, 
"cost_info": { 
"query_cost": "53.80" 
}, 
"table": { 
"table_name": "Country", 
"access_type": "ALL", 
"possible_keys": [ 
"p" 
],
Modifying 
the 
query 
range: 
>50M 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
20 
mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 
'Asia' AND population > 50000000G 
********************** 1. row ********************** 
id: 1 
select_type: SIMPLE 
table: Country 
partitions: NULL 
type: range 
possible_keys: p 
key: p 
key_len: 4 
ref: NULL 
rows: 24 
filtered: 14.29 
Extra: Using index condition; Using where 
1 row in set, 1 warning (0.00 sec)
mysql> ALTER TABLE Country ADD INDEX c (continent); 
Query OK, 0 rows affected (0.04 sec) 
Records: 0 Duplicates: 0 Warnings: 0 
mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 
'Asia' AND population > 5000000G 
*********************** 1. row *********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 21 
id: 1 
select_type: SIMPLE 
table: Country 
partitions: NULL 
type: ref 
possible_keys: p,c 
key: c 
key_len: 1 
ref: const 
rows: 51 
filtered: 45.19 
Extra: Using where 
1 row in set, 1 warning (0.00 sec)
Modifying 
the 
query 
range: 
>500M 
mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 
'Asia' AND population > 500000000G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 22 
id: 1 
select_type: SIMPLE 
table: Country 
partitions: NULL 
type: range 
possible_keys: p,c 
key: p 
key_len: 4 
ref: NULL 
rows: 2 
filtered: 21.34 
Extra: Using index condition; Using where 
1 row in set, 1 warning (0.00 sec)
Composite 
Indexes 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• On 
>50M 
which 
is 
better? 
• (population, 
continent) 
• (continent, 
population) 
23
mysql> ALTER TABLE Country ADD INDEX pc (pop..on, co..nt); 
Query OK, 0 rows affected (0.02 sec) 
Records: 0 Duplicates: 0 Warnings: 0 
mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 
'Asia' AND population > 50000000G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 24 
id: 1 
select_type: SIMPLE 
table: Country 
partitions: NULL 
type: ref 
possible_keys: p,c,pc 
key: c 
key_len: 1 
ref: const 
rows: 51 
filtered: 10.04 
Extra: Using where 
1 row in set, 1 warning (0.00 sec)
FORCE 
INDEX 
mysql> EXPLAIN SELECT * FROM Country FORCE INDEX (pc) 
WHERE Continent = 'Asia' AND population > 50000000G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 25 
id: 1 
select_type: SIMPLE 
table: Country 
partitions: NULL 
type: range 
possible_keys: pc 
key: pc 
key_len: 4 
ref: NULL 
rows: 24 
filtered: 14.29 
Extra: Using index condition 
1 row in set, 1 warning (0.00 sec)
mysql> EXPLAIN FORMAT=JSON SELECT * FROM Country FORCE 
INDEX (pc) WHERE Continent = 'Asia' AND population > 
50000000G 
********************** 1. row ********************** 
EXPLAIN: { 
"query_block": { 
"select_id": 1, 
"cost_info": { 
"query_cost": "34.61" 
}, 
"table": { 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 26 
.. 
], 
"key": "pc", 
"used_key_parts": [ 
"Population" 
], 
.. 
Easier 
to 
see 
with 
JSON 
EXPLAIN
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Rule 
of 
Thumb 
• Index 
on 
(const, 
range) 
instead 
of 
(range, 
const). 
• Applies 
to 
all 
databases. 
27
mysql> ALTER TABLE Country ADD INDEX cp (co..nt, pop..on); 
Query OK, 0 rows affected (0.02 sec) 
Records: 0 Duplicates: 0 Warnings: 0 
mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 
'Asia' AND population > 50000000G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 28 
id: 1 
select_type: SIMPLE 
table: Country 
partitions: NULL 
type: range 
possible_keys: p,c,pc,cp 
key: cp 
key_len: 5 
ref: NULL 
rows: 11 
filtered: 100.00 
Extra: Using index condition 
1 row in set, 1 warning (0.00 sec)
Where 
Population 
>N. 
Lower 
is 
better. 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Cost 
Estimates 
29 
5M 50M 500M 
Table 
Scan 53.80 53.80 53.80 
(Population) 152.21 34.61 3.81 
(Continent) 28.20 28.20 28.20 
(Population, 
152.21 34.61 3.81 
Continent) 
(Continent, 
Population) 
24.83 16.41 3.81
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
30 
Today’s 
Agenda 
Introduction 
World 
Schema 
IMDB 
Schema 
Advanced 
Topics 
1 
2 
3 
4
Examples 
Using 
IMDB 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Larger 
than 
world 
database 
schema. 
• Better 
to 
understand 
performance 
differences. 
• License 
requires 
data 
to 
be 
regenerated: 
• http://imdbpy.sourceforge.net/ 
31
mysql> EXPLAIN SELECT * FROM title WHERE 
title = 'The Lion King'G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 32 
id: 1 
select_type: SIMPLE 
table: title 
partitions: NULL 
type: ALL 
possible_keys: NULL 
key: NULL 
key_len: NULL 
ref: NULL 
rows: 2387523 
filtered: 0.50 
Extra: Using where 
1 row in set, 1 warning (0.00 sec) 
Ignore 
EXPLAIN 
time 
Actual 
run 
time 
is 
2.9 
seconds 
(Not 
shown)
mysql> ALTER TABLE title ADD INDEX (title(100)); 
Query OK, 0 rows affected (4.73 sec) 
Records: 0 Duplicates: 0 Warnings: 0 
mysql> EXPLAIN SELECT * FROM title WHERE 
title = 'The Lion King'G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 33 
id: 1 
select_type: SIMPLE 
table: title 
partitions: NULL 
type: ref 
possible_keys: title 
key: title 
key_len: 102 
ref: const 
rows: 4 
filtered: 100.00 
Extra: Using where 
1 row in set, 1 warning (0.01 sec) 
From 
2.9 
seconds 
to 
0.0 
seconds 
(!!!)
How 
does 
LIKE 
work? 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
34 
mysql> EXPLAIN SELECT * FROM title WHERE 
title LIKE 'The Lion %'G 
********************** 1. row ********************** 
id: 1 
select_type: SIMPLE 
table: title 
partitions: NULL 
type: range 
possible_keys: title 
key: title 
key_len: 102 
ref: NULL 
rows: 109 
filtered: 100.00 
Extra: Using where 
1 row in set, 1 warning (0.01 sec)
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
LIKE 
(cont.) 
35 
mysql> EXPLAIN SELECT * FROM title WHERE title 
LIKE 'The%'G 
********************** 1. row ********************** 
id: 1 
select_type: SIMPLE 
table: title 
partitions: NULL 
type: range 
possible_keys: title 
key: title 
key_len: 102 
ref: NULL 
rows: 325548 
filtered: 100.00 
Extra: Using where 
1 row in set, 1 warning (0.00 sec) 
VS 
Very 
Close. 
Estimate 
is 
that 
it 
is 
almost 
as 
fast 
to 
table 
scan. 
Run 
Time 
is 
1.9 
sec
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Sorting 
36 
mysql> EXPLAIN SELECT * FROM title WHERE 
title = 'The Lion King' ORDER BY production_yearG 
********************** 1. row ********************** 
id: 1 
select_type: SIMPLE 
table: title 
partitions: NULL 
type: ref 
possible_keys: title 
key: title 
key_len: 102 
ref: const 
rows: 4 
filtered: 100.00 
Extra: Using where; Using filesort 
1 row in set, 1 warning (0.00 sec)
SELECT movie_info.* FROM title INNER JOIN movie_info ON 
title.id=movie_info.movie_id WHERE title=‘The Lion King’G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 37 
id: 1 
select_type: SIMPLE 
table: movie_info 
type: ALL 
possible_keys: NULL 
key: NULL 
key_len: NULL 
ref: NULL 
rows: 1212187 
Extra: NULL 
********************** 2. row ********************** 
id: 1 
select_type: SIMPLE 
table: title 
type: eq_ref 
possible_keys: PRIMARY,title 
key: PRIMARY 
key_len: 4 
ref: imdb.movie_info.movie_id 
rows: 1 
Extra: Using where
mysql> ALTER TABLE movie_info ADD INDEX mi (movie_id); 
Query OK, 0 rows affected (1.95 sec) 
Records: 0 Duplicates: 0 Warnings: 0 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 38
EXPLAIN SELECT movie_info.* FROM title INNER .. WHERE 
title='The Lion King'G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 39 
id: 1 
select_type: SIMPLE 
table: title 
type: ref 
possible_keys: PRIMARY,title 
key: title 
key_len: 102 
ref: const 
rows: 4 
Extra: Using where 
********************** 2. row ********************** 
id: 1 
select_type: SIMPLE 
table: movie_info 
type: ref 
possible_keys: mi 
key: mi 
key_len: 4 
ref: imdb.title.id 
rows: 7 
Extra: NULL
mysql> EXPLAIN SELECT * FROM movie_info WHERE movie_id IN 
(SELECT id FROM title WHERE title = 'The Lion King')G 
********************** 1. row ********************** 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 40 
id: 1 
select_type: SIMPLE 
table: title 
type: ref 
possible_keys: PRIMARY,title 
key: title 
key_len: 102 
ref: const 
rows: 4 
Extra: Using where 
********************** 2. row ********************** 
id: 1 
select_type: SIMPLE 
table: movie_info 
type: ref 
possible_keys: mi 
key: mi 
key_len: 4 
ref: imdb.title.id 
rows: 7 
Extra: NULL
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
41 
Today’s 
Agenda 
Introduction 
World 
Schema 
IMDB 
Schema 
Advanced 
Topics 
1 
2 
3 
4
Optimizer 
Hints 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• USE 
INDEX 
• FORCE 
INDEX 
• IGNORE 
INDEX 
• STRAIGHT 
JOIN 
42
Optimizer 
Hints 
(cont.) 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
43 
Expanded 
in 
MySQL 
5.6 
mysql> select @@optimizer_switchG 
********************** 1. row ********************** 
@@optimizer_switch: 
index_merge=on,index_merge_union=on,index_merge_sort_union 
=on,index_merge_intersection=on,engine_condition_pushdown= 
on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,bl 
ock_nested_loop=on,batched_key_access=off,materialization= 
on,semijoin=on,loosescan=on,firstmatch=on,subquery_materia 
lization_cost_based=on,use_index_extensions=on,condition_f 
anout_filter=on 
1 row in set (0.00 sec)
Statistics 
Sampling 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Some 
decisions 
based 
on 
pre-­‐computed 
statistics. 
• For 
example, 
which 
order 
to 
join 
tables. 
• Accuracy 
of 
statistics 
greatly 
improved 
in 
MySQL 
5.6. 
• Feature 
is 
called 
“InnoDB 
persistent 
statistics”. 
44
Optimizer 
Decision 
Visibility 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Intended 
for 
MySQL 
developer 
usage, 
and 
reporting 
optimizer 
bugs. 
• Yet 
totally 
practical 
for 
advanced 
usage 
:) 
45 
New 
to 
MySQL 
5.6 
SET optimizer_trace="enabled=on"; 
SELECT /* query here */; 
SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
Optimizer 
Trace 
Output 
• Includes 
why 
decisions 
were 
not 
made: 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
46 
.. 
"index": "pc", 
"ranges": [ 
"50000000 < Population" 
], 
"index_dives_for_eq_ranges": true, 
"rowid_ordered": false, 
"using_mrr": false, 
"index_only": false, 
"rows": 24, 
"cost": 29.81, 
"chosen": false, 
"cause": "cost" 
},
Too 
many 
indexes 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Slows 
down 
modifications 
due 
to 
maintenance 
cost. 
• i.e. 
Slower 
INSERT/UPDATE/DELETE 
• May 
also 
lead 
to 
worse 
decisions 
being 
made 
by 
optimizer. 
• Too 
many 
similar 
choices. 
• Only 
limited 
time 
window 
to 
evaluate 
suitability. 
• Space 
requirements 
in 
memory 
and 
storage. 
47
MySQL 
5.7 
and 
beyond 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• We 
are 
working 
on 
a 
new 
cost 
model. 
• Better 
adapt 
to 
new 
hardware 
with 
different 
costs 
(i.e. 
SSDs 
have 
cheap 
random 
IO). 
• Memory 
buffer 
aware 
cost 
estimates. 
48
Cost 
constants 
configurable 
per 
engine 
in 
5.7 
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Previously 
hard-­‐coded 
constants. 
• Better 
adapts 
to 
differences 
in 
hardware. 
49
Copyright 
© 
2014 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
50
MySQL Query Optimization

More Related Content

What's hot

PostgreSql query planning and tuning
PostgreSql query planning and tuningPostgreSql query planning and tuning
PostgreSql query planning and tuning
Federico Campoli
 
Linking Metrics to Logs using Loki
Linking Metrics to Logs using LokiLinking Metrics to Logs using Loki
Linking Metrics to Logs using Loki
Knoldus Inc.
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
EDB
 
patroni-based citrus high availability environment deployment
patroni-based citrus high availability environment deploymentpatroni-based citrus high availability environment deployment
patroni-based citrus high availability environment deployment
hyeongchae lee
 
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonThrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Igor Anishchenko
 
MySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsMySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 Tips
OSSCube
 
Large Table Partitioning with PostgreSQL and Django
 Large Table Partitioning with PostgreSQL and Django Large Table Partitioning with PostgreSQL and Django
Large Table Partitioning with PostgreSQL and Django
EDB
 
ClickHouse Introduction by Alexander Zaitsev, Altinity CTO
ClickHouse Introduction by Alexander Zaitsev, Altinity CTOClickHouse Introduction by Alexander Zaitsev, Altinity CTO
ClickHouse Introduction by Alexander Zaitsev, Altinity CTO
Altinity Ltd
 
ClickHouse Query Performance Tips and Tricks, by Robert Hodges, Altinity CEO
ClickHouse Query Performance Tips and Tricks, by Robert Hodges, Altinity CEOClickHouse Query Performance Tips and Tricks, by Robert Hodges, Altinity CEO
ClickHouse Query Performance Tips and Tricks, by Robert Hodges, Altinity CEO
Altinity Ltd
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadinC* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
DataStax Academy
 
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdfDeep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
Altinity Ltd
 
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Databricks
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
Alexey Bashtanov
 
Cloud Native PostgreSQL
Cloud Native PostgreSQLCloud Native PostgreSQL
Cloud Native PostgreSQL
EDB
 
Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performance
PostgreSQL-Consulting
 
オンライン物理バックアップの排他モードと非排他モードについて ~PostgreSQLバージョン15対応版~(第34回PostgreSQLアンカンファレンス...
オンライン物理バックアップの排他モードと非排他モードについて ~PostgreSQLバージョン15対応版~(第34回PostgreSQLアンカンファレンス...オンライン物理バックアップの排他モードと非排他モードについて ~PostgreSQLバージョン15対応版~(第34回PostgreSQLアンカンファレンス...
オンライン物理バックアップの排他モードと非排他モードについて ~PostgreSQLバージョン15対応版~(第34回PostgreSQLアンカンファレンス...
NTT DATA Technology & Innovation
 
Introduction to PySpark
Introduction to PySparkIntroduction to PySpark
Introduction to PySpark
Russell Jurney
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer Works
EDB
 
The PostgreSQL Query Planner
The PostgreSQL Query PlannerThe PostgreSQL Query Planner
The PostgreSQL Query Planner
Command Prompt., Inc
 
Airflowで真面目にjob管理
Airflowで真面目にjob管理Airflowで真面目にjob管理
Airflowで真面目にjob管理
msssgur
 

What's hot (20)

PostgreSql query planning and tuning
PostgreSql query planning and tuningPostgreSql query planning and tuning
PostgreSql query planning and tuning
 
Linking Metrics to Logs using Loki
Linking Metrics to Logs using LokiLinking Metrics to Logs using Loki
Linking Metrics to Logs using Loki
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
patroni-based citrus high availability environment deployment
patroni-based citrus high availability environment deploymentpatroni-based citrus high availability environment deployment
patroni-based citrus high availability environment deployment
 
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonThrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased Comparison
 
MySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsMySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 Tips
 
Large Table Partitioning with PostgreSQL and Django
 Large Table Partitioning with PostgreSQL and Django Large Table Partitioning with PostgreSQL and Django
Large Table Partitioning with PostgreSQL and Django
 
ClickHouse Introduction by Alexander Zaitsev, Altinity CTO
ClickHouse Introduction by Alexander Zaitsev, Altinity CTOClickHouse Introduction by Alexander Zaitsev, Altinity CTO
ClickHouse Introduction by Alexander Zaitsev, Altinity CTO
 
ClickHouse Query Performance Tips and Tricks, by Robert Hodges, Altinity CEO
ClickHouse Query Performance Tips and Tricks, by Robert Hodges, Altinity CEOClickHouse Query Performance Tips and Tricks, by Robert Hodges, Altinity CEO
ClickHouse Query Performance Tips and Tricks, by Robert Hodges, Altinity CEO
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadinC* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
 
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdfDeep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
 
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
Cloud Native PostgreSQL
Cloud Native PostgreSQLCloud Native PostgreSQL
Cloud Native PostgreSQL
 
Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performance
 
オンライン物理バックアップの排他モードと非排他モードについて ~PostgreSQLバージョン15対応版~(第34回PostgreSQLアンカンファレンス...
オンライン物理バックアップの排他モードと非排他モードについて ~PostgreSQLバージョン15対応版~(第34回PostgreSQLアンカンファレンス...オンライン物理バックアップの排他モードと非排他モードについて ~PostgreSQLバージョン15対応版~(第34回PostgreSQLアンカンファレンス...
オンライン物理バックアップの排他モードと非排他モードについて ~PostgreSQLバージョン15対応版~(第34回PostgreSQLアンカンファレンス...
 
Introduction to PySpark
Introduction to PySparkIntroduction to PySpark
Introduction to PySpark
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer Works
 
The PostgreSQL Query Planner
The PostgreSQL Query PlannerThe PostgreSQL Query Planner
The PostgreSQL Query Planner
 
Airflowで真面目にjob管理
Airflowで真面目にjob管理Airflowで真面目にjob管理
Airflowで真面目にjob管理
 

Viewers also liked

MySQL Architecture and Engine
MySQL Architecture and EngineMySQL Architecture and Engine
MySQL Architecture and Engine
Abdul Manaf
 
MariaDB: Connect Storage Engine
MariaDB: Connect Storage EngineMariaDB: Connect Storage Engine
MariaDB: Connect Storage Engine
Kangaroot
 
InnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevInnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter Zaitsev
Fuenteovejuna
 
Building High Performance MySql Query Systems And Analytic Applications
Building High Performance MySql Query Systems And Analytic ApplicationsBuilding High Performance MySql Query Systems And Analytic Applications
Building High Performance MySql Query Systems And Analytic Applications
guest40cda0b
 
Zurich2007 MySQL Query Optimization
Zurich2007 MySQL Query OptimizationZurich2007 MySQL Query Optimization
Zurich2007 MySQL Query Optimization
Hiệp Lê Tuấn
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
Alexander Rubin
 
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
Dave Stokes
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query Optimization
MYXPLAIN
 
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
Pythian
 
Mysql query optimization
Mysql query optimizationMysql query optimization
Mysql query optimization
Baohua Cai
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
MYXPLAIN
 
Locking and Concurrency Control
Locking and Concurrency ControlLocking and Concurrency Control
Locking and Concurrency Control
Morgan Tocker
 
Tunning sql query
Tunning sql queryTunning sql query
Tunning sql query
vuhaininh88
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
Sveta Smirnova
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema Tuning
MYXPLAIN
 
My sql optimization
My sql optimizationMy sql optimization
My sql optimization
PrelovacMedia
 
Optimizing MySQL
Optimizing MySQLOptimizing MySQL
Optimizing MySQL
Morgan Tocker
 
Webinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuningWebinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuning
晓 周
 
MySQL Query Optimization.
MySQL Query Optimization.MySQL Query Optimization.
MySQL Query Optimization.
Remote MySQL DBA
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Jaime Crespo
 

Viewers also liked (20)

MySQL Architecture and Engine
MySQL Architecture and EngineMySQL Architecture and Engine
MySQL Architecture and Engine
 
MariaDB: Connect Storage Engine
MariaDB: Connect Storage EngineMariaDB: Connect Storage Engine
MariaDB: Connect Storage Engine
 
InnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevInnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter Zaitsev
 
Building High Performance MySql Query Systems And Analytic Applications
Building High Performance MySql Query Systems And Analytic ApplicationsBuilding High Performance MySql Query Systems And Analytic Applications
Building High Performance MySql Query Systems And Analytic Applications
 
Zurich2007 MySQL Query Optimization
Zurich2007 MySQL Query OptimizationZurich2007 MySQL Query Optimization
Zurich2007 MySQL Query Optimization
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query Optimization
 
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
 
Mysql query optimization
Mysql query optimizationMysql query optimization
Mysql query optimization
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
 
Locking and Concurrency Control
Locking and Concurrency ControlLocking and Concurrency Control
Locking and Concurrency Control
 
Tunning sql query
Tunning sql queryTunning sql query
Tunning sql query
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema Tuning
 
My sql optimization
My sql optimizationMy sql optimization
My sql optimization
 
Optimizing MySQL
Optimizing MySQLOptimizing MySQL
Optimizing MySQL
 
Webinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuningWebinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuning
 
MySQL Query Optimization.
MySQL Query Optimization.MySQL Query Optimization.
MySQL Query Optimization.
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
 

Similar to MySQL Query Optimization

MySQL 8.0 Released Update
MySQL 8.0 Released UpdateMySQL 8.0 Released Update
MySQL 8.0 Released Update
Keith Hollman
 
Five more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQLFive more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQL
Connor McDonald
 
Solr JDBC - Lucene/Solr Revolution 2016
Solr JDBC - Lucene/Solr Revolution 2016Solr JDBC - Lucene/Solr Revolution 2016
Solr JDBC - Lucene/Solr Revolution 2016
Kevin Risden
 
Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0
oysteing
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
Carlos Oliveira
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
mCloud
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
mCloud
 
Advanced SQL - Quebec 2014
Advanced SQL - Quebec 2014Advanced SQL - Quebec 2014
Advanced SQL - Quebec 2014
Connor McDonald
 
Day 6.pptx
Day 6.pptxDay 6.pptx
Day 6.pptx
atreesgalaxy
 
Lessons learned from Isbank - A Story of a DB2 for z/OS Initiative
Lessons learned from Isbank - A Story of a DB2 for z/OS InitiativeLessons learned from Isbank - A Story of a DB2 for z/OS Initiative
Lessons learned from Isbank - A Story of a DB2 for z/OS Initiative
Cuneyt Goksu
 
Solr JDBC: Presented by Kevin Risden, Avalon Consulting
Solr JDBC: Presented by Kevin Risden, Avalon ConsultingSolr JDBC: Presented by Kevin Risden, Avalon Consulting
Solr JDBC: Presented by Kevin Risden, Avalon Consulting
Lucidworks
 
Top 10 tips for Oracle performance
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performance
Guy Harrison
 
MySQL 5.7: Core Server Changes
MySQL 5.7: Core Server ChangesMySQL 5.7: Core Server Changes
MySQL 5.7: Core Server Changes
Morgan Tocker
 
How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15
oysteing
 
OQL querying and indexes with Apache Geode (incubating)
OQL querying and indexes with Apache Geode (incubating)OQL querying and indexes with Apache Geode (incubating)
OQL querying and indexes with Apache Geode (incubating)
Jason Huynh
 
What's New MySQL 8.0?
What's New MySQL 8.0?What's New MySQL 8.0?
What's New MySQL 8.0?
OracleMySQL
 
MySQL Troubleshooting with the Performance Schema
MySQL Troubleshooting with the Performance SchemaMySQL Troubleshooting with the Performance Schema
MySQL Troubleshooting with the Performance Schema
Sveta Smirnova
 
Confoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsConfoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & Histograms
Dave Stokes
 
How to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinarHow to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinar
oysteing
 
Performance Schema and Sys Schema in MySQL 5.7
Performance Schema and Sys Schema in MySQL 5.7Performance Schema and Sys Schema in MySQL 5.7
Performance Schema and Sys Schema in MySQL 5.7
Mark Leith
 

Similar to MySQL Query Optimization (20)

MySQL 8.0 Released Update
MySQL 8.0 Released UpdateMySQL 8.0 Released Update
MySQL 8.0 Released Update
 
Five more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQLFive more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQL
 
Solr JDBC - Lucene/Solr Revolution 2016
Solr JDBC - Lucene/Solr Revolution 2016Solr JDBC - Lucene/Solr Revolution 2016
Solr JDBC - Lucene/Solr Revolution 2016
 
Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
 
Advanced SQL - Quebec 2014
Advanced SQL - Quebec 2014Advanced SQL - Quebec 2014
Advanced SQL - Quebec 2014
 
Day 6.pptx
Day 6.pptxDay 6.pptx
Day 6.pptx
 
Lessons learned from Isbank - A Story of a DB2 for z/OS Initiative
Lessons learned from Isbank - A Story of a DB2 for z/OS InitiativeLessons learned from Isbank - A Story of a DB2 for z/OS Initiative
Lessons learned from Isbank - A Story of a DB2 for z/OS Initiative
 
Solr JDBC: Presented by Kevin Risden, Avalon Consulting
Solr JDBC: Presented by Kevin Risden, Avalon ConsultingSolr JDBC: Presented by Kevin Risden, Avalon Consulting
Solr JDBC: Presented by Kevin Risden, Avalon Consulting
 
Top 10 tips for Oracle performance
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performance
 
MySQL 5.7: Core Server Changes
MySQL 5.7: Core Server ChangesMySQL 5.7: Core Server Changes
MySQL 5.7: Core Server Changes
 
How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15
 
OQL querying and indexes with Apache Geode (incubating)
OQL querying and indexes with Apache Geode (incubating)OQL querying and indexes with Apache Geode (incubating)
OQL querying and indexes with Apache Geode (incubating)
 
What's New MySQL 8.0?
What's New MySQL 8.0?What's New MySQL 8.0?
What's New MySQL 8.0?
 
MySQL Troubleshooting with the Performance Schema
MySQL Troubleshooting with the Performance SchemaMySQL Troubleshooting with the Performance Schema
MySQL Troubleshooting with the Performance Schema
 
Confoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsConfoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & Histograms
 
How to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinarHow to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinar
 
Performance Schema and Sys Schema in MySQL 5.7
Performance Schema and Sys Schema in MySQL 5.7Performance Schema and Sys Schema in MySQL 5.7
Performance Schema and Sys Schema in MySQL 5.7
 

More from Morgan Tocker

Introducing Spirit - Online Schema Change
Introducing Spirit - Online Schema ChangeIntroducing Spirit - Online Schema Change
Introducing Spirit - Online Schema Change
Morgan Tocker
 
MySQL Usability Guidelines
MySQL Usability GuidelinesMySQL Usability Guidelines
MySQL Usability Guidelines
Morgan Tocker
 
My First 90 days with Vitess
My First 90 days with VitessMy First 90 days with Vitess
My First 90 days with Vitess
Morgan Tocker
 
FOSDEM MySQL and Friends Devroom
FOSDEM MySQL and Friends DevroomFOSDEM MySQL and Friends Devroom
FOSDEM MySQL and Friends Devroom
Morgan Tocker
 
Introducing TiDB - Percona Live Frankfurt
Introducing TiDB - Percona Live FrankfurtIntroducing TiDB - Percona Live Frankfurt
Introducing TiDB - Percona Live Frankfurt
Morgan Tocker
 
TiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - Boston MySQL Meetup GroupTiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - Boston MySQL Meetup Group
Morgan Tocker
 
TiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction - San Francisco MySQL MeetupTiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction - San Francisco MySQL Meetup
Morgan Tocker
 
TiDB Introduction
TiDB IntroductionTiDB Introduction
TiDB Introduction
Morgan Tocker
 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server Defaults
Morgan Tocker
 
MySQL Cloud Service Deep Dive
MySQL Cloud Service Deep DiveMySQL Cloud Service Deep Dive
MySQL Cloud Service Deep Dive
Morgan Tocker
 
MySQL 5.7 + JSON
MySQL 5.7 + JSONMySQL 5.7 + JSON
MySQL 5.7 + JSON
Morgan Tocker
 
Using MySQL in Automated Testing
Using MySQL in Automated TestingUsing MySQL in Automated Testing
Using MySQL in Automated Testing
Morgan Tocker
 
Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7
Morgan Tocker
 
MySQL Performance Metrics that Matter
MySQL Performance Metrics that MatterMySQL Performance Metrics that Matter
MySQL Performance Metrics that Matter
Morgan Tocker
 
MySQL For Linux Sysadmins
MySQL For Linux SysadminsMySQL For Linux Sysadmins
MySQL For Linux Sysadmins
Morgan Tocker
 
MySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big DataMySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big Data
Morgan Tocker
 
MySQL NoSQL APIs
MySQL NoSQL APIsMySQL NoSQL APIs
MySQL NoSQL APIs
Morgan Tocker
 
MySQL 5.6 - Operations and Diagnostics Improvements
MySQL 5.6 - Operations and Diagnostics ImprovementsMySQL 5.6 - Operations and Diagnostics Improvements
MySQL 5.6 - Operations and Diagnostics Improvements
Morgan Tocker
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQL
Morgan Tocker
 
My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2
Morgan Tocker
 

More from Morgan Tocker (20)

Introducing Spirit - Online Schema Change
Introducing Spirit - Online Schema ChangeIntroducing Spirit - Online Schema Change
Introducing Spirit - Online Schema Change
 
MySQL Usability Guidelines
MySQL Usability GuidelinesMySQL Usability Guidelines
MySQL Usability Guidelines
 
My First 90 days with Vitess
My First 90 days with VitessMy First 90 days with Vitess
My First 90 days with Vitess
 
FOSDEM MySQL and Friends Devroom
FOSDEM MySQL and Friends DevroomFOSDEM MySQL and Friends Devroom
FOSDEM MySQL and Friends Devroom
 
Introducing TiDB - Percona Live Frankfurt
Introducing TiDB - Percona Live FrankfurtIntroducing TiDB - Percona Live Frankfurt
Introducing TiDB - Percona Live Frankfurt
 
TiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - Boston MySQL Meetup GroupTiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - Boston MySQL Meetup Group
 
TiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction - San Francisco MySQL MeetupTiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction - San Francisco MySQL Meetup
 
TiDB Introduction
TiDB IntroductionTiDB Introduction
TiDB Introduction
 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server Defaults
 
MySQL Cloud Service Deep Dive
MySQL Cloud Service Deep DiveMySQL Cloud Service Deep Dive
MySQL Cloud Service Deep Dive
 
MySQL 5.7 + JSON
MySQL 5.7 + JSONMySQL 5.7 + JSON
MySQL 5.7 + JSON
 
Using MySQL in Automated Testing
Using MySQL in Automated TestingUsing MySQL in Automated Testing
Using MySQL in Automated Testing
 
Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7
 
MySQL Performance Metrics that Matter
MySQL Performance Metrics that MatterMySQL Performance Metrics that Matter
MySQL Performance Metrics that Matter
 
MySQL For Linux Sysadmins
MySQL For Linux SysadminsMySQL For Linux Sysadmins
MySQL For Linux Sysadmins
 
MySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big DataMySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big Data
 
MySQL NoSQL APIs
MySQL NoSQL APIsMySQL NoSQL APIs
MySQL NoSQL APIs
 
MySQL 5.6 - Operations and Diagnostics Improvements
MySQL 5.6 - Operations and Diagnostics ImprovementsMySQL 5.6 - Operations and Diagnostics Improvements
MySQL 5.6 - Operations and Diagnostics Improvements
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQL
 
My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2
 

Recently uploaded

WEBINAR SLIDES: CCX for Cloud Service Providers
WEBINAR SLIDES: CCX for Cloud Service ProvidersWEBINAR SLIDES: CCX for Cloud Service Providers
WEBINAR SLIDES: CCX for Cloud Service Providers
Severalnines
 
YouTube SEO Mastery ......................
YouTube SEO Mastery ......................YouTube SEO Mastery ......................
YouTube SEO Mastery ......................
islamiato717
 
一比一原版英国牛津大学毕业证(oxon毕业证书)如何办理
一比一原版英国牛津大学毕业证(oxon毕业证书)如何办理一比一原版英国牛津大学毕业证(oxon毕业证书)如何办理
一比一原版英国牛津大学毕业证(oxon毕业证书)如何办理
avufu
 
Disk to Cloud: Abstract your File Operations with CBFS
Disk to Cloud: Abstract your File Operations with CBFSDisk to Cloud: Abstract your File Operations with CBFS
Disk to Cloud: Abstract your File Operations with CBFS
Ortus Solutions, Corp
 
ANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdfANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdf
sachin chaurasia
 
Design system: The basis for a consistent design
Design system: The basis for a consistent designDesign system: The basis for a consistent design
Design system: The basis for a consistent design
Ortus Solutions, Corp
 
Splunk_Remote_Work_Insights_Overview.pptx
Splunk_Remote_Work_Insights_Overview.pptxSplunk_Remote_Work_Insights_Overview.pptx
Splunk_Remote_Work_Insights_Overview.pptx
sudsdeep
 
Development of Chatbot Using AI\ML Technologies
Development of Chatbot Using AI\ML TechnologiesDevelopment of Chatbot Using AI\ML Technologies
Development of Chatbot Using AI\ML Technologies
MaisnamLuwangPibarel
 
Mumbai @Call @Girls Whatsapp 9930687706 With High Profile Service
Mumbai @Call @Girls Whatsapp 9930687706 With High Profile ServiceMumbai @Call @Girls Whatsapp 9930687706 With High Profile Service
Mumbai @Call @Girls Whatsapp 9930687706 With High Profile Service
kolkata dolls
 
WhatsApp Tracker - Tracking WhatsApp to Boost Online Safety.pdf
WhatsApp Tracker -  Tracking WhatsApp to Boost Online Safety.pdfWhatsApp Tracker -  Tracking WhatsApp to Boost Online Safety.pdf
WhatsApp Tracker - Tracking WhatsApp to Boost Online Safety.pdf
onemonitarsoftware
 
Kolkata @ℂall @Girls ꧁❤ 000000000 ❤꧂@ℂall @Girls Service Vip Top Model Safe
Kolkata @ℂall @Girls ꧁❤ 000000000 ❤꧂@ℂall @Girls Service Vip Top Model SafeKolkata @ℂall @Girls ꧁❤ 000000000 ❤꧂@ℂall @Girls Service Vip Top Model Safe
Kolkata @ℂall @Girls ꧁❤ 000000000 ❤꧂@ℂall @Girls Service Vip Top Model Safe
Misti Soneji
 
Addressing the Top 9 User Pain Points with Visual Design Elements.pptx
Addressing the Top 9 User Pain Points with Visual Design Elements.pptxAddressing the Top 9 User Pain Points with Visual Design Elements.pptx
Addressing the Top 9 User Pain Points with Visual Design Elements.pptx
Sparity1
 
Java SE 17 Study Guide for Certification - Chapter 01
Java SE 17 Study Guide for Certification - Chapter 01Java SE 17 Study Guide for Certification - Chapter 01
Java SE 17 Study Guide for Certification - Chapter 01
williamrobertherman
 
dachnug51 - HCLs evolution of the employee experience platform.pdf
dachnug51 - HCLs evolution of the employee experience platform.pdfdachnug51 - HCLs evolution of the employee experience platform.pdf
dachnug51 - HCLs evolution of the employee experience platform.pdf
DNUG e.V.
 
AI Chatbot Development – A Comprehensive Guide  .pdf
AI Chatbot Development – A Comprehensive Guide  .pdfAI Chatbot Development – A Comprehensive Guide  .pdf
AI Chatbot Development – A Comprehensive Guide  .pdf
ayushiqss
 
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
arvindkumarji156
 
How to debug ColdFusion Applications using “ColdFusion Builder extension for ...
How to debug ColdFusion Applications using “ColdFusion Builder extension for ...How to debug ColdFusion Applications using “ColdFusion Builder extension for ...
How to debug ColdFusion Applications using “ColdFusion Builder extension for ...
Ortus Solutions, Corp
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio, Inc.
 
What is OCR Technology and How to Extract Text from Any Image for Free
What is OCR Technology and How to Extract Text from Any Image for FreeWhat is OCR Technology and How to Extract Text from Any Image for Free
What is OCR Technology and How to Extract Text from Any Image for Free
TwisterTools
 
ℂall Girls in Surat 🔥 +91-7023059433 🔥 Best High ℂlass Surat Esℂorts Serviℂe ...
ℂall Girls in Surat 🔥 +91-7023059433 🔥 Best High ℂlass Surat Esℂorts Serviℂe ...ℂall Girls in Surat 🔥 +91-7023059433 🔥 Best High ℂlass Surat Esℂorts Serviℂe ...
ℂall Girls in Surat 🔥 +91-7023059433 🔥 Best High ℂlass Surat Esℂorts Serviℂe ...
nitu gupta#N06
 

Recently uploaded (20)

WEBINAR SLIDES: CCX for Cloud Service Providers
WEBINAR SLIDES: CCX for Cloud Service ProvidersWEBINAR SLIDES: CCX for Cloud Service Providers
WEBINAR SLIDES: CCX for Cloud Service Providers
 
YouTube SEO Mastery ......................
YouTube SEO Mastery ......................YouTube SEO Mastery ......................
YouTube SEO Mastery ......................
 
一比一原版英国牛津大学毕业证(oxon毕业证书)如何办理
一比一原版英国牛津大学毕业证(oxon毕业证书)如何办理一比一原版英国牛津大学毕业证(oxon毕业证书)如何办理
一比一原版英国牛津大学毕业证(oxon毕业证书)如何办理
 
Disk to Cloud: Abstract your File Operations with CBFS
Disk to Cloud: Abstract your File Operations with CBFSDisk to Cloud: Abstract your File Operations with CBFS
Disk to Cloud: Abstract your File Operations with CBFS
 
ANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdfANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdf
 
Design system: The basis for a consistent design
Design system: The basis for a consistent designDesign system: The basis for a consistent design
Design system: The basis for a consistent design
 
Splunk_Remote_Work_Insights_Overview.pptx
Splunk_Remote_Work_Insights_Overview.pptxSplunk_Remote_Work_Insights_Overview.pptx
Splunk_Remote_Work_Insights_Overview.pptx
 
Development of Chatbot Using AI\ML Technologies
Development of Chatbot Using AI\ML TechnologiesDevelopment of Chatbot Using AI\ML Technologies
Development of Chatbot Using AI\ML Technologies
 
Mumbai @Call @Girls Whatsapp 9930687706 With High Profile Service
Mumbai @Call @Girls Whatsapp 9930687706 With High Profile ServiceMumbai @Call @Girls Whatsapp 9930687706 With High Profile Service
Mumbai @Call @Girls Whatsapp 9930687706 With High Profile Service
 
WhatsApp Tracker - Tracking WhatsApp to Boost Online Safety.pdf
WhatsApp Tracker -  Tracking WhatsApp to Boost Online Safety.pdfWhatsApp Tracker -  Tracking WhatsApp to Boost Online Safety.pdf
WhatsApp Tracker - Tracking WhatsApp to Boost Online Safety.pdf
 
Kolkata @ℂall @Girls ꧁❤ 000000000 ❤꧂@ℂall @Girls Service Vip Top Model Safe
Kolkata @ℂall @Girls ꧁❤ 000000000 ❤꧂@ℂall @Girls Service Vip Top Model SafeKolkata @ℂall @Girls ꧁❤ 000000000 ❤꧂@ℂall @Girls Service Vip Top Model Safe
Kolkata @ℂall @Girls ꧁❤ 000000000 ❤꧂@ℂall @Girls Service Vip Top Model Safe
 
Addressing the Top 9 User Pain Points with Visual Design Elements.pptx
Addressing the Top 9 User Pain Points with Visual Design Elements.pptxAddressing the Top 9 User Pain Points with Visual Design Elements.pptx
Addressing the Top 9 User Pain Points with Visual Design Elements.pptx
 
Java SE 17 Study Guide for Certification - Chapter 01
Java SE 17 Study Guide for Certification - Chapter 01Java SE 17 Study Guide for Certification - Chapter 01
Java SE 17 Study Guide for Certification - Chapter 01
 
dachnug51 - HCLs evolution of the employee experience platform.pdf
dachnug51 - HCLs evolution of the employee experience platform.pdfdachnug51 - HCLs evolution of the employee experience platform.pdf
dachnug51 - HCLs evolution of the employee experience platform.pdf
 
AI Chatbot Development – A Comprehensive Guide  .pdf
AI Chatbot Development – A Comprehensive Guide  .pdfAI Chatbot Development – A Comprehensive Guide  .pdf
AI Chatbot Development – A Comprehensive Guide  .pdf
 
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
 
How to debug ColdFusion Applications using “ColdFusion Builder extension for ...
How to debug ColdFusion Applications using “ColdFusion Builder extension for ...How to debug ColdFusion Applications using “ColdFusion Builder extension for ...
How to debug ColdFusion Applications using “ColdFusion Builder extension for ...
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
 
What is OCR Technology and How to Extract Text from Any Image for Free
What is OCR Technology and How to Extract Text from Any Image for FreeWhat is OCR Technology and How to Extract Text from Any Image for Free
What is OCR Technology and How to Extract Text from Any Image for Free
 
ℂall Girls in Surat 🔥 +91-7023059433 🔥 Best High ℂlass Surat Esℂorts Serviℂe ...
ℂall Girls in Surat 🔥 +91-7023059433 🔥 Best High ℂlass Surat Esℂorts Serviℂe ...ℂall Girls in Surat 🔥 +91-7023059433 🔥 Best High ℂlass Surat Esℂorts Serviℂe ...
ℂall Girls in Surat 🔥 +91-7023059433 🔥 Best High ℂlass Surat Esℂorts Serviℂe ...
 

MySQL Query Optimization

  • 2. Query Optimization Morgan Tocker MySQL Community Manager October, 2014 Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
  • 3. Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 3
  • 4. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 4 Today’s Agenda Introduction World Schema IMDB Schema Advanced Topics 1 2 3 4
  • 5. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Introduction • SQL is declarative. • You say what you want, not how to process. • Can’t sight check a query to understand how it executes. • Database management chooses the best possible way -­‐ think like a GPS navigator. 5
  • 6. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | GPS Software 6
  • 7. MySQL Optimizer Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 7
  • 8. Diagnostic Commands Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • EXPLAIN (all versions) • EXPLAIN FORMAT=JSON (MySQL 5.6+) • Workbench supports visual format. • OPTIMIZER TRACE (MySQL 5.6+) 8
  • 9. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 9 Today’s Agenda Introduction World Schema IMDB Schema Advanced Topics 1 2 3 4
  • 10. The World Schema Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Contains cities, countries and language statistics. • Download From: • http://dev.mysql.com/doc/index-­‐other.html • Very small data set -­‐ good for learning. • Not perfect for explaining performance differences 10
  • 11. Find Countries in Asia with Population >5M Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 11 mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 'Asia' AND population > 5000000G ********************** 1. row ********************** id: 1 select_type: SIMPLE table: Country partitions: NULL type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 239 filtered: 4.76 Extra: Using where 1 row in set, 1 warning (0.00 sec)
  • 12. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 12 EXPLAIN: { "query_block": { "select_id": 1, "cost_info": { "query_cost": "53.80" }, "table": { "table_name": "Country", "access_type": "ALL", "rows_examined_per_scan": 239, "rows_produced_per_join": 11, "filtered": 4.7614, "cost_info": { "read_cost": "51.52", "eval_cost": "2.28", "prefix_cost": "53.80", "data_read_per_join": "2K" }, "used_columns": [ "Code", .. "Capital", "Code2" ], "attached_condition": "((`world`.`country`.`Continent` = 'Asia') and (`world`.`country`.`Population` > 5000000))" } } } FORMAT=JSON Available since MySQL 5.6. Expanded in 5.7 to add cost information.
  • 13. What indexes will make this query faster? Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Some suggestions: • Index on (population) • Index on (continent) • Index on (population, continent) • Index on (continent, population) 13 SELECT * FROM Country WHERE Continent = 'Asia' AND population > 5000000;
  • 14. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Next Question • Given so many choices, which is the best choice? • A good GPS navigator understands traffic and finds the fastest route. • A good query optimizer does similar. 14
  • 15. Query Optimizer Strategy Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Pick the plan that consumes the least amount of resources (CPU, IO). 15
  • 16. Query Optimizer Strategy (cont.) Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Use statistics available to identify good and bad plans. • Decisions may include: • Which indexes are good/bad. • Which order to join tables in. • If indexes should be read in a particular order to avoid sorting, or if sorting is not expensive. 16
  • 17. Using single column indexes first…. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • p (population) • c (continent) 17
  • 18. mysql> ALTER TABLE Country ADD INDEX p (population); Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 'Asia' AND population > 5000000G ************************ 1. row ************************ Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 18 id: 1 select_type: SIMPLE table: Country partitions: NULL type: ALL possible_keys: p key: NULL key_len: NULL ref: NULL rows: 239 filtered: 6.46 Extra: Using where 1 row in set, 1 warning (0.00 sec)
  • 19. Why would an index not be used? Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 19 mysql> EXPLAIN FORMAT=JSON SELECT * FROM Country FORCE INDEX (p) WHERE Continent = 'Asia' AND population > 5000000G ******* 1. row ******* EXPLAIN: { "query_block": { "select_id": 1, "cost_info": { "query_cost": "152.21" }, "table": { "table_name": "Country", "access_type": "range", "possible_keys": [ "p" ], Using MySQL 5.7+ mysql> EXPLAIN FORMAT=JSON SELECT * FROM Country WHERE Continent = 'Asia' AND population > 5000000G ******* 1. row ******* EXPLAIN: { "query_block": { "select_id": 1, "cost_info": { "query_cost": "53.80" }, "table": { "table_name": "Country", "access_type": "ALL", "possible_keys": [ "p" ],
  • 20. Modifying the query range: >50M Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 20 mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 'Asia' AND population > 50000000G ********************** 1. row ********************** id: 1 select_type: SIMPLE table: Country partitions: NULL type: range possible_keys: p key: p key_len: 4 ref: NULL rows: 24 filtered: 14.29 Extra: Using index condition; Using where 1 row in set, 1 warning (0.00 sec)
  • 21. mysql> ALTER TABLE Country ADD INDEX c (continent); Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 'Asia' AND population > 5000000G *********************** 1. row *********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 21 id: 1 select_type: SIMPLE table: Country partitions: NULL type: ref possible_keys: p,c key: c key_len: 1 ref: const rows: 51 filtered: 45.19 Extra: Using where 1 row in set, 1 warning (0.00 sec)
  • 22. Modifying the query range: >500M mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 'Asia' AND population > 500000000G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 22 id: 1 select_type: SIMPLE table: Country partitions: NULL type: range possible_keys: p,c key: p key_len: 4 ref: NULL rows: 2 filtered: 21.34 Extra: Using index condition; Using where 1 row in set, 1 warning (0.00 sec)
  • 23. Composite Indexes Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • On >50M which is better? • (population, continent) • (continent, population) 23
  • 24. mysql> ALTER TABLE Country ADD INDEX pc (pop..on, co..nt); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 'Asia' AND population > 50000000G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 24 id: 1 select_type: SIMPLE table: Country partitions: NULL type: ref possible_keys: p,c,pc key: c key_len: 1 ref: const rows: 51 filtered: 10.04 Extra: Using where 1 row in set, 1 warning (0.00 sec)
  • 25. FORCE INDEX mysql> EXPLAIN SELECT * FROM Country FORCE INDEX (pc) WHERE Continent = 'Asia' AND population > 50000000G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 25 id: 1 select_type: SIMPLE table: Country partitions: NULL type: range possible_keys: pc key: pc key_len: 4 ref: NULL rows: 24 filtered: 14.29 Extra: Using index condition 1 row in set, 1 warning (0.00 sec)
  • 26. mysql> EXPLAIN FORMAT=JSON SELECT * FROM Country FORCE INDEX (pc) WHERE Continent = 'Asia' AND population > 50000000G ********************** 1. row ********************** EXPLAIN: { "query_block": { "select_id": 1, "cost_info": { "query_cost": "34.61" }, "table": { Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 26 .. ], "key": "pc", "used_key_parts": [ "Population" ], .. Easier to see with JSON EXPLAIN
  • 27. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Rule of Thumb • Index on (const, range) instead of (range, const). • Applies to all databases. 27
  • 28. mysql> ALTER TABLE Country ADD INDEX cp (co..nt, pop..on); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT * FROM Country WHERE Continent = 'Asia' AND population > 50000000G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 28 id: 1 select_type: SIMPLE table: Country partitions: NULL type: range possible_keys: p,c,pc,cp key: cp key_len: 5 ref: NULL rows: 11 filtered: 100.00 Extra: Using index condition 1 row in set, 1 warning (0.00 sec)
  • 29. Where Population >N. Lower is better. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Cost Estimates 29 5M 50M 500M Table Scan 53.80 53.80 53.80 (Population) 152.21 34.61 3.81 (Continent) 28.20 28.20 28.20 (Population, 152.21 34.61 3.81 Continent) (Continent, Population) 24.83 16.41 3.81
  • 30. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 30 Today’s Agenda Introduction World Schema IMDB Schema Advanced Topics 1 2 3 4
  • 31. Examples Using IMDB Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Larger than world database schema. • Better to understand performance differences. • License requires data to be regenerated: • http://imdbpy.sourceforge.net/ 31
  • 32. mysql> EXPLAIN SELECT * FROM title WHERE title = 'The Lion King'G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 32 id: 1 select_type: SIMPLE table: title partitions: NULL type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 2387523 filtered: 0.50 Extra: Using where 1 row in set, 1 warning (0.00 sec) Ignore EXPLAIN time Actual run time is 2.9 seconds (Not shown)
  • 33. mysql> ALTER TABLE title ADD INDEX (title(100)); Query OK, 0 rows affected (4.73 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT * FROM title WHERE title = 'The Lion King'G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 33 id: 1 select_type: SIMPLE table: title partitions: NULL type: ref possible_keys: title key: title key_len: 102 ref: const rows: 4 filtered: 100.00 Extra: Using where 1 row in set, 1 warning (0.01 sec) From 2.9 seconds to 0.0 seconds (!!!)
  • 34. How does LIKE work? Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 34 mysql> EXPLAIN SELECT * FROM title WHERE title LIKE 'The Lion %'G ********************** 1. row ********************** id: 1 select_type: SIMPLE table: title partitions: NULL type: range possible_keys: title key: title key_len: 102 ref: NULL rows: 109 filtered: 100.00 Extra: Using where 1 row in set, 1 warning (0.01 sec)
  • 35. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | LIKE (cont.) 35 mysql> EXPLAIN SELECT * FROM title WHERE title LIKE 'The%'G ********************** 1. row ********************** id: 1 select_type: SIMPLE table: title partitions: NULL type: range possible_keys: title key: title key_len: 102 ref: NULL rows: 325548 filtered: 100.00 Extra: Using where 1 row in set, 1 warning (0.00 sec) VS Very Close. Estimate is that it is almost as fast to table scan. Run Time is 1.9 sec
  • 36. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Sorting 36 mysql> EXPLAIN SELECT * FROM title WHERE title = 'The Lion King' ORDER BY production_yearG ********************** 1. row ********************** id: 1 select_type: SIMPLE table: title partitions: NULL type: ref possible_keys: title key: title key_len: 102 ref: const rows: 4 filtered: 100.00 Extra: Using where; Using filesort 1 row in set, 1 warning (0.00 sec)
  • 37. SELECT movie_info.* FROM title INNER JOIN movie_info ON title.id=movie_info.movie_id WHERE title=‘The Lion King’G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 37 id: 1 select_type: SIMPLE table: movie_info type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 1212187 Extra: NULL ********************** 2. row ********************** id: 1 select_type: SIMPLE table: title type: eq_ref possible_keys: PRIMARY,title key: PRIMARY key_len: 4 ref: imdb.movie_info.movie_id rows: 1 Extra: Using where
  • 38. mysql> ALTER TABLE movie_info ADD INDEX mi (movie_id); Query OK, 0 rows affected (1.95 sec) Records: 0 Duplicates: 0 Warnings: 0 Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 38
  • 39. EXPLAIN SELECT movie_info.* FROM title INNER .. WHERE title='The Lion King'G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 39 id: 1 select_type: SIMPLE table: title type: ref possible_keys: PRIMARY,title key: title key_len: 102 ref: const rows: 4 Extra: Using where ********************** 2. row ********************** id: 1 select_type: SIMPLE table: movie_info type: ref possible_keys: mi key: mi key_len: 4 ref: imdb.title.id rows: 7 Extra: NULL
  • 40. mysql> EXPLAIN SELECT * FROM movie_info WHERE movie_id IN (SELECT id FROM title WHERE title = 'The Lion King')G ********************** 1. row ********************** Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 40 id: 1 select_type: SIMPLE table: title type: ref possible_keys: PRIMARY,title key: title key_len: 102 ref: const rows: 4 Extra: Using where ********************** 2. row ********************** id: 1 select_type: SIMPLE table: movie_info type: ref possible_keys: mi key: mi key_len: 4 ref: imdb.title.id rows: 7 Extra: NULL
  • 41. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 41 Today’s Agenda Introduction World Schema IMDB Schema Advanced Topics 1 2 3 4
  • 42. Optimizer Hints Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • USE INDEX • FORCE INDEX • IGNORE INDEX • STRAIGHT JOIN 42
  • 43. Optimizer Hints (cont.) Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 43 Expanded in MySQL 5.6 mysql> select @@optimizer_switchG ********************** 1. row ********************** @@optimizer_switch: index_merge=on,index_merge_union=on,index_merge_sort_union =on,index_merge_intersection=on,engine_condition_pushdown= on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,bl ock_nested_loop=on,batched_key_access=off,materialization= on,semijoin=on,loosescan=on,firstmatch=on,subquery_materia lization_cost_based=on,use_index_extensions=on,condition_f anout_filter=on 1 row in set (0.00 sec)
  • 44. Statistics Sampling Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Some decisions based on pre-­‐computed statistics. • For example, which order to join tables. • Accuracy of statistics greatly improved in MySQL 5.6. • Feature is called “InnoDB persistent statistics”. 44
  • 45. Optimizer Decision Visibility Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Intended for MySQL developer usage, and reporting optimizer bugs. • Yet totally practical for advanced usage :) 45 New to MySQL 5.6 SET optimizer_trace="enabled=on"; SELECT /* query here */; SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
  • 46. Optimizer Trace Output • Includes why decisions were not made: Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 46 .. "index": "pc", "ranges": [ "50000000 < Population" ], "index_dives_for_eq_ranges": true, "rowid_ordered": false, "using_mrr": false, "index_only": false, "rows": 24, "cost": 29.81, "chosen": false, "cause": "cost" },
  • 47. Too many indexes Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Slows down modifications due to maintenance cost. • i.e. Slower INSERT/UPDATE/DELETE • May also lead to worse decisions being made by optimizer. • Too many similar choices. • Only limited time window to evaluate suitability. • Space requirements in memory and storage. 47
  • 48. MySQL 5.7 and beyond Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • We are working on a new cost model. • Better adapt to new hardware with different costs (i.e. SSDs have cheap random IO). • Memory buffer aware cost estimates. 48
  • 49. Cost constants configurable per engine in 5.7 Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | • Previously hard-­‐coded constants. • Better adapts to differences in hardware. 49
  • 50. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 50