SlideShare a Scribd company logo
1 of 65
Download to read offline
Query Optimization with MySQL 5.6:
Old and New Tricks
Jaime Crespo
Senior MySQL Instructor
Percona Live London 2013
November 11, 2013
About This Presentation
•  Objective #1:
-  Get faster queries
•  Reminder of some well-known query
optimization techniques
-  Focus on new 5.6 query features
•  For other performance tricks with MySQL &
InnoDB:
-  InnoDB Architecture and Performance
Optimization with Peter Zaitsev at 2pm

2

www.percona.com
Agenda
1.  Introduction and Environment Setup
2.  Server Update and Configuration
3.  Tools for Query Profiling
4.  Indexing: Old and New Strategies
5.  New Query Planner Features
6.  Results and wrap-up

3

www.percona.com
Example Platform
•  Sample LAMP Application
-  'My Movies' PHP Script
-  Example movie database

•  Distributed as a VirtualBox VM
- 
- 
- 
- 

Import percona_live.ova
User: percona / pass: percona
Make sure httpd and PS are running
Optionally: startxfce4

•  Currently no design optimization, no indexes
(except PK), no proper query design
4

www.percona.com
Query Optimization with MySQL 5.6: Old and New Tricks

SERVER UPDATE AND
CONFIGURATION
5

www.percona.com

5
Basic Configuration Review
•  InnoDB as the default engine (default in 5.5)
•  InnoDB File Per Table (default in 5.6)
•  Larger Buffer Pool and Transaction Log
(changed in 5.6)
•  Row Based Replication and tx_isolation
•  Performance Schema overhead (enabled by
default in 5.6)
•  InnoDB page checksum (changed in 5.6)
•  InnoDB Old Blocks time (changed in 5.6)

6

www.percona.com
Query Optimization with MySQL 5.6: Old and New Tricks

TOOLS FOR QUERY PROFILING
7

www.percona.com

5
EXPLAIN
•  EXPLAIN is most important tool for the
MySQL DBA and Developer
•  It provides:
- 
- 
- 
- 

8

The query plan strategy
The index(es) to be used
The order of table access
An estimation of the number of rows to be
examined!

www.percona.com

5
EXPLAIN on DML
•  EXPLAIN is now available also for
INSERT, UPDATE and DELETE
mysql> EXPLAIN DELETE FROM title WHERE title = 'Pilot'G!
********************* 1. row *********************!
id: 1!
select_type: SIMPLE!
table: title!
type: range!
possible_keys: PRIMARY,title!
key: title!
key_len: 77!
ref: NULL!
rows: 1380!
Extra: Using where!
1 row in set (0.00 sec)!
9

www.percona.com

5
Structured EXPLAIN
mysql> EXPLAIN FORMAT=JSON SELECT COUNT(*) FROM title 

WHERE (title = 'Pilot' OR production_year > 2010) 

AND kind_id < 4G

*************************** 1. row ***************************!
EXPLAIN: {!
"query_block": {!
"select_id": 1,!
"table": {!
"table_name": "title",!
"access_type": "index_merge",!
"possible_keys": [!
"title",!
"production_year"!
],!
"key": "sort_union(title,production_year)",!
"key_length": "77,5",!
"rows": 4434,!
"filtered": 100,!
"attached_condition": "(((`imdb`.`title`.`title` = 'Pilot') or
(`imdb`.`title`.`production_year` > 2010)) and
(`imdb`.`title`.`kind_id` < 4))"!
}!
}!
}!

10

www.percona.com

5
Optimizer Trace
•  Allows profiling of MySQL query planner
•  It shows not only information about the
final query plan (EXPLAIN), but also about
other discarded strategies, and its
“execution cost”
•  It can be accessed via the
INFORMATION_SCHEMA database
•  It is off by default
11

www.percona.com

5
Checking the Optimizer Trace
•  mysql> SET optimizer_trace="enabled=on";
•  mysql> SELECT COUNT(*) FROM title WHERE
(title = 'Pilot' OR production_year >
2010) AND kind_id < 4;
+----------+
| COUNT(*) |
+----------+
|
3050 |
+----------+
1 row in set (0.01 sec)
•  mysql> SELECT trace FROM
information_schema.optimizer_trace;
12

www.percona.com

5
Checking the Optimizer Trace
(cont.)
{
"range_scan_alternatives": [
{
"index": "production_year",
"ranges": [
"2010 < production_year"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": true,
"rows": 3054,
"cost": 615.16,
"chosen": true
}
],
"index_to_merge": "production_year",
"cumulated_cost": 905.69
}
],
"cost_of_reading_ranges": 905.69,
"cost_sort_rowid_and_read_disk": 3672.8,
"cost_duplicate_removal": 9467.6,
"total_cost": 14046
}
],
"chosen_range_access_summary": {
"range_access_plan": {
"type": "index_merge",
"index_merge_of": [

13

www.percona.com

5
Limitations of EXPLAIN
•  It does not really execute the query
-  Not even subqueries any more

•  The row statistics can be sometimes very
off
•  It does not give us information about
temporary tables (on disk or memory?),
subtask timing, query cache hit, etc.

14

www.percona.com

5
Handler Statistics
•  They provide time & memory status-independency:
mysql> SHOW SESSION STATUS LIKE 'Hand%';

+----------------------------+-------+

| Variable_name
| Value |

+----------------------------+-------+

| Handler_commit
| 1
|

| Handler_delete
| 0
|

| Handler_discover
| 0
|

| Handler_external_lock
| 2
|

| Handler_mrr_init
| 0
|

| Handler_prepare
| 0
|

| Handler_read_first
| 0
|

| Handler_read_key
| 1
|

| Handler_read_last
| 0
|

| Handler_read_next
| 266
|

| Handler_read_prev
| 0
|

| Handler_read_rnd
| 0
|

| Handler_read_rnd_next
| 0
|

| Handler_rollback
| 0
|

| Handler_savepoint
| 0
|

| Handler_savepoint_rollback | 0
|

| Handler_update
| 0
|

| Handler_write
| 0
|

+----------------------------+-------+

18 rows in set (0.00 sec)

15

www.percona.com

5
Slow Query Log and pt-querydigest
•  Works on all MySQL versions
-  Specially useful in combination with the
extended log statistics in Percona Server
SET GLOBAL log_slow_verbosity = 'full';

!
# Time: 130601 8:01:06.058915
# User@Host: root[root] @ localhost [] Id: 42
# Schema: imdb Last_errno: 0 Killed: 0
# Query_time: 7.725616 Lock_time: 0.000328 Rows_sent: 4 Rows_examined: 1543720
Rows_affected: 0
# Bytes_sent: 272 Tmp_tables: 0 Tmp_disk_tables: 0 Tmp_table_sizes: 0
# QC_Hit: No Full_scan: Yes Full_join: No Tmp_table: No Tmp_table_on_disk: No
# Filesort: No Filesort_on_disk: No Merge_passes: 0
SET timestamp=1370073666;
SELECT id,title,production_year FROM title WHERE title = 'Bambi';

!

16

www.percona.com
pt-query-digest
slow query log
general query log

tcpdump

pt-query-digest

report

binary log

process list

input
17

tool

output
www.percona.com
pt-query-digest Report
# Profile!
# Rank Query ID
Response time
Calls R/Call Apdx V/M
Item!
# ==== ================== =============== ===== ======= ==== ===== =======!
#
1 0xA8D2BBDE7EBE7822 4932.2992 28.8%
78 63.2346 0.00 5.22 SELECT person_info!
#
2 0xFE25DAF5DBB71F49 4205.2160 24.6%
130 32.3478 0.00 3.47 SELECT title!
#
3 0x70DAC639802CA233 1299.6269 7.6%
14 92.8305 0.00 0.17 SELECT cast_info!
#
4 0xE336B880F4FEC4B8 1184.5101 6.9%
294 4.0289 0.36 2.29 SELECT cast_info!
#
5 0x60550B93960F1837 905.1648 5.3%
60 15.0861 0.05 1.33 SELECT name!
#
6 0xF46D5C09B4E0CA2F 777.2446 4.5% 16340 0.0476 1.00 0.17 SELECT char_name!
#
7 0x09FCFFF0E5BC929F 747.4346 4.4%
130 5.7495 0.53 7.69 SELECT name!
#
8 0x9433950BE12B9470 744.1755 4.4% 14368 0.0518 1.00 0.18 SELECT name!
#
9 0x4DC0E044996DA715 448.5637 2.6%
130 3.4505 0.65 8.31 SELECT title!
#
10 0x09FB72D72ED18E93 361.1904 2.1%
78 4.6306 0.28 1.89 SELECT cast_info title

#!
# Query 1: 0.02 QPS, 1.40x concurrency, ID 0xA8D2BBDE7EBE7822 at byte 25744265!
# Attribute
pct
total
min
max
avg
95% stddev median!
# ============ === ======= ======= ======= ======= ======= ======= =======!
# Count
0
78!
# Exec time
28
4932s
11s
109s
63s
88s
18s
63s!
# Lock time
0
724ms
36us
642ms
9ms
11ms
72ms
93us!
# Rows sent
7 12.95k
86
334 170.05 329.68
89.24 136.99!
# Rows examine 14 168.99M
2.17M
2.17M
2.17M
2.17M
0
2.17M!
# Rows affecte
0
0
0
0
0
0
0
0!
# Rows read
7 12.95k
86
334 170.05 329.68
89.24 136.99!
# Bytes sent
5
2.39M 16.18k 54.14k 31.43k 54.03k 13.47k 25.99k!

18

www.percona.com
performance_schema
•  Integrated profiling tool
•  Not new, but improved significately for 5.6
•  Substitutes the old PROFILING interface
•  Can have a 5-10% overhead by default

•  Can be used to monitor several runtime
statistics, among them, query execution
statistics of the latest queries:
SELECT * FROM
performance_schema.events_statements_history_long

!

19

www.percona.com
Query Optimization with MySQL 5.6: Old and New Tricks

INDEXING: OLD AND NEW
STRATEGIES
20

www.percona.com

5
Advantages of Indexing
• 
• 
• 
• 

21

Faster Filtering
Faster Column Return (Covering Index)
Faster Ordering
Certain Faster Functions (max, min)

www.percona.com
Faster Filtering
•  Indexes allow to return results by reading
a fewer number of records
-  We should minimize the rows value in
EXPLAIN/Handlers

-  const > ref > range > index > ALL!

•  Usually, only a single index can be used
per table access
-  Compound indexes may provide higher
filtering rate
22

www.percona.com
Compound Indexes
•  Column Order in Index Does Matter:
-  Order by decreasing selectivity
-  Only one range access per index*

23

www.percona.com
Covering Index Technique
•  Additional columns can be indexed in
order to speed-up column retrieval
•  Specially interesting in n-to-n relationship
tables
•  Not possible if the number or size of
columns is too large (e.g. SELECT *,
BLOBs)

24

www.percona.com
Index Condition Pushdown
•  Let’s prepare a use case:
UPDATE cast_info SET note = left(note,
250);

ALTER TABLE cast_info MODIFY note
varchar(250), ADD INDEX role_id_note
(role_id, note); !

•  We want to execute:
SELECT * FROM cast_info 

WHERE role_id = 1 

and note like '%Jaime%‘;!
25

www.percona.com

5
Without ICP (5.5)
mysql> EXPLAIN SELECT * 

FROM cast_info 

WHERE role_id = 1 

and note like '%Jaime%'G

********** 1. row **********

id: 1

select_type: SIMPLE

table: cast_info

type: ref

possible_keys: role_id_note

key: role_id_note

key_len: 4

ref: const

rows: 11553718

Extra: Using where

1 row in set (0.01 sec)!

26

mysql> SHOW STATUS like 'Hand
%';!

+----------------------------+---------+!
| Variable_name
| Value
|!
+----------------------------+---------+!
| Handler_commit
| 1
|!
| Handler_delete
| 0
|!
| Handler_discover
| 0
|!
| Handler_prepare
| 0
|!
| Handler_read_first
| 0
|!
| Handler_read_key
| 1
|!
| Handler_read_last
| 0
|!
| Handler_read_next
| 8346769 |!
| Handler_read_prev
| 0
|!
| Handler_read_rnd
| 0
|!
| Handler_read_rnd_next
| 0
|!
| Handler_rollback
| 0
|!
| Handler_savepoint
| 0
|!
| Handler_savepoint_rollback | 0
|!
| Handler_update
| 0
|!
| Handler_write
| 0
|!
+----------------------------+---------+!
16 rows in set (0.00 sec)!

www.percona.com

5
With ICP (5.6)
mysql> EXPLAIN SELECT * 

FROM cast_info 

WHERE role_id = 1 and note like
'%Jaime%'G

************ 1. row ************

id: 1

select_type: SIMPLE

table: cast_info

type: ref

possible_keys: role_id_note

key: role_id_note

key_len: 4

ref: const

rows: 10259274

Extra: Using index
condition

1 row in set (0.00 sec)!

27

mysql> SHOW STATUS like 'Hand%';!
+----------------------------+-------+!
| Variable_name
| Value |!
+----------------------------+-------+!
| Handler_commit
| 1
|!
| Handler_delete
| 0
|!
| Handler_discover
| 0
|!
| Handler_external_lock
| 2
|!
| Handler_mrr_init
| 0
|!
| Handler_prepare
| 0
|!
| Handler_read_first
| 0
|!
| Handler_read_key
| 1
|!
| Handler_read_last
| 0
|!
| Handler_read_next
| 266
|!
| Handler_read_prev
| 0
|!
| Handler_read_rnd
| 0
|!
| Handler_read_rnd_next
| 0
|!
| Handler_rollback
| 0
|!
| Handler_savepoint
| 0
|!
| Handler_savepoint_rollback | 0
|!
| Handler_update
| 0
|!
| Handler_write
| 0
|!
+----------------------------+-------+!
18 rows in set (0.00 sec)

www.percona.com

5
Comparison of ICP Execution
•  Execution time for this example:
•  MySQL 5.5: 5.76 sec
•  MySQL 5.6: 1.09 sec
•  Over 5x faster
•  In this example, it would not work with a
prefix index (e.g. TEXT/BLOB), as it must
search the whole field
•  Fun fact: if covering technique is tried, it actually runs
slower than with ICP for this case/hardware (#68554)
28

www.percona.com

5
ICP and Indexing
•  ICP will change the way we index our tables
SELECT * FROM cast_info 

FORCE INDEX(person_role_id_role_id)

WHERE person_role_id > 0 

and person_role_id < 150000 

and role_id > 1 and role_id < 7;!

•  For example, a multiple column index can be, in
some cases, efficiently used for a range
condition on several columns:
• 

29

Effectiveness is highly dependent on how selective
is the second part of the index (an “index scan” is
still done at engine level)
www.percona.com
Index for Sorts
•  Additional columns can be indexed in
order to speed up ordering of rows
•  Only effective in simple patterns
•  GROUP BY operations are essentially
sorts
•  They can also be greatly improved
with the right indexes in some cases

30

www.percona.com
Multi-Range Read (MRR)
•  Reorders access to table data when using a
secondary index on disk for sequential I/O
-  Like ICP, its efficiency is highly dependent on
data distribution and memory contents

•  It also depends on hardware sequential
access (e.g., InnoDB’s read ahead)
-  May not be useful on SSDs

•  Not compatible with Using index
•  Only for range access and equi-joins
31

www.percona.com

5
MRR Example
•  We want to execute:
SELECT * FROM cast_info 

WHERE person_role_id > 0 

and person_role_id < 150000;

SELECT * FROM cast_info 

WHERE role_id = 3 

and person_role_id > 0 

and person_role_id < 500000;!

•  We will use these indexes, respectively:
ALTER TABLE cast_info 

ADD INDEX person_role_id (person_role_id);

ALTER TABLE cast_info 

ADD INDEX person_role_id_role_id (person_role_id,
role_id);!
32

www.percona.com

5
Without MRR (5.5)
mysql> EXPLAIN SELECT * FROM
cast_info FORCE
INDEX(person_role_id) WHERE
person_role_id > 0 and
person_role_id < 150000G

*********** 1. row ***********

id: 1

select_type: SIMPLE

table: cast_info

type: range

possible_keys: person_role_id

key: person_role_id

key_len: 5

ref: NULL

rows: 8966490

Extra: Using where

1 row in set (0.00 sec)!

33

mysql> SHOW STATUS like 'Hand%';!
+----------------------------+---------+!
| Variable_name
| Value
|!
+----------------------------+---------+!
| Handler_commit
| 1
|!
| Handler_delete
| 0
|!
| Handler_discover
| 0
|!
| Handler_prepare
| 0
|!
| Handler_read_first
| 0
|!
| Handler_read_key
| 1
|!
| Handler_read_last
| 0
|!
| Handler_read_next
| 4654312 |!
| Handler_read_prev
| 0
|!
| Handler_read_rnd
| 0
|!
| Handler_read_rnd_next
| 0
|!
| Handler_rollback
| 0
|!
| Handler_savepoint
| 0
|!
| Handler_savepoint_rollback | 0
|!
| Handler_update
| 0
|!
| Handler_write
| 0
|!
+----------------------------+---------+!
16 rows in set (0.00 sec)

www.percona.com

5
With MRR (5.6)
mysql> EXPLAIN SELECT * FROM
cast_info FORCE
INDEX(person_role_id) WHERE
person_role_id > 0 and
person_role_id < 150000G

*********** 1. row ***********

id: 1

select_type: SIMPLE

table: cast_info

type: range

possible_keys: person_role_id

key: person_role_id

key_len: 5

ref: NULL

rows: 8966490

Extra: Using index

condition; Using MRR

1 row in set (0.00 sec)!

34

mysql> SHOW STATUS like 'Hand%';!
+----------------------------+---------+!
| Variable_name
| Value
|!
+----------------------------+---------+!
| Handler_commit
| 1
|!
| Handler_delete
| 0
|!
| Handler_discover
| 0
|!
| Handler_external_lock
| 4
|!
| Handler_mrr_init
| 0
|!
| Handler_prepare
| 0
|!
| Handler_read_first
| 0
|!
| Handler_read_key
| 4654313 |!
| Handler_read_last
| 0
|!
| Handler_read_next
| 4654312 |!
| Handler_read_prev
| 0
|!
| Handler_read_rnd
| 4654312 |!
| Handler_read_rnd_next
| 0
|!
| Handler_rollback
| 0
|!
| Handler_savepoint
| 0
|!
| Handler_savepoint_rollback | 0
|!
| Handler_update
| 0
|!
| Handler_write
| 0
|!
+----------------------------+---------+!
18 rows in set (0.00 sec)

www.percona.com

5
Comparison of MRR
Execution
•  Execution time for this example:
• 
• 

MySQL 5.5: 4654312 rows in set (1 min 4.79 sec)
MySQL 5.6 (w/MRR, wo/ICP): 4654312 rows in set
(48.64 sec)

•  Consistent 33% execution improvement
for this test case
• 

Difficult to see for smaller resultsets

•  What has changed?
• 
• 

35

15% more “read_ahead”s, resulting in a 40% less data reads
Reordering has an overhead, can be tuned with
read_rnd_buffer_size
www.percona.com

5
What’s the Point for the
‘FORCE INDEX’?
•  For demonstration purposes (full table scan
is better here)
•  Some features are not properly detected yet:
SELECT * 

FROM cast_info FORCE INDEX(person_role_id_role_id) 

WHERE role_id = 3 

and person_role_id > 0 

and person_role_id < 500000;

•  Full table scan (any version): Empty set
(8.08 sec)
•  MySQL 5.5: Empty set (1 min 16.88 sec)
•  MySQL 5.6 ICP+MRR*: Empty set (0.46 sec)
*ICP is responsible for the dramatic change in execution time, not MRR

36

www.percona.com
Batched Key Access (BKA)
•  It retrieves keys in batches and allows
MRR usage for JOINs, as an alternative to
standard Nested Loop Join execution
•  Not enabled by default
SET optimizer_switch=

'mrr=on,mrr_cost_based=off,batched_key_access=on';
!

37

www.percona.com
Without BKA (5.5) - EXPLAIN
mysql> EXPLAIN 

SELECT cast_info.note, char_name.name 

FROM cast_info FORCE index(person_role_id) 

JOIN char_name 

ON cast_info.person_role_id = char_name.id 

WHERE cast_info.person_role_id > 0 and
cast_info.person_role_id < 150000


************ 1. row ************!
id: 1!
select_type: SIMPLE!
table: char_name!
type: range!
possible_keys: PRIMARY!
key: PRIMARY!
key_len: 4!
ref: NULL!
rows: 313782!
Extra: Using where!
...!

38

...!
************* 2. row *************
id: 1!
select_type: SIMPLE!
table: cast_info!
type: ref!
possible_keys: person_role_id!
key: person_role_id!
key_len: 5!
ref: imdb.char_name.id!
rows: 4!
Extra: NULL!
2 rows in set (0.00 sec)!

www.percona.com
Without BKA (5.5) - Handlers
mysql> SHOW STATUS like ‘Hand%’;!
+----------------------------+---------+

| Variable_name
| Value
|

+----------------------------+---------+

| Handler_commit
| 1
|

| Handler_delete
| 0
|

| Handler_discover
| 0
|

| Handler_prepare
| 0
|

| Handler_read_first
| 0
|

| Handler_read_key
| 150000 |

| Handler_read_last
| 0
|

| Handler_read_next
| 4804311 |

| Handler_read_prev
| 0
|

| Handler_read_rnd
| 0
|

| Handler_read_rnd_next
| 0
|

| Handler_rollback
| 0
|

| Handler_savepoint
| 0
|

| Handler_savepoint_rollback | 0
|

| Handler_update
| 0
|

| Handler_write
| 0
|

+----------------------------+---------+

18 rows in set (0.00 sec)!

39

www.percona.com
With BKA (5.6) - EXPLAIN
mysql> EXPLAIN 

SELECT cast_info.note, char_name.name 

FROM cast_info FORCE index(person_role_id)
JOIN char_name ON cast_info.person_role_id
= char_name.id 

WHERE cast_info.person_role_id > 0 and
cast_info.person_role_id < 150000G!

************ 1. row ************!
id: 1!
select_type: SIMPLE!
table: char_name!
type: range!
possible_keys: PRIMARY!
key: PRIMARY!
key_len: 4!
ref: NULL!
rows: 313782!
Extra: Using where

...!

40

...!
************ 2. row ************!
id: 1!
select_type: SIMPLE!
table: cast_info!
type: ref!
possible_keys: person_role_id!
key: person_role_id!
key_len: 5!
ref: imdb.char_name.id!
rows: 4!
Extra: Using join buffer
(Batched Key Access)!
2 rows in set (0.00 sec)!

www.percona.com
With BKA (5.6) - Handlers
mysql> SHOW STATUS like ‘Hand%’;

+----------------------------+---------+

| Variable_name
| Value
|

+----------------------------+---------+

| Handler_commit
| 1
|

| Handler_delete
| 0
|

| Handler_discover
| 0
|

| Handler_external_lock
| 6
|

| Handler_mrr_init
| 1
|

| Handler_prepare
| 0
|

| Handler_read_first
| 0
|

| Handler_read_key
| 4804312 |

| Handler_read_last
| 0
|

| Handler_read_next
| 4804311 |

| Handler_read_prev
| 0
|

| Handler_read_rnd
| 4654312 |

| Handler_read_rnd_next
| 0
|

| Handler_rollback
| 0
|

| Handler_savepoint
| 0
|

| Handler_savepoint_rollback | 0
|

| Handler_update
| 0
|

| Handler_write
| 0
|

+----------------------------+---------+

18 rows in set (0.00 sec)!

41

www.percona.com
Comparison of BKA
Execution
•  Execution time for this example:
• 
• 

MySQL 5.5: 4654312 rows in set (1 min 6.78 sec)
MySQL 5.6 (w/MRR, w/BKA): 4654312 rows in set
(1 min 0.47 sec)

•  The results are consistent between
executions, but the gain is not too big. But
if we change the join_buffer_size…
• 

MySQL 5.6 (w/MRR, w/BKA, join_buffer_size =
50M): 4654312 rows in set (19.54 sec)
(join_buffer_size does not affect execution time in the 5.5 version)

42

www.percona.com

5
Better Index Merge
• 

In this example, it avoids a full table scan:

mysql> EXPLAIN SELECT COUNT(*) FROM title WHERE (title =
'Pilot' OR production_year > 2010) AND kind_id < 4G
************************ 1. row ************************
id: 1
select_type: SIMPLE
table: title
type: index_merge
possible_keys: title,production_year
key: title,production_year
key_len: 77,5
ref: NULL
rows: 4434
Extra: Using sort_union(title,production_year);
Using where
1 row in set (0.00 sec)
(almost all titles have kind_id < 4)

• 
43

MySQL 5.5: 0.79s – MySQL 5.6: 0.01s
www.percona.com

5
Extended Secondary Keys
•  Implicit primary keys inside secondary keys
can be used for filtering (ref, range, etc), not
only for covering index or sorting.
•  Requires use_index_extensions=on
(default)
ALTER TABLE title add index
(title(25));
SELECT COUNT(*) FROM title
WHERE title = 'Pilot'
AND id BETWEEN 1000 AND 1000000;
44

www.percona.com

5
Extended Secondary Keys
mysql> EXPLAIN SELECT COUNT(*) FROM title WHERE
title = 'Pilot' AND id BETWEEN 1000 AND
1000000G
****************** 1. row *******************
id: 1
select_type: SIMPLE
table: title
type: range
possible_keys: PRIMARY,title
key: title
key_len: 81
ref: NULL
rows: 531
Extra: Using index condition; Using
where
1 row in set (0.00 sec)
45

www.percona.com

5
Duplicate Key Check
mysql> alter table title add index (production_year);!
Query OK, 0 rows affected (10.08 sec)!
Records: 0 Duplicates: 0 Warnings: 0!
mysql> alter table title add index (production_year);
Query OK, 0 rows affected, 1 warning (5.11 sec)
Records: 0 Duplicates: 0 Warnings: 1!
!
mysql> show warningsG!
************************* 1. row *************************!

Level: Note!
Code: 1831!
Message: Duplicate index 'production_year_2' defined on
the table 'imdb.title'. This is deprecated and will be
disallowed in a future release.!
1 row in set (0.00 sec)!
46

www.percona.com

5
pt-duplicate-key-checker
•  It detects duplicate and redundant
indexes
-  Including primary keys at the end of
secondary keys on InnoDB tables

47

www.percona.com
pt-index-usage & user_stats
•  Can detect unused indexes
-  That slow down writes
-  Can also affect reads!
-  Wasted memory
-  Query optimizer woes

48

www.percona.com
Query Optimization with MySQL 5.6: Old and New Tricks

NEW QUERY PLANNER FEATURES
49

www.percona.com

5
Filesort with Short LIMIT
•  Queries that require a filesort but only the first
records are selected can benefit from this
optimization:
mysql> EXPLAIN select * from movie_info 

ORDER BY info LIMIT 100G

********************* 1. row *********************

id: 1

select_type: SIMPLE

table: movie_info

type: ALL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: 6927988

Extra: Using filesort

1 row in set (0.00 sec)

!

Note: Both EXPLAIN and the STATUS Handlers show the same outcome
50

www.percona.com

5
Filesort with Short LIMIT
(cont.)
•  SELECT * FROM movie_info ORDER
BY info LIMIT 100;!
-  MySQL 5.5: 20.06 sec
-  MySQL 5.6 (P_S on): 9.14 sec
-  MySQL 5.6 (P_S off): 8.51 sec

•  Over 2x faster.
•  Exact speed-up may depend on the
original sort buffer size

51

www.percona.com

5
Subquery Optimization
•  Late Subquery Materialization
-  Useful for FROM subqueries if further
filtering is done
-  MySQL may also decide to create its own
index on a temporary table on memory

SELECT * FROM title JOIN (SELECT * FROM cast_info)
AS subselect ON subselect.movie_id = title.id WHERE
subselect.id = 1;


EXPLAIN: 0.00 sec (vs >2 min in 5.5)
EXECUTION: 1m 22 sec (vs > 4 min in 5.5)

52

www.percona.com

5
JOINs and Subqueries
•  Better detection of IN + non-dependent
subqueries
-  Many queries do not need to be converted
to a JOIN anymore for a proper execution
plan
-  MySQL decides what is the best strategy:
Materialization, Semi-Join, etc.

53

www.percona.com

5
That Subquery in 5.5
mysql> EXPLAIN SELECT * FROM title WHERE kind_id IN 

(SELECT id FROM kind_type WHERE kind='video game')G

*************************** 1. row ***************************

id: 1

select_type: PRIMARY

table: title

type: ALL

An index on
possible_keys: NULL

won't fix it!
key: NULL

key_len: NULL

ref: NULL

rows: 1567676

Extra: Using where

*************************** 2. row ***************************

id: 2

select_type: DEPENDENT SUBQUERY

table: kind_type

type: const

possible_keys: PRIMARY,kind_id

key: kind_id

key_len: 47

ref: const

rows: 1

Extra: Using index

2 rows in set (0.00 sec)!

54

title.kind_id

www.percona.com

5
That Subquery in 5.6
mysql> EXPLAIN SELECT * FROM title WHERE kind_id IN (SELECT id FROM kind_type WHERE
kind='video game')G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: kind_type

type: const

possible_keys: PRIMARY,kind

key: kind

key_len: 47

ref: const

rows: 1

Extra: Using index

*************************** 2. row ***************************

id: 1

select_type: SIMPLE

table: title

An index on title.kind_id
type: ALL

can and will be very useful!
possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: 1477989

Extra: Using where

2 rows in set (0.00 sec)!

55

www.percona.com

5
Join Order
•  Table order algorithm has been
optimized, which leads to better query
plans when joining many tables

56

www.percona.com

5
Persistent Optimizer
Statistics
•  InnoDB index statistics are no longer
discarded on server shutdown and
recomputed the next time a table is
accessed
•  Controlled by variable:
innodb_stats_persistent = ON
(default)
•  Remember that SHOW commands/accessing to
I_S do not automatically regenerate statistics by
default
57

www.percona.com

5
READ ONLY Transactions
•  START TRANSACTION READ ONLY;
-  It avoids some overhead of R/W
transactions (MVCC)
-  Automatically used for SELECT in
auto_commit
-  Once started, a new transaction has to be
started to perform a write

58

www.percona.com

5
Explicit Partition Selection
•  Usually unnecessary, as MySQL does
automatically partition pruning
•  Can be useful combined with some nondeterministic functions or in some special
cases:
SELECT count(*)
FROM title PARTITION(p01, p02);

59

www.percona.com

5
Query Optimization with MySQL 5.6: Old and New Tricks

RESULTS AND WRAP-UP
60

www.percona.com

5
5.6 is a Great Release
•  Many optimizer changes that can
potentially improve query execution time
•  Some of them are transparent, some
others require tuning and understanding
-  Some old tricks and indexing strategies
become obsolete in 5.6

•  pt-upgrade, from Percona Toolkit, and
Percona Playback can be great tools to
analyze improvements and regressions
61

www.percona.com
Where to Learn More ?
•  More query and server optimization
techniques in our training courses (
http://www.percona.com/training):

-  Scaling and Optimization for MySQL
-  Operations and Troubleshooting with
MySQL
-  Moving to MySQL 5.6
-  Analysing Queries with Percona Toolkit
-  Percona Server and Tools (NEW!)
•  Ask me for a discount (only to PLUK
attendants)!
62

www.percona.com
Where to Learn More ? (cont.)
•  Books:
•  High Performance MySQL, 3rd Edition
•  SQL Antipatterns
•  Free MySQL Webinars:
•  http://www.percona.com/resources/
mysql-webinars

63

www.percona.com
Percona Live MySQL
Conference and Expo 2014

•  For more information and to register:
Visit: http://www.percona.com/live/mysql-conference-2014/
Thank You!
Jaime Crespo
jaime.crespo@percona.com

65

www.percona.com
www.percona.com

More Related Content

What's hot

Indexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuningIndexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuningOSSCube
 
Mvcc in postgreSQL 권건우
Mvcc in postgreSQL 권건우Mvcc in postgreSQL 권건우
Mvcc in postgreSQL 권건우PgDay.Seoul
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performanceoysteing
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewOlav Sandstå
 
Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0Mydbops
 
M|18 Deep Dive: InnoDB Transactions and Write Paths
M|18 Deep Dive: InnoDB Transactions and Write PathsM|18 Deep Dive: InnoDB Transactions and Write Paths
M|18 Deep Dive: InnoDB Transactions and Write PathsMariaDB plc
 
MySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveMySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveSveta Smirnova
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performanceoysteing
 
JSON improvements in MySQL 8.0
JSON improvements in MySQL 8.0JSON improvements in MySQL 8.0
JSON improvements in MySQL 8.0Mydbops
 
[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2020] SQL Tuning[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2020] SQL TuningPgDay.Seoul
 
Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Mydbops
 
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdfProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdfJesmar Cannao'
 
HandsOn ProxySQL Tutorial - PLSC18
HandsOn ProxySQL Tutorial - PLSC18HandsOn ProxySQL Tutorial - PLSC18
HandsOn ProxySQL Tutorial - PLSC18Derek Downey
 
Query Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
Query Optimization with MySQL 8.0 and MariaDB 10.3: The BasicsQuery Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
Query Optimization with MySQL 8.0 and MariaDB 10.3: The BasicsJaime Crespo
 
Proxysql sharding
Proxysql shardingProxysql sharding
Proxysql shardingMarco Tusa
 
InnoDB MVCC Architecture (by 권건우)
InnoDB MVCC Architecture (by 권건우)InnoDB MVCC Architecture (by 권건우)
InnoDB MVCC Architecture (by 권건우)I Goo Lee.
 
ProxySQL High Avalability and Configuration Management Overview
ProxySQL High Avalability and Configuration Management OverviewProxySQL High Avalability and Configuration Management Overview
ProxySQL High Avalability and Configuration Management OverviewRené Cannaò
 
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZEMySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZENorvald Ryeng
 

What's hot (20)

Indexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuningIndexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuning
 
Mvcc in postgreSQL 권건우
Mvcc in postgreSQL 권건우Mvcc in postgreSQL 권건우
Mvcc in postgreSQL 권건우
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performance
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0
 
M|18 Deep Dive: InnoDB Transactions and Write Paths
M|18 Deep Dive: InnoDB Transactions and Write PathsM|18 Deep Dive: InnoDB Transactions and Write Paths
M|18 Deep Dive: InnoDB Transactions and Write Paths
 
MySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveMySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to Have
 
Percona toolkit
Percona toolkitPercona toolkit
Percona toolkit
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
 
JSON improvements in MySQL 8.0
JSON improvements in MySQL 8.0JSON improvements in MySQL 8.0
JSON improvements in MySQL 8.0
 
[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2020] SQL Tuning[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2020] SQL Tuning
 
Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.
 
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdfProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
 
HandsOn ProxySQL Tutorial - PLSC18
HandsOn ProxySQL Tutorial - PLSC18HandsOn ProxySQL Tutorial - PLSC18
HandsOn ProxySQL Tutorial - PLSC18
 
Query Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
Query Optimization with MySQL 8.0 and MariaDB 10.3: The BasicsQuery Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
Query Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
 
Proxysql sharding
Proxysql shardingProxysql sharding
Proxysql sharding
 
Explain that explain
Explain that explainExplain that explain
Explain that explain
 
InnoDB MVCC Architecture (by 권건우)
InnoDB MVCC Architecture (by 권건우)InnoDB MVCC Architecture (by 권건우)
InnoDB MVCC Architecture (by 권건우)
 
ProxySQL High Avalability and Configuration Management Overview
ProxySQL High Avalability and Configuration Management OverviewProxySQL High Avalability and Configuration Management Overview
ProxySQL High Avalability and Configuration Management Overview
 
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZEMySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
 

Viewers also liked

MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewMYXPLAIN
 
MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)Karthik .P.R
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningMYXPLAIN
 
MySQL 5.7 - What's new and How to upgrade
MySQL 5.7 - What's new and How to upgradeMySQL 5.7 - What's new and How to upgrade
MySQL 5.7 - What's new and How to upgradeAbel Flórez
 
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineApache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineDataWorks Summit
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleGuatemala User Group
 
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013Sergey Petrunya
 
CS 542 -- Query Optimization
CS 542 -- Query OptimizationCS 542 -- Query Optimization
CS 542 -- Query OptimizationJ Singh
 
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 Applicationsguest40cda0b
 
Zurich2007 MySQL Query Optimization
Zurich2007 MySQL Query OptimizationZurich2007 MySQL Query Optimization
Zurich2007 MySQL Query OptimizationHiệp Lê Tuấn
 
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 2014Dave Stokes
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query OptimizationMYXPLAIN
 
Mysql query optimization
Mysql query optimizationMysql query optimization
Mysql query optimizationBaohua Cai
 
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 SphinxPythian
 
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 TricksMYXPLAIN
 
Ansible for large scale deployment
Ansible for large scale deploymentAnsible for large scale deployment
Ansible for large scale deploymentRemote MySQL DBA
 

Viewers also liked (20)

MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)
 
Cost-Based query optimization
Cost-Based query optimizationCost-Based query optimization
Cost-Based query optimization
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema Tuning
 
project in DBMS
project in DBMSproject in DBMS
project in DBMS
 
MySQL 5.7 - What's new and How to upgrade
MySQL 5.7 - What's new and How to upgradeMySQL 5.7 - What's new and How to upgrade
MySQL 5.7 - What's new and How to upgrade
 
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineApache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
 
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
 
CS 542 -- Query Optimization
CS 542 -- Query OptimizationCS 542 -- Query Optimization
CS 542 -- Query Optimization
 
Mysql For Developers
Mysql For DevelopersMysql For Developers
Mysql For Developers
 
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
 
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 optimization
Mysql query optimizationMysql query optimization
Mysql 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
 
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
 
Ansible for large scale deployment
Ansible for large scale deploymentAnsible for large scale deployment
Ansible for large scale deployment
 

Similar to Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013

MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)Aurimas Mikalauskas
 
MySQL 5.7 in a Nutshell
MySQL 5.7 in a NutshellMySQL 5.7 in a Nutshell
MySQL 5.7 in a NutshellEmily Ikuta
 
MySQL 5.7 New Features for Developers
MySQL 5.7 New Features for DevelopersMySQL 5.7 New Features for Developers
MySQL 5.7 New Features for DevelopersZohar Elkayam
 
MySQL 5.7 - What's new, How to upgrade and Document Store
MySQL 5.7 - What's new, How to upgrade and Document StoreMySQL 5.7 - What's new, How to upgrade and Document Store
MySQL 5.7 - What's new, How to upgrade and Document StoreAbel Flórez
 
MySQL Tech Tour 2015 - 5.7 Whats new
MySQL Tech Tour 2015 - 5.7 Whats newMySQL Tech Tour 2015 - 5.7 Whats new
MySQL Tech Tour 2015 - 5.7 Whats newMark Swarbrick
 
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 ImprovementsMorgan Tocker
 
Performance Schema in Action: demo
Performance Schema in Action: demoPerformance Schema in Action: demo
Performance Schema in Action: demoSveta Smirnova
 
sveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in actionsveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in actionDariia Seimova
 
Configuring Sage 500 for Performance
Configuring Sage 500 for PerformanceConfiguring Sage 500 for Performance
Configuring Sage 500 for PerformanceRKLeSolutions
 
MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015Mario Beck
 
MySQL Cluster 7.3 Performance Tuning - Severalnines Slides
MySQL Cluster 7.3 Performance Tuning - Severalnines SlidesMySQL Cluster 7.3 Performance Tuning - Severalnines Slides
MySQL Cluster 7.3 Performance Tuning - Severalnines SlidesSeveralnines
 
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
제3회난공불락 오픈소스 인프라세미나 - MySQL PerformanceTommy Lee
 
MySQL Manchester TT - 5.7 Whats new
MySQL Manchester TT - 5.7 Whats newMySQL Manchester TT - 5.7 Whats new
MySQL Manchester TT - 5.7 Whats newMark Swarbrick
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance AnalysisDatabase Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance AnalysisDAGEOP LTD
 
MySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete TutorialMySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete TutorialSveta Smirnova
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in ActionSveta Smirnova
 
What's New in MySQL 5.7
What's New in MySQL 5.7What's New in MySQL 5.7
What's New in MySQL 5.7Olivier DASINI
 
PostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major FeaturesPostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major FeaturesInMobi Technology
 
My sql 56_roadmap_april2012
My sql 56_roadmap_april2012My sql 56_roadmap_april2012
My sql 56_roadmap_april2012sqlhjalp
 

Similar to Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013 (20)

MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
 
MySQL 5.7 in a Nutshell
MySQL 5.7 in a NutshellMySQL 5.7 in a Nutshell
MySQL 5.7 in a Nutshell
 
MySQL 5.7 New Features for Developers
MySQL 5.7 New Features for DevelopersMySQL 5.7 New Features for Developers
MySQL 5.7 New Features for Developers
 
MySQL 5.7 - What's new, How to upgrade and Document Store
MySQL 5.7 - What's new, How to upgrade and Document StoreMySQL 5.7 - What's new, How to upgrade and Document Store
MySQL 5.7 - What's new, How to upgrade and Document Store
 
MySQL Tech Tour 2015 - 5.7 Whats new
MySQL Tech Tour 2015 - 5.7 Whats newMySQL Tech Tour 2015 - 5.7 Whats new
MySQL Tech Tour 2015 - 5.7 Whats new
 
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
 
Performance Schema in Action: demo
Performance Schema in Action: demoPerformance Schema in Action: demo
Performance Schema in Action: demo
 
sveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in actionsveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in action
 
Configuring Sage 500 for Performance
Configuring Sage 500 for PerformanceConfiguring Sage 500 for Performance
Configuring Sage 500 for Performance
 
MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015
 
MySQL Cluster 7.3 Performance Tuning - Severalnines Slides
MySQL Cluster 7.3 Performance Tuning - Severalnines SlidesMySQL Cluster 7.3 Performance Tuning - Severalnines Slides
MySQL Cluster 7.3 Performance Tuning - Severalnines Slides
 
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
 
MySQL Manchester TT - 5.7 Whats new
MySQL Manchester TT - 5.7 Whats newMySQL Manchester TT - 5.7 Whats new
MySQL Manchester TT - 5.7 Whats new
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance AnalysisDatabase Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance Analysis
 
MySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete TutorialMySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete Tutorial
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
 
MySQL 5.7 what's new
MySQL 5.7 what's newMySQL 5.7 what's new
MySQL 5.7 what's new
 
What's New in MySQL 5.7
What's New in MySQL 5.7What's New in MySQL 5.7
What's New in MySQL 5.7
 
PostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major FeaturesPostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major Features
 
My sql 56_roadmap_april2012
My sql 56_roadmap_april2012My sql 56_roadmap_april2012
My sql 56_roadmap_april2012
 

More from Jaime Crespo

Haciendo copias de seguridad de todo el conocimiento humano con python y soft...
Haciendo copias de seguridad de todo el conocimiento humano con python y soft...Haciendo copias de seguridad de todo el conocimiento humano con python y soft...
Haciendo copias de seguridad de todo el conocimiento humano con python y soft...Jaime Crespo
 
Backing up Wikipedia Databases
Backing up Wikipedia DatabasesBacking up Wikipedia Databases
Backing up Wikipedia DatabasesJaime Crespo
 
Wikipedia Burgos devfest 2017
Wikipedia Burgos devfest 2017Wikipedia Burgos devfest 2017
Wikipedia Burgos devfest 2017Jaime Crespo
 
Scaling and hardware provisioning for databases (lessons learned at wikipedia)
Scaling and hardware provisioning for databases (lessons learned at wikipedia)Scaling and hardware provisioning for databases (lessons learned at wikipedia)
Scaling and hardware provisioning for databases (lessons learned at wikipedia)Jaime Crespo
 
MySQL Schema Design in Practice
MySQL Schema Design in PracticeMySQL Schema Design in Practice
MySQL Schema Design in PracticeJaime Crespo
 
MySQL at Wikipedia: How we do relational data at the Wikimedia Foundation
MySQL at Wikipedia: How we do relational data at the Wikimedia FoundationMySQL at Wikipedia: How we do relational data at the Wikimedia Foundation
MySQL at Wikipedia: How we do relational data at the Wikimedia FoundationJaime Crespo
 
Query optimization: from 0 to 10 (and up to 5.7)
Query optimization: from 0 to 10 (and up to 5.7)Query optimization: from 0 to 10 (and up to 5.7)
Query optimization: from 0 to 10 (and up to 5.7)Jaime Crespo
 
MySQL GUI Administration
MySQL GUI AdministrationMySQL GUI Administration
MySQL GUI AdministrationJaime Crespo
 
Software libre para gestión de proyectos
Software libre para gestión de proyectosSoftware libre para gestión de proyectos
Software libre para gestión de proyectosJaime Crespo
 
warptalk: Experiencia de usuario y usabilidad
warptalk: Experiencia de usuario y usabilidadwarptalk: Experiencia de usuario y usabilidad
warptalk: Experiencia de usuario y usabilidadJaime Crespo
 

More from Jaime Crespo (11)

Haciendo copias de seguridad de todo el conocimiento humano con python y soft...
Haciendo copias de seguridad de todo el conocimiento humano con python y soft...Haciendo copias de seguridad de todo el conocimiento humano con python y soft...
Haciendo copias de seguridad de todo el conocimiento humano con python y soft...
 
Backing up Wikipedia Databases
Backing up Wikipedia DatabasesBacking up Wikipedia Databases
Backing up Wikipedia Databases
 
Wikipedia Burgos devfest 2017
Wikipedia Burgos devfest 2017Wikipedia Burgos devfest 2017
Wikipedia Burgos devfest 2017
 
Scaling and hardware provisioning for databases (lessons learned at wikipedia)
Scaling and hardware provisioning for databases (lessons learned at wikipedia)Scaling and hardware provisioning for databases (lessons learned at wikipedia)
Scaling and hardware provisioning for databases (lessons learned at wikipedia)
 
MySQL Schema Design in Practice
MySQL Schema Design in PracticeMySQL Schema Design in Practice
MySQL Schema Design in Practice
 
MySQL at Wikipedia: How we do relational data at the Wikimedia Foundation
MySQL at Wikipedia: How we do relational data at the Wikimedia FoundationMySQL at Wikipedia: How we do relational data at the Wikimedia Foundation
MySQL at Wikipedia: How we do relational data at the Wikimedia Foundation
 
Query optimization: from 0 to 10 (and up to 5.7)
Query optimization: from 0 to 10 (and up to 5.7)Query optimization: from 0 to 10 (and up to 5.7)
Query optimization: from 0 to 10 (and up to 5.7)
 
Why MySQL
Why MySQLWhy MySQL
Why MySQL
 
MySQL GUI Administration
MySQL GUI AdministrationMySQL GUI Administration
MySQL GUI Administration
 
Software libre para gestión de proyectos
Software libre para gestión de proyectosSoftware libre para gestión de proyectos
Software libre para gestión de proyectos
 
warptalk: Experiencia de usuario y usabilidad
warptalk: Experiencia de usuario y usabilidadwarptalk: Experiencia de usuario y usabilidad
warptalk: Experiencia de usuario y usabilidad
 

Recently uploaded

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 

Recently uploaded (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 

Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013

  • 1. Query Optimization with MySQL 5.6: Old and New Tricks Jaime Crespo Senior MySQL Instructor Percona Live London 2013 November 11, 2013
  • 2. About This Presentation •  Objective #1: -  Get faster queries •  Reminder of some well-known query optimization techniques -  Focus on new 5.6 query features •  For other performance tricks with MySQL & InnoDB: -  InnoDB Architecture and Performance Optimization with Peter Zaitsev at 2pm 2 www.percona.com
  • 3. Agenda 1.  Introduction and Environment Setup 2.  Server Update and Configuration 3.  Tools for Query Profiling 4.  Indexing: Old and New Strategies 5.  New Query Planner Features 6.  Results and wrap-up 3 www.percona.com
  • 4. Example Platform •  Sample LAMP Application -  'My Movies' PHP Script -  Example movie database •  Distributed as a VirtualBox VM -  -  -  -  Import percona_live.ova User: percona / pass: percona Make sure httpd and PS are running Optionally: startxfce4 •  Currently no design optimization, no indexes (except PK), no proper query design 4 www.percona.com
  • 5. Query Optimization with MySQL 5.6: Old and New Tricks SERVER UPDATE AND CONFIGURATION 5 www.percona.com 5
  • 6. Basic Configuration Review •  InnoDB as the default engine (default in 5.5) •  InnoDB File Per Table (default in 5.6) •  Larger Buffer Pool and Transaction Log (changed in 5.6) •  Row Based Replication and tx_isolation •  Performance Schema overhead (enabled by default in 5.6) •  InnoDB page checksum (changed in 5.6) •  InnoDB Old Blocks time (changed in 5.6) 6 www.percona.com
  • 7. Query Optimization with MySQL 5.6: Old and New Tricks TOOLS FOR QUERY PROFILING 7 www.percona.com 5
  • 8. EXPLAIN •  EXPLAIN is most important tool for the MySQL DBA and Developer •  It provides: -  -  -  -  8 The query plan strategy The index(es) to be used The order of table access An estimation of the number of rows to be examined! www.percona.com 5
  • 9. EXPLAIN on DML •  EXPLAIN is now available also for INSERT, UPDATE and DELETE mysql> EXPLAIN DELETE FROM title WHERE title = 'Pilot'G! ********************* 1. row *********************! id: 1! select_type: SIMPLE! table: title! type: range! possible_keys: PRIMARY,title! key: title! key_len: 77! ref: NULL! rows: 1380! Extra: Using where! 1 row in set (0.00 sec)! 9 www.percona.com 5
  • 10. Structured EXPLAIN mysql> EXPLAIN FORMAT=JSON SELECT COUNT(*) FROM title 
 WHERE (title = 'Pilot' OR production_year > 2010) 
 AND kind_id < 4G
 *************************** 1. row ***************************! EXPLAIN: {! "query_block": {! "select_id": 1,! "table": {! "table_name": "title",! "access_type": "index_merge",! "possible_keys": [! "title",! "production_year"! ],! "key": "sort_union(title,production_year)",! "key_length": "77,5",! "rows": 4434,! "filtered": 100,! "attached_condition": "(((`imdb`.`title`.`title` = 'Pilot') or (`imdb`.`title`.`production_year` > 2010)) and (`imdb`.`title`.`kind_id` < 4))"! }! }! }! 10 www.percona.com 5
  • 11. Optimizer Trace •  Allows profiling of MySQL query planner •  It shows not only information about the final query plan (EXPLAIN), but also about other discarded strategies, and its “execution cost” •  It can be accessed via the INFORMATION_SCHEMA database •  It is off by default 11 www.percona.com 5
  • 12. Checking the Optimizer Trace •  mysql> SET optimizer_trace="enabled=on"; •  mysql> SELECT COUNT(*) FROM title WHERE (title = 'Pilot' OR production_year > 2010) AND kind_id < 4; +----------+ | COUNT(*) | +----------+ | 3050 | +----------+ 1 row in set (0.01 sec) •  mysql> SELECT trace FROM information_schema.optimizer_trace; 12 www.percona.com 5
  • 13. Checking the Optimizer Trace (cont.) { "range_scan_alternatives": [ { "index": "production_year", "ranges": [ "2010 < production_year" ], "index_dives_for_eq_ranges": true, "rowid_ordered": false, "using_mrr": false, "index_only": true, "rows": 3054, "cost": 615.16, "chosen": true } ], "index_to_merge": "production_year", "cumulated_cost": 905.69 } ], "cost_of_reading_ranges": 905.69, "cost_sort_rowid_and_read_disk": 3672.8, "cost_duplicate_removal": 9467.6, "total_cost": 14046 } ], "chosen_range_access_summary": { "range_access_plan": { "type": "index_merge", "index_merge_of": [ 13 www.percona.com 5
  • 14. Limitations of EXPLAIN •  It does not really execute the query -  Not even subqueries any more •  The row statistics can be sometimes very off •  It does not give us information about temporary tables (on disk or memory?), subtask timing, query cache hit, etc. 14 www.percona.com 5
  • 15. Handler Statistics •  They provide time & memory status-independency: mysql> SHOW SESSION STATUS LIKE 'Hand%';
 +----------------------------+-------+
 | Variable_name | Value |
 +----------------------------+-------+
 | Handler_commit | 1 |
 | Handler_delete | 0 |
 | Handler_discover | 0 |
 | Handler_external_lock | 2 |
 | Handler_mrr_init | 0 |
 | Handler_prepare | 0 |
 | Handler_read_first | 0 |
 | Handler_read_key | 1 |
 | Handler_read_last | 0 |
 | Handler_read_next | 266 |
 | Handler_read_prev | 0 |
 | Handler_read_rnd | 0 |
 | Handler_read_rnd_next | 0 |
 | Handler_rollback | 0 |
 | Handler_savepoint | 0 |
 | Handler_savepoint_rollback | 0 |
 | Handler_update | 0 |
 | Handler_write | 0 |
 +----------------------------+-------+
 18 rows in set (0.00 sec) 15 www.percona.com 5
  • 16. Slow Query Log and pt-querydigest •  Works on all MySQL versions -  Specially useful in combination with the extended log statistics in Percona Server SET GLOBAL log_slow_verbosity = 'full';
 ! # Time: 130601 8:01:06.058915 # User@Host: root[root] @ localhost [] Id: 42 # Schema: imdb Last_errno: 0 Killed: 0 # Query_time: 7.725616 Lock_time: 0.000328 Rows_sent: 4 Rows_examined: 1543720 Rows_affected: 0 # Bytes_sent: 272 Tmp_tables: 0 Tmp_disk_tables: 0 Tmp_table_sizes: 0 # QC_Hit: No Full_scan: Yes Full_join: No Tmp_table: No Tmp_table_on_disk: No # Filesort: No Filesort_on_disk: No Merge_passes: 0 SET timestamp=1370073666; SELECT id,title,production_year FROM title WHERE title = 'Bambi'; ! 16 www.percona.com
  • 17. pt-query-digest slow query log general query log tcpdump pt-query-digest report binary log process list input 17 tool output www.percona.com
  • 18. pt-query-digest Report # Profile! # Rank Query ID Response time Calls R/Call Apdx V/M Item! # ==== ================== =============== ===== ======= ==== ===== =======! # 1 0xA8D2BBDE7EBE7822 4932.2992 28.8% 78 63.2346 0.00 5.22 SELECT person_info! # 2 0xFE25DAF5DBB71F49 4205.2160 24.6% 130 32.3478 0.00 3.47 SELECT title! # 3 0x70DAC639802CA233 1299.6269 7.6% 14 92.8305 0.00 0.17 SELECT cast_info! # 4 0xE336B880F4FEC4B8 1184.5101 6.9% 294 4.0289 0.36 2.29 SELECT cast_info! # 5 0x60550B93960F1837 905.1648 5.3% 60 15.0861 0.05 1.33 SELECT name! # 6 0xF46D5C09B4E0CA2F 777.2446 4.5% 16340 0.0476 1.00 0.17 SELECT char_name! # 7 0x09FCFFF0E5BC929F 747.4346 4.4% 130 5.7495 0.53 7.69 SELECT name! # 8 0x9433950BE12B9470 744.1755 4.4% 14368 0.0518 1.00 0.18 SELECT name! # 9 0x4DC0E044996DA715 448.5637 2.6% 130 3.4505 0.65 8.31 SELECT title! # 10 0x09FB72D72ED18E93 361.1904 2.1% 78 4.6306 0.28 1.89 SELECT cast_info title
 #! # Query 1: 0.02 QPS, 1.40x concurrency, ID 0xA8D2BBDE7EBE7822 at byte 25744265! # Attribute pct total min max avg 95% stddev median! # ============ === ======= ======= ======= ======= ======= ======= =======! # Count 0 78! # Exec time 28 4932s 11s 109s 63s 88s 18s 63s! # Lock time 0 724ms 36us 642ms 9ms 11ms 72ms 93us! # Rows sent 7 12.95k 86 334 170.05 329.68 89.24 136.99! # Rows examine 14 168.99M 2.17M 2.17M 2.17M 2.17M 0 2.17M! # Rows affecte 0 0 0 0 0 0 0 0! # Rows read 7 12.95k 86 334 170.05 329.68 89.24 136.99! # Bytes sent 5 2.39M 16.18k 54.14k 31.43k 54.03k 13.47k 25.99k! 18 www.percona.com
  • 19. performance_schema •  Integrated profiling tool •  Not new, but improved significately for 5.6 •  Substitutes the old PROFILING interface •  Can have a 5-10% overhead by default •  Can be used to monitor several runtime statistics, among them, query execution statistics of the latest queries: SELECT * FROM performance_schema.events_statements_history_long ! 19 www.percona.com
  • 20. Query Optimization with MySQL 5.6: Old and New Tricks INDEXING: OLD AND NEW STRATEGIES 20 www.percona.com 5
  • 21. Advantages of Indexing •  •  •  •  21 Faster Filtering Faster Column Return (Covering Index) Faster Ordering Certain Faster Functions (max, min) www.percona.com
  • 22. Faster Filtering •  Indexes allow to return results by reading a fewer number of records -  We should minimize the rows value in EXPLAIN/Handlers -  const > ref > range > index > ALL! •  Usually, only a single index can be used per table access -  Compound indexes may provide higher filtering rate 22 www.percona.com
  • 23. Compound Indexes •  Column Order in Index Does Matter: -  Order by decreasing selectivity -  Only one range access per index* 23 www.percona.com
  • 24. Covering Index Technique •  Additional columns can be indexed in order to speed-up column retrieval •  Specially interesting in n-to-n relationship tables •  Not possible if the number or size of columns is too large (e.g. SELECT *, BLOBs) 24 www.percona.com
  • 25. Index Condition Pushdown •  Let’s prepare a use case: UPDATE cast_info SET note = left(note, 250);
 ALTER TABLE cast_info MODIFY note varchar(250), ADD INDEX role_id_note (role_id, note); ! •  We want to execute: SELECT * FROM cast_info 
 WHERE role_id = 1 
 and note like '%Jaime%‘;! 25 www.percona.com 5
  • 26. Without ICP (5.5) mysql> EXPLAIN SELECT * 
 FROM cast_info 
 WHERE role_id = 1 
 and note like '%Jaime%'G
 ********** 1. row **********
 id: 1
 select_type: SIMPLE
 table: cast_info
 type: ref
 possible_keys: role_id_note
 key: role_id_note
 key_len: 4
 ref: const
 rows: 11553718
 Extra: Using where
 1 row in set (0.01 sec)! 26 mysql> SHOW STATUS like 'Hand %';! +----------------------------+---------+! | Variable_name | Value |! +----------------------------+---------+! | Handler_commit | 1 |! | Handler_delete | 0 |! | Handler_discover | 0 |! | Handler_prepare | 0 |! | Handler_read_first | 0 |! | Handler_read_key | 1 |! | Handler_read_last | 0 |! | Handler_read_next | 8346769 |! | Handler_read_prev | 0 |! | Handler_read_rnd | 0 |! | Handler_read_rnd_next | 0 |! | Handler_rollback | 0 |! | Handler_savepoint | 0 |! | Handler_savepoint_rollback | 0 |! | Handler_update | 0 |! | Handler_write | 0 |! +----------------------------+---------+! 16 rows in set (0.00 sec)! www.percona.com 5
  • 27. With ICP (5.6) mysql> EXPLAIN SELECT * 
 FROM cast_info 
 WHERE role_id = 1 and note like '%Jaime%'G
 ************ 1. row ************
 id: 1
 select_type: SIMPLE
 table: cast_info
 type: ref
 possible_keys: role_id_note
 key: role_id_note
 key_len: 4
 ref: const
 rows: 10259274
 Extra: Using index condition
 1 row in set (0.00 sec)! 27 mysql> SHOW STATUS like 'Hand%';! +----------------------------+-------+! | Variable_name | Value |! +----------------------------+-------+! | Handler_commit | 1 |! | Handler_delete | 0 |! | Handler_discover | 0 |! | Handler_external_lock | 2 |! | Handler_mrr_init | 0 |! | Handler_prepare | 0 |! | Handler_read_first | 0 |! | Handler_read_key | 1 |! | Handler_read_last | 0 |! | Handler_read_next | 266 |! | Handler_read_prev | 0 |! | Handler_read_rnd | 0 |! | Handler_read_rnd_next | 0 |! | Handler_rollback | 0 |! | Handler_savepoint | 0 |! | Handler_savepoint_rollback | 0 |! | Handler_update | 0 |! | Handler_write | 0 |! +----------------------------+-------+! 18 rows in set (0.00 sec) www.percona.com 5
  • 28. Comparison of ICP Execution •  Execution time for this example: •  MySQL 5.5: 5.76 sec •  MySQL 5.6: 1.09 sec •  Over 5x faster •  In this example, it would not work with a prefix index (e.g. TEXT/BLOB), as it must search the whole field •  Fun fact: if covering technique is tried, it actually runs slower than with ICP for this case/hardware (#68554) 28 www.percona.com 5
  • 29. ICP and Indexing •  ICP will change the way we index our tables SELECT * FROM cast_info 
 FORCE INDEX(person_role_id_role_id)
 WHERE person_role_id > 0 
 and person_role_id < 150000 
 and role_id > 1 and role_id < 7;! •  For example, a multiple column index can be, in some cases, efficiently used for a range condition on several columns: •  29 Effectiveness is highly dependent on how selective is the second part of the index (an “index scan” is still done at engine level) www.percona.com
  • 30. Index for Sorts •  Additional columns can be indexed in order to speed up ordering of rows •  Only effective in simple patterns •  GROUP BY operations are essentially sorts •  They can also be greatly improved with the right indexes in some cases 30 www.percona.com
  • 31. Multi-Range Read (MRR) •  Reorders access to table data when using a secondary index on disk for sequential I/O -  Like ICP, its efficiency is highly dependent on data distribution and memory contents •  It also depends on hardware sequential access (e.g., InnoDB’s read ahead) -  May not be useful on SSDs •  Not compatible with Using index •  Only for range access and equi-joins 31 www.percona.com 5
  • 32. MRR Example •  We want to execute: SELECT * FROM cast_info 
 WHERE person_role_id > 0 
 and person_role_id < 150000;
 SELECT * FROM cast_info 
 WHERE role_id = 3 
 and person_role_id > 0 
 and person_role_id < 500000;! •  We will use these indexes, respectively: ALTER TABLE cast_info 
 ADD INDEX person_role_id (person_role_id);
 ALTER TABLE cast_info 
 ADD INDEX person_role_id_role_id (person_role_id, role_id);! 32 www.percona.com 5
  • 33. Without MRR (5.5) mysql> EXPLAIN SELECT * FROM cast_info FORCE INDEX(person_role_id) WHERE person_role_id > 0 and person_role_id < 150000G
 *********** 1. row ***********
 id: 1
 select_type: SIMPLE
 table: cast_info
 type: range
 possible_keys: person_role_id
 key: person_role_id
 key_len: 5
 ref: NULL
 rows: 8966490
 Extra: Using where
 1 row in set (0.00 sec)! 33 mysql> SHOW STATUS like 'Hand%';! +----------------------------+---------+! | Variable_name | Value |! +----------------------------+---------+! | Handler_commit | 1 |! | Handler_delete | 0 |! | Handler_discover | 0 |! | Handler_prepare | 0 |! | Handler_read_first | 0 |! | Handler_read_key | 1 |! | Handler_read_last | 0 |! | Handler_read_next | 4654312 |! | Handler_read_prev | 0 |! | Handler_read_rnd | 0 |! | Handler_read_rnd_next | 0 |! | Handler_rollback | 0 |! | Handler_savepoint | 0 |! | Handler_savepoint_rollback | 0 |! | Handler_update | 0 |! | Handler_write | 0 |! +----------------------------+---------+! 16 rows in set (0.00 sec) www.percona.com 5
  • 34. With MRR (5.6) mysql> EXPLAIN SELECT * FROM cast_info FORCE INDEX(person_role_id) WHERE person_role_id > 0 and person_role_id < 150000G
 *********** 1. row ***********
 id: 1
 select_type: SIMPLE
 table: cast_info
 type: range
 possible_keys: person_role_id
 key: person_role_id
 key_len: 5
 ref: NULL
 rows: 8966490
 Extra: Using index
 condition; Using MRR
 1 row in set (0.00 sec)! 34 mysql> SHOW STATUS like 'Hand%';! +----------------------------+---------+! | Variable_name | Value |! +----------------------------+---------+! | Handler_commit | 1 |! | Handler_delete | 0 |! | Handler_discover | 0 |! | Handler_external_lock | 4 |! | Handler_mrr_init | 0 |! | Handler_prepare | 0 |! | Handler_read_first | 0 |! | Handler_read_key | 4654313 |! | Handler_read_last | 0 |! | Handler_read_next | 4654312 |! | Handler_read_prev | 0 |! | Handler_read_rnd | 4654312 |! | Handler_read_rnd_next | 0 |! | Handler_rollback | 0 |! | Handler_savepoint | 0 |! | Handler_savepoint_rollback | 0 |! | Handler_update | 0 |! | Handler_write | 0 |! +----------------------------+---------+! 18 rows in set (0.00 sec) www.percona.com 5
  • 35. Comparison of MRR Execution •  Execution time for this example: •  •  MySQL 5.5: 4654312 rows in set (1 min 4.79 sec) MySQL 5.6 (w/MRR, wo/ICP): 4654312 rows in set (48.64 sec) •  Consistent 33% execution improvement for this test case •  Difficult to see for smaller resultsets •  What has changed? •  •  35 15% more “read_ahead”s, resulting in a 40% less data reads Reordering has an overhead, can be tuned with read_rnd_buffer_size www.percona.com 5
  • 36. What’s the Point for the ‘FORCE INDEX’? •  For demonstration purposes (full table scan is better here) •  Some features are not properly detected yet: SELECT * 
 FROM cast_info FORCE INDEX(person_role_id_role_id) 
 WHERE role_id = 3 
 and person_role_id > 0 
 and person_role_id < 500000; •  Full table scan (any version): Empty set (8.08 sec) •  MySQL 5.5: Empty set (1 min 16.88 sec) •  MySQL 5.6 ICP+MRR*: Empty set (0.46 sec) *ICP is responsible for the dramatic change in execution time, not MRR 36 www.percona.com
  • 37. Batched Key Access (BKA) •  It retrieves keys in batches and allows MRR usage for JOINs, as an alternative to standard Nested Loop Join execution •  Not enabled by default SET optimizer_switch=
 'mrr=on,mrr_cost_based=off,batched_key_access=on'; ! 37 www.percona.com
  • 38. Without BKA (5.5) - EXPLAIN mysql> EXPLAIN 
 SELECT cast_info.note, char_name.name 
 FROM cast_info FORCE index(person_role_id) 
 JOIN char_name 
 ON cast_info.person_role_id = char_name.id 
 WHERE cast_info.person_role_id > 0 and cast_info.person_role_id < 150000
 ************ 1. row ************! id: 1! select_type: SIMPLE! table: char_name! type: range! possible_keys: PRIMARY! key: PRIMARY! key_len: 4! ref: NULL! rows: 313782! Extra: Using where! ...! 38 ...! ************* 2. row ************* id: 1! select_type: SIMPLE! table: cast_info! type: ref! possible_keys: person_role_id! key: person_role_id! key_len: 5! ref: imdb.char_name.id! rows: 4! Extra: NULL! 2 rows in set (0.00 sec)! www.percona.com
  • 39. Without BKA (5.5) - Handlers mysql> SHOW STATUS like ‘Hand%’;! +----------------------------+---------+
 | Variable_name | Value |
 +----------------------------+---------+
 | Handler_commit | 1 |
 | Handler_delete | 0 |
 | Handler_discover | 0 |
 | Handler_prepare | 0 |
 | Handler_read_first | 0 |
 | Handler_read_key | 150000 |
 | Handler_read_last | 0 |
 | Handler_read_next | 4804311 |
 | Handler_read_prev | 0 |
 | Handler_read_rnd | 0 |
 | Handler_read_rnd_next | 0 |
 | Handler_rollback | 0 |
 | Handler_savepoint | 0 |
 | Handler_savepoint_rollback | 0 |
 | Handler_update | 0 |
 | Handler_write | 0 |
 +----------------------------+---------+
 18 rows in set (0.00 sec)! 39 www.percona.com
  • 40. With BKA (5.6) - EXPLAIN mysql> EXPLAIN 
 SELECT cast_info.note, char_name.name 
 FROM cast_info FORCE index(person_role_id) JOIN char_name ON cast_info.person_role_id = char_name.id 
 WHERE cast_info.person_role_id > 0 and cast_info.person_role_id < 150000G! ************ 1. row ************! id: 1! select_type: SIMPLE! table: char_name! type: range! possible_keys: PRIMARY! key: PRIMARY! key_len: 4! ref: NULL! rows: 313782! Extra: Using where
 ...! 40 ...! ************ 2. row ************! id: 1! select_type: SIMPLE! table: cast_info! type: ref! possible_keys: person_role_id! key: person_role_id! key_len: 5! ref: imdb.char_name.id! rows: 4! Extra: Using join buffer (Batched Key Access)! 2 rows in set (0.00 sec)! www.percona.com
  • 41. With BKA (5.6) - Handlers mysql> SHOW STATUS like ‘Hand%’;
 +----------------------------+---------+
 | Variable_name | Value |
 +----------------------------+---------+
 | Handler_commit | 1 |
 | Handler_delete | 0 |
 | Handler_discover | 0 |
 | Handler_external_lock | 6 |
 | Handler_mrr_init | 1 |
 | Handler_prepare | 0 |
 | Handler_read_first | 0 |
 | Handler_read_key | 4804312 |
 | Handler_read_last | 0 |
 | Handler_read_next | 4804311 |
 | Handler_read_prev | 0 |
 | Handler_read_rnd | 4654312 |
 | Handler_read_rnd_next | 0 |
 | Handler_rollback | 0 |
 | Handler_savepoint | 0 |
 | Handler_savepoint_rollback | 0 |
 | Handler_update | 0 |
 | Handler_write | 0 |
 +----------------------------+---------+
 18 rows in set (0.00 sec)! 41 www.percona.com
  • 42. Comparison of BKA Execution •  Execution time for this example: •  •  MySQL 5.5: 4654312 rows in set (1 min 6.78 sec) MySQL 5.6 (w/MRR, w/BKA): 4654312 rows in set (1 min 0.47 sec) •  The results are consistent between executions, but the gain is not too big. But if we change the join_buffer_size… •  MySQL 5.6 (w/MRR, w/BKA, join_buffer_size = 50M): 4654312 rows in set (19.54 sec) (join_buffer_size does not affect execution time in the 5.5 version) 42 www.percona.com 5
  • 43. Better Index Merge •  In this example, it avoids a full table scan: mysql> EXPLAIN SELECT COUNT(*) FROM title WHERE (title = 'Pilot' OR production_year > 2010) AND kind_id < 4G ************************ 1. row ************************ id: 1 select_type: SIMPLE table: title type: index_merge possible_keys: title,production_year key: title,production_year key_len: 77,5 ref: NULL rows: 4434 Extra: Using sort_union(title,production_year); Using where 1 row in set (0.00 sec) (almost all titles have kind_id < 4) •  43 MySQL 5.5: 0.79s – MySQL 5.6: 0.01s www.percona.com 5
  • 44. Extended Secondary Keys •  Implicit primary keys inside secondary keys can be used for filtering (ref, range, etc), not only for covering index or sorting. •  Requires use_index_extensions=on (default) ALTER TABLE title add index (title(25)); SELECT COUNT(*) FROM title WHERE title = 'Pilot' AND id BETWEEN 1000 AND 1000000; 44 www.percona.com 5
  • 45. Extended Secondary Keys mysql> EXPLAIN SELECT COUNT(*) FROM title WHERE title = 'Pilot' AND id BETWEEN 1000 AND 1000000G ****************** 1. row ******************* id: 1 select_type: SIMPLE table: title type: range possible_keys: PRIMARY,title key: title key_len: 81 ref: NULL rows: 531 Extra: Using index condition; Using where 1 row in set (0.00 sec) 45 www.percona.com 5
  • 46. Duplicate Key Check mysql> alter table title add index (production_year);! Query OK, 0 rows affected (10.08 sec)! Records: 0 Duplicates: 0 Warnings: 0! mysql> alter table title add index (production_year); Query OK, 0 rows affected, 1 warning (5.11 sec) Records: 0 Duplicates: 0 Warnings: 1! ! mysql> show warningsG! ************************* 1. row *************************! Level: Note! Code: 1831! Message: Duplicate index 'production_year_2' defined on the table 'imdb.title'. This is deprecated and will be disallowed in a future release.! 1 row in set (0.00 sec)! 46 www.percona.com 5
  • 47. pt-duplicate-key-checker •  It detects duplicate and redundant indexes -  Including primary keys at the end of secondary keys on InnoDB tables 47 www.percona.com
  • 48. pt-index-usage & user_stats •  Can detect unused indexes -  That slow down writes -  Can also affect reads! -  Wasted memory -  Query optimizer woes 48 www.percona.com
  • 49. Query Optimization with MySQL 5.6: Old and New Tricks NEW QUERY PLANNER FEATURES 49 www.percona.com 5
  • 50. Filesort with Short LIMIT •  Queries that require a filesort but only the first records are selected can benefit from this optimization: mysql> EXPLAIN select * from movie_info 
 ORDER BY info LIMIT 100G
 ********************* 1. row *********************
 id: 1
 select_type: SIMPLE
 table: movie_info
 type: ALL
 possible_keys: NULL
 key: NULL
 key_len: NULL
 ref: NULL
 rows: 6927988
 Extra: Using filesort
 1 row in set (0.00 sec)
 ! Note: Both EXPLAIN and the STATUS Handlers show the same outcome 50 www.percona.com 5
  • 51. Filesort with Short LIMIT (cont.) •  SELECT * FROM movie_info ORDER BY info LIMIT 100;! -  MySQL 5.5: 20.06 sec -  MySQL 5.6 (P_S on): 9.14 sec -  MySQL 5.6 (P_S off): 8.51 sec •  Over 2x faster. •  Exact speed-up may depend on the original sort buffer size 51 www.percona.com 5
  • 52. Subquery Optimization •  Late Subquery Materialization -  Useful for FROM subqueries if further filtering is done -  MySQL may also decide to create its own index on a temporary table on memory  SELECT * FROM title JOIN (SELECT * FROM cast_info) AS subselect ON subselect.movie_id = title.id WHERE subselect.id = 1;
 EXPLAIN: 0.00 sec (vs >2 min in 5.5) EXECUTION: 1m 22 sec (vs > 4 min in 5.5) 52 www.percona.com 5
  • 53. JOINs and Subqueries •  Better detection of IN + non-dependent subqueries -  Many queries do not need to be converted to a JOIN anymore for a proper execution plan -  MySQL decides what is the best strategy: Materialization, Semi-Join, etc. 53 www.percona.com 5
  • 54. That Subquery in 5.5 mysql> EXPLAIN SELECT * FROM title WHERE kind_id IN 
 (SELECT id FROM kind_type WHERE kind='video game')G
 *************************** 1. row ***************************
 id: 1
 select_type: PRIMARY
 table: title
 type: ALL
 An index on possible_keys: NULL
 won't fix it! key: NULL
 key_len: NULL
 ref: NULL
 rows: 1567676
 Extra: Using where
 *************************** 2. row ***************************
 id: 2
 select_type: DEPENDENT SUBQUERY
 table: kind_type
 type: const
 possible_keys: PRIMARY,kind_id
 key: kind_id
 key_len: 47
 ref: const
 rows: 1
 Extra: Using index
 2 rows in set (0.00 sec)! 54 title.kind_id www.percona.com 5
  • 55. That Subquery in 5.6 mysql> EXPLAIN SELECT * FROM title WHERE kind_id IN (SELECT id FROM kind_type WHERE kind='video game')G
 *************************** 1. row ***************************
 id: 1
 select_type: SIMPLE
 table: kind_type
 type: const
 possible_keys: PRIMARY,kind
 key: kind
 key_len: 47
 ref: const
 rows: 1
 Extra: Using index
 *************************** 2. row ***************************
 id: 1
 select_type: SIMPLE
 table: title
 An index on title.kind_id type: ALL
 can and will be very useful! possible_keys: NULL
 key: NULL
 key_len: NULL
 ref: NULL
 rows: 1477989
 Extra: Using where
 2 rows in set (0.00 sec)! 55 www.percona.com 5
  • 56. Join Order •  Table order algorithm has been optimized, which leads to better query plans when joining many tables 56 www.percona.com 5
  • 57. Persistent Optimizer Statistics •  InnoDB index statistics are no longer discarded on server shutdown and recomputed the next time a table is accessed •  Controlled by variable: innodb_stats_persistent = ON (default) •  Remember that SHOW commands/accessing to I_S do not automatically regenerate statistics by default 57 www.percona.com 5
  • 58. READ ONLY Transactions •  START TRANSACTION READ ONLY; -  It avoids some overhead of R/W transactions (MVCC) -  Automatically used for SELECT in auto_commit -  Once started, a new transaction has to be started to perform a write 58 www.percona.com 5
  • 59. Explicit Partition Selection •  Usually unnecessary, as MySQL does automatically partition pruning •  Can be useful combined with some nondeterministic functions or in some special cases: SELECT count(*) FROM title PARTITION(p01, p02); 59 www.percona.com 5
  • 60. Query Optimization with MySQL 5.6: Old and New Tricks RESULTS AND WRAP-UP 60 www.percona.com 5
  • 61. 5.6 is a Great Release •  Many optimizer changes that can potentially improve query execution time •  Some of them are transparent, some others require tuning and understanding -  Some old tricks and indexing strategies become obsolete in 5.6 •  pt-upgrade, from Percona Toolkit, and Percona Playback can be great tools to analyze improvements and regressions 61 www.percona.com
  • 62. Where to Learn More ? •  More query and server optimization techniques in our training courses ( http://www.percona.com/training): -  Scaling and Optimization for MySQL -  Operations and Troubleshooting with MySQL -  Moving to MySQL 5.6 -  Analysing Queries with Percona Toolkit -  Percona Server and Tools (NEW!) •  Ask me for a discount (only to PLUK attendants)! 62 www.percona.com
  • 63. Where to Learn More ? (cont.) •  Books: •  High Performance MySQL, 3rd Edition •  SQL Antipatterns •  Free MySQL Webinars: •  http://www.percona.com/resources/ mysql-webinars 63 www.percona.com
  • 64. Percona Live MySQL Conference and Expo 2014 •  For more information and to register: Visit: http://www.percona.com/live/mysql-conference-2014/