SlideShare a Scribd company logo
1 of 68
Download to read offline
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
How to Analyze and Tune MySQL Queries for
Better Performance
Øystein Grøvlen
Senior Principal Software Engineer
MySQL Optimizer Team, Oracle
March/April, 2016
Please Stand By. This session will begin promptly at the time
indicated on the agenda. Thank You.
Copyright © 2016 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 © 2016, Oracle and/or its affiliates. All rights reserved. | 3
Program Agenda
Cost-based query optimization in MySQL
Tools for monitoring, analyzing, and tuning queries
Data access and index selection
Join optimizer
Sorting
Influencing the optimizer
1
2
3
4
5
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 4
Program Agenda
Cost-based query optimization in MySQL
Tools for monitoring, analyzing, and tuning queries
Data access and index selection
Join optimizer
Sorting
Influencing the optimizer
1
2
3
4
5
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 5
MySQL Optimizer
SELECT a, b
FROM t1, t2, t3
WHERE t1.a = t2.b
AND t2.b = t3.c
AND t2.d > 20
AND t2.d < 30;
MySQL Server
Cost based
optimizations
Heuristics
Cost Model
Optimizer
Table/index info
(data dictionary)
Statistics
(storage engines)
t2 t3
t1
Table
scan
Range
scan
Ref
access
JOIN
JOIN
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 6
Cost-based Query Optimization
• Assign cost to operations
• Assign cost to partial or alternative plans
• Search for plan with lowest cost
• Cost-based optimizations:
General idea
Access method Subquery strategyJoin order
t2 t3
t1
Table
scan
Range
scan
Ref
access
JOIN
JOIN
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 7
Optimizer Cost Model
t1 Cost estimate
Row estimate
Cost Model
Cost formulas
Access
methods
Join Subquery
Cost constants
CPU IO
Metadata:
- Row and index size
- Index information
- Uniqueness
Statistics:
- Table size
- Cardinality
- Range estimates
Cost model
configuration
Range
scan
JOIN
New in
MySQL 5.7
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 8
Cost Model Example
Table scan:
• IO-cost: #pages in table * IO_BLOCK_READ_COST
• CPU cost: #rows * ROW_EVALUATE_COST
Range scan (on secondary index):
• IO-cost: #rows_in_range * IO_BLOCK_READ_COST
• CPU cost: #rows_in_range * ROW_EVALUATE_COST
SELECT SUM(o_totalprice) FROM orders
WHERE o_orderdate BETWEEN '1994-01-01' AND '1994-12-31';
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 9
Cost Model Example
EXPLAIN SELECT SUM(o_totalprice) FROM orders
WHERE o_orderdate BETWEEN '1994-01-01' AND '1994-12-31';
EXPLAIN SELECT SUM(o_totalprice) FROM orders
WHERE o_orderdate BETWEEN '1994-01-01' AND '1994-06-30';
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE orders ALL i_o_orderdate NULL NULL NULL 15000000 Using where
Id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE orders range i_o_orderdate i_o_orderdate 4 NULL 2235118
Using index
condition
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 10
Cost Model Example: Optimizer Trace
join_optimization / row_estimation / table : orders / range_analysis
"table_scan": {
"rows": 15000000,
"cost": 3.12e6
} /* table_scan */,
"potential_range_indices": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "i_o_orderdate",
"usable": true,
"key_parts": [ "o_orderDATE", "o_orderkey" ]
}
] /* potential_range_indices */,
…
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "i_o_orderdate",
"ranges": [ "1994-01-01 <= o_orderDATE <= 1994-12-31"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": false,
"rows": 4489990,
"cost": 5.39e6,
"chosen": false,
"cause": "cost"
}
] /* range_scan_alternatives */,
…
} /* analyzing_range_alternatives */
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 11
Cost Model vs Real World
Data in Memory Data on Disk Data on SSD
Table scan 6.8 seconds 36 seconds 15 seconds
Index scan 5.2 seconds 2.5 hours 30 minutes
Measured Execution Times
Force Index Scan:
SELECT SUM(o_totalprice)
FROM orders FORCE INDEX (i_o_orderdate)
WHERE o_orderdate BETWEEN '1994-01-01' AND '1994-12-31';
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 12
Performance Schema
SELECT event_name, count_read, avg_timer_read/1000000000.0 "Avg Read Time (ms)",
sum_number_of_bytes_read "Bytes Read"
FROM performance_schema.file_summary_by_event_name
WHERE event_name='wait/io/file/innodb/innodb_data_file';
Disk I/O
event_name count_read Avg Read Time (ms) Bytes Read
wait/io/file/innodb/innodb_data_file 2188853 4.2094 35862167552
event_name count_read Avg Read Time (ms) Bytes Read
wait/io/file/innodb/innodb_data_file 115769 0.0342 1896759296
Index Scan
Table Scan
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 13
Program Agenda
Cost-based query optimization in MySQL
Tools for monitoring, analyzing, and tuning queries
Data access and index selection
Join optimizer
Sorting
Influencing the optimizer
1
2
3
4
5
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 14
Useful tools
• MySQL Enterprise Monitor (MEM), Query Analyzer
– Commercial product
• Performance schema, MySQL sys schema
• EXPLAIN
– Tabular EXPLAIN
– Structured EXPLAIN (FORMAT=JSON)
– Visual EXPLAIN (MySQL Workbench)
• Optimizer trace
• Slow log
• Status variables (SHOW STATUS LIKE 'Sort%')
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 15
MySQL Enterprise Monitor, Query Analyzer
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 16
Query Analyzer Query Details
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 17
Performance Schema
• events_statements_history
events_statements_history_long
– Most recent statements executed
• events_statements_summary_by_digest
– Summary for similar statements (same statement digest)
• file_summary_by_event_name
– Interesting event: wait/io/file/innodb/innodb_data_file
• table_io_waits_summary_by_table
table_io_waits_summary_by_index_usage
– Statistics on storage engine access per table and index
Some useful tables
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 18
Performance Schema
• Tables:
events_statements_current (Current statement for each thread)
events_statements_history (10 most recent statements per thread)
events_statements_history_long (10000 most recent statements)
• Columns:
THREAD_ID, EVENT_ID, END_EVENT_ID, EVENT_NAME, SOURCE, TIMER_START, TIMER_END, TIMER_WAIT,
LOCK_TIME, SQL_TEXT, DIGEST, DIGEST_TEXT, CURRENT_SCHEMA, OBJECT_TYPE, OBJECT_SCHEMA,
OBJECT_NAME, OBJECT_INSTANCE_BEGIN, MYSQL_ERRNO, RETURNED_SQLSTATE, MESSAGE_TEXT, ERRORS,
WARNINGS, ROWS_AFFECTED, ROWS_SENT, ROWS_EXAMINED, CREATED_TMP_DISK_TABLES,
CREATED_TMP_TABLES, SELECT_FULL_JOIN, SELECT_FULL_RANGE_JOIN, SELECT_RANGE, SELECT_RANGE_CHECK,
SELECT_SCAN, SORT_MERGE_PASSES, SORT_RANGE, SORT_ROWS, SORT_SCAN, NO_INDEX_USED,
NO_GOOD_INDEX_USED, NESTING_EVENT_ID, NESTING_EVENT_TYPE
Statement events
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 19
Performance Schema
• Normalization of queries to group statements that are similar to be
grouped and summarized:
SELECT * FROM orders WHERE o_custkey=10 AND o_totalprice>20
SELECT * FROM orders WHERE o_custkey = 20 AND o_totalprice > 100
SELECT * FROM orders WHERE o_custkey = ? AND o_totalprice > ?
• events_statements_summary_by_digest
DIGEST, DIGEST_TEXT, COUNT_STAR, SUM_TIMER_WAIT, MIN_TIMER_WAIT, AVG_TIMER_WAIT,
MAX_TIMER_WAIT, SUM_LOCK_TIME, SUM_ERRORS, SUM_WARNINGS, SUM_ROWS_AFFECTED,
SUM_ROWS_SENT, SUM_ROWS_EXAMINED, SUM_CREATED_TMP_DISK_TABLES, SUM_CREATED_TMP_TABLES,
SUM_SELECT_FULL_JOIN, SUM_SELECT_FULL_RANGE_JOIN, SUM_SELECT_RANGE, SUM_SELECT_RANGE_CHECK,
SUM_SELECT_SCAN, SUM_SORT_MERGE_PASSES, SUM_SORT_RANGE, SUM_SORT_ROWS, SUM_SORT_SCAN,
SUM_NO_INDEX_USED, SUM_NO_GOOD_INDEX_USED, FIRST_SEEN, LAST_SEEN
Statement digest
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 20
MySQL sys Schema
• A collection of views, procedures and functions, designed to make reading
raw Performance Schema data easier
• Implements many common DBA and Developer use cases
– File IO usage per user
– Which indexes is never used?
– Which queries use full table scans?
• Examples of very useful functions:
– format_time() , format_bytes(), format_statement()
• Included with MySQL 5.7
• Bundled with MySQL Workbench
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 21
MySQL sys Schema
statement_analysis: Lists a normalized statement view with aggregated
statistics, ordered by the total execution time per normalized statement
mysql> SELECT * FROM sys.statement_analysis LIMIT 1G
*************************** 1. row ***************************
query: INSERT INTO `mem__quan` . `nor ... nDuration` = IF ( VALUES ( ...
db: mem
full_scan: 0
exec_count: 1110067
err_count: 0
warn_count: 0
total_latency: 1.93h
max_latency: 5.03 s
avg_latency: 6.27 ms
lock_latency: 00:18:29.18
Example
rows_sent: 0
rows_sent_avg: 0
rows_examined: 0
rows_examined_avg: 0
tmp_tables: 0
tmp_disk_tables: 0
rows_sorted: 0
sort_merge_passes: 0
digest: d48316a218e95b1b8b72db5e6b177788!
first_seen: 2014-05-20 10:42:17
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 22
EXPLAIN
• Use EXPLAIN to print the final query plan:
• Explain for a running query (New in MySQL 5.7):
EXPLAIN FOR CONNECTION connection_id;
Understand the query plan
EXPLAIN SELECT * FROM t1 JOIN t2 ON t1.a = t2.a WHERE b > 10 AND c > 10;
+----+--------+-------+------------+------+---------------+-----+---------+-----+------+----------+------------+
| id | select…| table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------+-------+------------+------+---------------+-----+---------+-----+------+----------+------------+
| 1 | SIMPLE | t1 | NULL | range| idx1 | idx1| 4 | NULL| 12 | 33.33 | Using where|
| 1 | SIMPLE | t2 | NULL | ref | idx2 | idx2| 4 | t1.a| 1 | 100.00 | NULL |
+----+--------+-------+------------+------+---------------+-----+---------+-----+------+----------+------------+
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 23
Added in
MySQL 5.7
Structured EXPLAIN
• JSON format:
• Contains more information:
– Used index parts
– Pushed index conditions
– Cost estimates
– Data estimates
EXPLAIN FORMAT=JSON
SELECT * FROM t1 WHERE b > 10 AND c > 10;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "17.81"
},
"table": {
"table_name": "t1",
"access_type": "range",
"possible_keys": [
"idx1"
],
"key": "idx1",
"used_key_parts": [
"b"
],
"key_length": "4",
"rows_examined_per_scan": 12,
"rows_produced_per_join": 3,
"filtered": "33.33",
"index_condition": "(`test`.`t1`.`b` > 10)",
"cost_info": {
"read_cost": "17.01",
"eval_cost": "0.80",
"prefix_cost": "17.81",
"data_read_per_join": "63"
},
………
"attached_condition": "(`test`.`t1`.`c` > 10)"
}
}
}
EXPLAIN FORMAT=JSON SELECT …
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 24
EXPLAIN
{
"query_block": {
"select_id": 1,
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 10,
"filtered": 100,
"attached_condition": "(t1.a = 9)"
} /* table */
},
{
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 10,
"filtered": 100,
"using_join_buffer": "Block Nested Loop",
"attached_condition": "((t2.a = 9) and ((t2.b <= 3) or ((t2.b =
5) and (t1.b = 12))))"
} /* table */
}
] /* nested_loop */
} /* query_block */
}
Structured EXPLAIN
Assigning Conditions to Tables
EXPLAIN FORMAT=JSON SELECT * FROM t1, t2
WHERE t1.a=t2.a AND t2.a=9 AND (NOT (t1.a > 10 OR t2.b >3) OR (t1.b=t2.b+7 AND t2.b = 5));
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 25
Optimizer Trace: Query Plan Debugging
• EXPLAIN shows the selected plan
• Optimizer trace shows WHY the plan was selected
SET optimizer_trace= "enabled=on";
SELECT * FROM t1,t2 WHERE f1=1 AND f1=f2 AND f2>0;
SELECT trace FROM information_schema.optimizer_trace
INTO OUTFILE <filename> LINES TERMINATED BY '';
SET optimizer_trace="enabled=off";
QUERY SELECT * FROM t1,t2 WHERE f1=1 AND f1=f2 AND f2>0;
TRACE "steps": [ { "join_preparation": { "select#": 1,… } … } …]
MISSING_BYTES_BEYOND_MAX_MEM_SIZE 0
INSUFFICIENT_PRIVILEGES 0
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 26
Program Agenda
Cost-based query optimization in MySQL
Tools for monitoring, analyzing, and tuning queries
Data access and index selection
Join optimizer
Sorting
Influencing the optimizer
1
2
3
4
5
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 27
Selecting Access Method
• For each table, find the best access method:
– Check if the access method is useful
– Estimate cost of using access method
– Select the cheapest to be used
• Choice of access method is cost based
Finding the optimal method to read data from storage engine
Main access methods:
 Table scan
 Index scan
 Index look-up
(ref access)
 Range scan
 Index merge
 Loose index scan
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 28
Ref Access
EXPLAIN SELECT * FROM customer WHERE c_custkey = 570887;
Single Table Queries
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE customer const PRIMARY PRIMARY 4 const 1 NULL
EXPLAIN SELECT * FROM orders WHERE o_orderdate = '1992-09-12';
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE orders ref i_o_orderdate i_o_orderdate 4 const 6271 NULL
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 29
Ref Access
Join Queries
EXPLAIN SELECT *
FROM orders JOIN customer ON c_custkey = o_custkey
WHERE o_orderdate = '1992-09-12';
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE orders ref
i_o_orderdate,
i_o_custkey
i_o_orderdate 4 const 6271
Using
where
1 SIMPLE customer eq_ref PRIMARY PRIMARY 4
dbt3.orders.
o_custkey
1 NULL
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 30
Range Optimizer
• Goal: find the "minimal" ranges for each index that needs to be read
• Example:
SELECT * FROM t1 WHERE (key1 > 10 AND key1 < 20) AND key2 > 30
• Range scan using INDEX(key1):
• Range scan using INDEX(key2):
10 20
30
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 31
Range Optimizer: Case Study
SELECT * FROM orders
WHERE YEAR(o_orderdate) = 1997 AND MONTH(o_orderdate) = 5
AND o_clerk = 'Clerk#000001866';
Why table scan?
id select type table type possible keys key key len ref rows extra
1 SIMPLE orders ALL NULL NULL NULL NULL 15000000 Using where
Index not considered
mysql> SELECT * FROM orders WHERE year(o_orderdate) = 1997 AND MONTH(…
...
15 rows in set (8.91 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 32
Range Optimizer: Case Study
SELECT * FROM orders
WHERE o_orderdate BETWEEN '1997-05-01' AND '1997-05-31'
AND o_clerk = 'Clerk#000001866';
Rewrite query to avoid functions on indexed columns
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE orders range i_o_orderdate i_o_orderdate 4 NULL 376352
Using index
condition;
Using where
mysql> SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND …
...
15 rows in set (0.91 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 33
Range Optimizer: Case Study
CREATE INDEX i_o_clerk ON orders(o_clerk);
SELECT * FROM orders
WHERE o_orderdate BETWEEN '1997-05-01' AND '1997-05-31'
AND o_clerk = 'Clerk#000001866';
Adding another index
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE orders range
i_o_orderdate,
i_o_clerk
i_o_clerk 16 NULL 1504
Using index condition;
Using where
mysql> SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND …
...
15 rows in set (0.01 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 34
Range Access for Multi-Column Indexes
• Table:
• INDEX idx(a, b, c);
• Logical storage layout of index:
Example table with multi-part index
10
1 2 3 4 5
10 11
1 2 3 4 5
12
1 2 3 4 5
13
1 2 3 4 5
a
b
c
11 12
pk a b c
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 35
Range Access for Multi-Column Indexes, cont
• Equality on 1st index column?
– Can add condition on 2nd index column to range condition
• Example:
SELECT * from t1 WHERE a IN (10,11,13) AND (b=2 OR b=4)
• Resulting range scan:
10
1 2 3 4 5
11
1 2 3 4 5
12
1 2 3 4 5
13
1 2 3 4 5
a
b
c
2 4 2 4 2 4
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 36
Range Access for Multi-Column Indexes, cont
• Non-Equality on 1st index column:
– Can NOT add condition on 2nd index column to range condition
• Example:
SELECT * from t1 WHERE a > 10 AND a < 13 AND (b=2 OR b=4)
• Resulting range scan:
10
1 2 3 4 5
11
1 2 3 4 5
12
1 2 3 4 5
13
1 2 3 4 5
a
b
c
a >10 AND a < 13
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 37
Range Optimizer: Case Study
CREATE INDEX i_o_clerk_date ON orders(o_clerk, o_orderdate);
SELECT * FROM orders
WHERE o_orderdate BETWEEN '1997-05-01' AND '1997-05-31'
AND o_clerk = 'Clerk#000001866';
Create multi-column index
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE orders range
i_o_orderdate,
i_o_clerk,
i_o_clerk_date
i_o_clerk_date 20 NULL 14
Using index
condition
mysql> SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND …
...
15 rows in set (0.00 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 38
Performance Schema: Query History
UPDATE performance_schema.setup_consumers
SET enabled='YES' WHERE name = 'events_statements_history';
mysql> SELECT sql_text, (timer_wait)/1000000000.0 "t (ms)", rows_examined rows
FROM performance_schema.events_statements_history ORDER BY timer_start;
+---------------------------------------------------------------+--------+------+
| sql_text | t (ms) | rows |
+---------------------------------------------------------------+--------+------+
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 8.1690 | 1505 |
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 7.2120 | 1505 |
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 8.1613 | 1505 |
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 7.0535 | 1505 |
| CREATE INDEX i_o_clerk_date ON orders(o_clerk,o_orderdate) |82036.4190 | 0 |
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.7259 | 15 |
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.5791 | 15 |
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.5423 | 15 |
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.6031 | 15 |
| SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.2710 | 15 |
+---------------------------------------------------------------+--------+------+
MySQL 5.7:
Enabled by default
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 39
Program Agenda
Cost-based query optimization in MySQL
Tools for monitoring, analyzing, and tuning queries
Data access and index selection
Join optimizer
Sorting
Influencing the optimizer
1
2
3
4
5
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 40
Join Optimizer
• Goal: Given a JOIN of N tables, find the best JOIN ordering
• Strategy:
– Start with all 1-table plans (Sorted based on size and key dependency)
– Expand each plan with remaining tables
• Depth-first
– If “cost of partial plan” > “cost of best plan”:
• “prune” plan
– Heuristic pruning:
• Prune less promising partial plans
• May in rare cases miss most optimal plan (turn off with set optimizer_prune_level = 0)
”Greedy search strategy”
t1
t2
t2
t2
t2
t3
t3
t3
t4t4
t4
t4t4
t3
t3 t2
t4t2 t3
N! possible
plans
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 41
JOIN Optimizer Illustrated
SELECT City.Name, Language FROM Language, Country, City
WHERE City.CountryCode = Country.Code
AND City.ID = Country.Capital
AND City.Population >= 1000000
AND Language.Country = Country.Code;
Language Country City
LanguageCountry
Country City
CityCity
City
cost=26568 cost=32568 cost=627
cost=1245
cost=862
start
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 42
Join Optimizer: Case study
SELECT o_year, SUM(CASE WHEN nation = 'FRANCE' THEN volume ELSE 0 END) / SUM(volume) AS
mkt_share
FROM (
SELECT EXTRACT(YEAR FROM o_orderdate) AS o_year,
l_extendedprice * (1 - l_discount) AS volume, n2.n_name AS nation
FROM part
JOIN lineitem ON p_partkey = l_partkey
JOIN supplier ON s_suppkey = l_suppkey
JOIN orders ON l_orderkey = o_orderkey
JOIN customer ON o_custkey = c_custkey
JOIN nation n1 ON c_nationkey = n1.n_nationkey
JOIN region ON n1.n_regionkey = r_regionkey
JOIN nation n2 ON s_nationkey = n2.n_nationkey
WHERE r_name = 'EUROPE' AND o_orderdate BETWEEN '1995-01-01' AND '1996-12-31'
AND p_type = 'PROMO BRUSHED STEEL'
) AS all_nations GROUP BY o_year ORDER BY o_year;
DBT-3 Query 8: National Market Share Query
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 43
Join Optimizer: Case Study
MySQL Workbench: Visual EXPLAIN
Execution time: 21 seconds
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 44
Join Optimizer: Case study
SELECT o_year, SUM(CASE WHEN nation = 'FRANCE' THEN volume ELSE 0 END) / SUM(volume) AS
mkt_share
FROM (
SELECT EXTRACT(YEAR FROM o_orderdate) AS o_year,
l_extendedprice * (1 - l_discount) AS volume, n2.n_name AS nation
FROM part
STRAIGHT_JOIN lineitem ON p_partkey = l_partkey
JOIN supplier ON s_suppkey = l_suppkey
JOIN orders ON l_orderkey = o_orderkey
JOIN customer ON o_custkey = c_custkey
JOIN nation n1 ON c_nationkey = n1.n_nationkey
JOIN region ON n1.n_regionkey = r_regionkey
JOIN nation n2 ON s_nationkey = n2.n_nationkey
WHERE r_name = 'EUROPE' AND o_orderdate BETWEEN '1995-01-01' AND '1996-12-31'
AND p_type = 'PROMO BRUSHED STEEL'
) AS all_nations GROUP BY o_year ORDER BY o_year;
Force early processing of high selectivity predicates
Highest selectivity
part before lineitem
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 45
Join Optimizer: Case study
Improved join order
Execution time: 3 seconds
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 46
MySQL 5.7: Cost Information in Structured EXPLAIN
Accumulated cost Total query cost
Cost per table
Improvements to Query 8 in MySQL 5.7:
• Filtering on non-indexed columns are taken into account
– No need for hint to force part table to be processed early
• Merge derived tables into outer query
– No temporary table
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 47
Program Agenda
Cost-based query optimization in MySQL
Tools for monitoring, analyzing, and tuning queries
Data access and index selection
Join optimizer
Sorting
Influencing the optimizer
1
2
3
4
5
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 48
ORDER BY Optimizations
• General solution; “Filesort”:
– Store query result in temporary table before sorting
– If data volume is large, may need to sort in several passes with intermediate storage
on disk.
• Optimizations:
– Take advantage of index to generate query result in sorted order
– For ”LIMIT n” queries, maintain priority queue of n top items in memory instead of
filesort. (MySQL 5.6)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 49
Filesort
SELECT * FROM orders ORDER BY o_totalprice ;
SELECT c_name, o_orderkey, o_totalprice
FROM orders JOIN customer ON c_custkey = o_custkey
WHERE c_acctbal < -1000 ORDER BY o_totalprice ;
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE customer ALL PRIMARY NULL NULL NULL 1500000
Using where;
Using temporary;
Using filesort
1 SIMPLE orders ref i_o_custkey i_o_custkey 5 ... 7 NULL
id
select
type
table type possible keys key
key
len
ref rows extra
1 SIMPLE orders ALL NULL NULL NULL NULL 15000000 Using filesort
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 50
Filesort
Status variables related to sorting:
mysql> show status like 'Sort%';
+-------------------+--------+
| Variable_name | Value |
+-------------------+--------+
| Sort_merge_passes | 1 |
| Sort_range | 0 |
| Sort_rows | 136170 |
| Sort_scan | 1 |
+-------------------+--------+
Status variables
>0: Intermediate storage on disk.
Consider increasing sort_buffer_size
Number of sort operations
(range scan or table/index scans)
Number of rows sorted
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 51
Filesort
Sorting status per statement available from Performance Schema
mysql> SELECT sql_text,sort_merge_passes,sort_range,sort_rows,sort_scan
FROM performance_schema.events_statements_history
ORDER BY timer_start DESC LIMIT 1;
+--------------+-------------------+------------+-----------+-----------+
| sql_text | sort_merge_passes | sort_range | sort_rows | sort_scan |
+--------------+-------------------+------------+-----------+-----------+
| SELECT ... | 1 | 0 | 136170 | 1 |
+--------------+-------------------+------------+-----------+-----------+
Performance Schema
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 52
mysql> FLUSH STATUS;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT AVG(o_totalprice) FROM
( SELECT * FROM orders
ORDER BY o_totalprice DESC
LIMIT 100000) td;
+-------------------+
| AVG(o_totalprice) |
+-------------------+
| 398185.986158 |
+-------------------+
1 row in set (24.65 sec)
mysql> SHOW STATUS LIKE 'sort%';
+-------------------+--------+
| Variable_name | Value |
+-------------------+--------+
| Sort_merge_passes | 1432 |
| Sort_range | 0 |
| Sort_rows | 100000 |
| Sort_scan | 1 |
+-------------------+--------+
4 rows in set (0.00 sec)
Filesort: Case Study
Unnecessary large data volume! Many intermediate sorting steps!
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 53
Filesort: Case Study
mysql> SELECT AVG(o_totalprice) FROM (SELECT o_totalprice FROM orders ORDER BY
o_totalprice DESC LIMIT 100000) td;
+-------------------+
| AVG(o_totalprice) |
+-------------------+
| 398185.986158 |
+-------------------+
1 row in set (8.18 sec)
mysql> SELECT sql_text, sort_merge_passes FROM performance_schema.
events_statements_history ORDER BY timer_start DESC LIMIT 1;
+----------------------------------------------------+-------------------+
| sql_text | sort_merge_passes |
+----------------------------------------------------+-------------------+
| SELECT AVG(o_totalprice) FROM (SELECT o_totalprice | 229 |
+----------------------------------------------------+-------------------+
Reduce amount of data to be sorted
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 54
Filesort: Case Study
mysql> SET sort_buffer_size = 1024*1024;
mysql> SELECT AVG(o_totalprice) FROM (SELECT o_totalprice FROM orders ORDER BY
o_totalprice DESC LIMIT 100000) td;
+-------------------+
| AVG(o_totalprice) |
+-------------------+
| 398185.986158 |
+-------------------+
1 row in set (7.24 sec)
mysql> SELECT sql_text, sort_merge_passes FROM performance_schema.
events_statements_history ORDER BY timer_start DESC LIMIT 1;
+----------------------------------------------------+-------------------+
| sql_text | sort_merge_passes |
+----------------------------------------------------+-------------------+
| SELECT AVG(o_totalprice) FROM (SELECT o_totalprice | 57 |
+----------------------------------------------------+-------------------+
Increase sort buffer (1 MB)
Default is 256 kB
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 55
Filesort: Case Study
mysql> SET sort_buffer_size = 8*1024*1024;
mysql> SELECT AVG(o_totalprice) FROM (SELECT o_totalprice FROM orders ORDER BY
o_totalprice DESC LIMIT 100000) td;
+-------------------+
| AVG(o_totalprice) |
+-------------------+
| 398185.986158 |
+-------------------+
1 row in set (6.30 sec)
mysql> SELECT sql_text, sort_merge_passes FROM performance_schema.
events_statements_history ORDER BY timer_start DESC LIMIT 1;
+----------------------------------------------------+-------------------+
| sql_text | sort_merge_passes |
+----------------------------------------------------+-------------------+
| SELECT AVG(o_totalprice) FROM (SELECT o_totalprice | 0 |
+----------------------------------------------------+-------------------+
Increase sort buffer even more (8 MB)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 56
Using Index to Avoid Sorting
CREATE INDEX i_o_totalprice ON orders(o_totalprice);
SELECT o_orderkey, o_totalprice FROM orders ORDER BY o_totalprice ;
id
select
type
table type
possible
keys
key key len ref rows extra
1 SIMPLE orders index NULL i_o_totalprice 6 NULL 15000000 Using index
SELECT * FROM orders ORDER BY o_totalprice ;
However, still (due to total cost):
id
select
type
table type
possible
keys
key key len ref rows extra
1 SIMPLE orders ALL NULL NULL NULL NULL 15000000 Using filesort
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 57
Using Index to Avoid Sorting
SELECT AVG(o_totalprice) FROM
(SELECT o_totalprice FROM orders ORDER BY o_totalprice DESC LIMIT 100000) td;
Case study revisited
id
select
type
table Type
possible
keys
key
key
len
ref rows extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 100000 NULL
2 DERIVED orders index NULL i_o_totalprice 6 NULL 15000000 Using index
mysql> SELECT AVG(o_totalprice) FROM (
SELECT o_totalprice FROM orders
ORDER BY o_totalprice DESC LIMIT 100000) td;
...
1 row in set (0.06 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 58
Program Agenda
Cost-based query optimization in MySQL
Tools for monitoring, analyzing, and tuning queries
Data access and index selection
Join optimizer
Sorting
Influencing the optimizer
1
2
3
4
5
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 59
Influencing the Optimizer
• Add indexes
• Force use of specific indexes:
– USE INDEX, FORCE INDEX, IGNORE INDEX
• Force specific join order:
– STRAIGHT_JOIN
• Adjust session variables
– optimizer_switch flags: set optimizer_switch="index_merge=off"
– Buffer sizes: set sort_buffer=8*1024*1024;
– Other variables: set optimizer_search_depth = 10;
When the optimizer does not do what you want
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 60
MySQL 5.7: New Optimizer Hints
• Ny hint syntax:
– SELECT /*+ HINT1(args) HINT2(args) */ … FROM …
• New hints:
– BKA(tables)/NO_BKA(tables), BNL(tables)/NO_BNL(tables)
– MRR(table indexes)/NO_MRR(table indexes)
– SEMIJOIN/NO_SEMIJOIN(strategies), SUBQUERY(strategy)
– NO_ICP(table indexes)
– NO_RANGE_OPTIMIZATION(table indexes)
– QB_NAME(name)
• Finer granularilty than optimizer_switch session variable
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 61
• Disable semi-join with hint:
EXPLAIN SELECT * FROM t2 WHERE t2.a IN (SELECT /*+ NO_SEMIJOIN() */ a FROM t3);
• No hint, optimizer chooses semi-join algorithm LooseScan:
EXPLAIN SELECT * FROM t2 WHERE t2.a IN (SELECT a FROM t3);
MySQL 5.7: Hint Example: SEMIJOIN
id select type table type
possible
keys
key
key
len
ref rows extra
1 SIMPLE t3 index a a 4 NULL 3 Using where; LooseScan
1 SIMPLE t2 ref a a 4 test.t3.a 1 Using index
id select type table type
possible
keys
key
key
len
ref rows extra
1 PRIMARY t2 index null a 4 NULL 4 Using where; Using index
2
DEPENDENT
SUBQUERY
t3
Index_
subquery
a a 4 func 1 Using index
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 62
MySQL 5.7: Hint Example: SEMIJOIN
• Force Semi-join Materialization to be used
EXPLAIN SELECT /*+ SEMIJOIN(@subq MATERIALIZATION) */ * FROM t2
WHERE t2.a IN (SELECT /*+ QB_NAME(subq) */ a FROM t3);
3 rows in set, 1 warning (0.01 sec)
id select type table type
possible
keys
key
key
len
ref rows extra
1 SIMPLE t2 index a a 4 NULL 4
Using where;
Using index
1 SIMPLE <subquery2> eq_ref <auto_key> <auto_key> 4 test.t2.a 1 NULL
2 MATERIALIZED t3 index a a 4 NULL 3 Using index
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 63
MySQL 5.7: Query Rewrite Plugin
• Rewrite problematic queries without the need to make application changes
– Add hints
– Modify join order
– Much more …
• Add rewrite rules to table:
INSERT INTO query_rewrite.rewrite_rules (pattern, replacement ) VALUES
("SELECT * FROM t1 WHERE a > ? AND b = ?",
"SELECT * FROM t1 FORCE INDEX (a_idx) WHERE a > ? AND b = ?");
• New pre- and post-parse query rewrite APIs
– Users can write their own plug-ins
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 64
More information
• My blog:
– http://oysteing.blogspot.com/
• Optimizer team blog:
– http://mysqloptimizerteam.blogspot.com/
• MySQL Server Team blog
– http://mysqlserverteam.com/
• MySQL forums:
– Optimizer & Parser: http://forums.mysql.com/list.php?115
– Performance: http://forums.mysql.com/list.php?24
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Q+A
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 66
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 6767
68

More Related Content

What's hot

Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersCarlos Sierra
 
Oracle RAC 12c (12.1.0.2) Operational Best Practices - A result of true colla...
Oracle RAC 12c (12.1.0.2) Operational Best Practices - A result of true colla...Oracle RAC 12c (12.1.0.2) Operational Best Practices - A result of true colla...
Oracle RAC 12c (12.1.0.2) Operational Best Practices - A result of true colla...Markus Michalewicz
 
JSON improvements in MySQL 8.0
JSON improvements in MySQL 8.0JSON improvements in MySQL 8.0
JSON improvements in MySQL 8.0Mydbops
 
New availability features in oracle rac 12c release 2 anair ss
New availability features in oracle rac 12c release 2 anair   ssNew availability features in oracle rac 12c release 2 anair   ss
New availability features in oracle rac 12c release 2 anair ssAnil Nair
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Aaron Shilo
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Kyle Hailey
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLSergey Petrunya
 
Explain the explain_plan
Explain the explain_planExplain the explain_plan
Explain the explain_planMaria Colgan
 
BEST_PRACTICES: Buenas prácticas para el DBA
BEST_PRACTICES: Buenas prácticas para el DBA BEST_PRACTICES: Buenas prácticas para el DBA
BEST_PRACTICES: Buenas prácticas para el DBA SolidQ
 
Understanding Oracle RAC 12c Internals OOW13 [CON8806]
Understanding Oracle RAC 12c Internals OOW13 [CON8806]Understanding Oracle RAC 12c Internals OOW13 [CON8806]
Understanding Oracle RAC 12c Internals OOW13 [CON8806]Markus Michalewicz
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsEnkitec
 
Low Level CPU Performance Profiling Examples
Low Level CPU Performance Profiling ExamplesLow Level CPU Performance Profiling Examples
Low Level CPU Performance Profiling ExamplesTanel Poder
 
Oracle db performance tuning
Oracle db performance tuningOracle db performance tuning
Oracle db performance tuningSimon Huang
 
Oracle RAC 19c: Best Practices and Secret Internals
Oracle RAC 19c: Best Practices and Secret InternalsOracle RAC 19c: Best Practices and Secret Internals
Oracle RAC 19c: Best Practices and Secret InternalsAnil Nair
 
Oracle 12cR2 Installation On Linux With ASM
Oracle 12cR2 Installation On Linux With ASMOracle 12cR2 Installation On Linux With ASM
Oracle 12cR2 Installation On Linux With ASMArun Sharma
 
Paper: Oracle RAC Internals - The Cache Fusion Edition
Paper: Oracle RAC Internals - The Cache Fusion EditionPaper: Oracle RAC Internals - The Cache Fusion Edition
Paper: Oracle RAC Internals - The Cache Fusion EditionMarkus Michalewicz
 
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsDB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsJohn Beresniewicz
 
Keeping Spark on Track: Productionizing Spark for ETL
Keeping Spark on Track: Productionizing Spark for ETLKeeping Spark on Track: Productionizing Spark for ETL
Keeping Spark on Track: Productionizing Spark for ETLDatabricks
 

What's hot (20)

PostgreSQL replication
PostgreSQL replicationPostgreSQL replication
PostgreSQL replication
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginners
 
Oracle RAC 12c (12.1.0.2) Operational Best Practices - A result of true colla...
Oracle RAC 12c (12.1.0.2) Operational Best Practices - A result of true colla...Oracle RAC 12c (12.1.0.2) Operational Best Practices - A result of true colla...
Oracle RAC 12c (12.1.0.2) Operational Best Practices - A result of true colla...
 
JSON improvements in MySQL 8.0
JSON improvements in MySQL 8.0JSON improvements in MySQL 8.0
JSON improvements in MySQL 8.0
 
New availability features in oracle rac 12c release 2 anair ss
New availability features in oracle rac 12c release 2 anair   ssNew availability features in oracle rac 12c release 2 anair   ss
New availability features in oracle rac 12c release 2 anair ss
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQL
 
Explain the explain_plan
Explain the explain_planExplain the explain_plan
Explain the explain_plan
 
BEST_PRACTICES: Buenas prácticas para el DBA
BEST_PRACTICES: Buenas prácticas para el DBA BEST_PRACTICES: Buenas prácticas para el DBA
BEST_PRACTICES: Buenas prácticas para el DBA
 
Understanding Oracle RAC 12c Internals OOW13 [CON8806]
Understanding Oracle RAC 12c Internals OOW13 [CON8806]Understanding Oracle RAC 12c Internals OOW13 [CON8806]
Understanding Oracle RAC 12c Internals OOW13 [CON8806]
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
Oracle RAC 12c Overview
Oracle RAC 12c OverviewOracle RAC 12c Overview
Oracle RAC 12c Overview
 
Low Level CPU Performance Profiling Examples
Low Level CPU Performance Profiling ExamplesLow Level CPU Performance Profiling Examples
Low Level CPU Performance Profiling Examples
 
Oracle db performance tuning
Oracle db performance tuningOracle db performance tuning
Oracle db performance tuning
 
Oracle RAC 19c: Best Practices and Secret Internals
Oracle RAC 19c: Best Practices and Secret InternalsOracle RAC 19c: Best Practices and Secret Internals
Oracle RAC 19c: Best Practices and Secret Internals
 
Oracle 12cR2 Installation On Linux With ASM
Oracle 12cR2 Installation On Linux With ASMOracle 12cR2 Installation On Linux With ASM
Oracle 12cR2 Installation On Linux With ASM
 
Paper: Oracle RAC Internals - The Cache Fusion Edition
Paper: Oracle RAC Internals - The Cache Fusion EditionPaper: Oracle RAC Internals - The Cache Fusion Edition
Paper: Oracle RAC Internals - The Cache Fusion Edition
 
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsDB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
 
Keeping Spark on Track: Productionizing Spark for ETL
Keeping Spark on Track: Productionizing Spark for ETLKeeping Spark on Track: Productionizing Spark for ETL
Keeping Spark on Track: Productionizing Spark for ETL
 

Viewers also liked

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
 
MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions oysteing
 
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 8.0: Common Table Expressions
MySQL 8.0: Common Table ExpressionsMySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressionsoysteing
 
Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Dave Stokes
 
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
 
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 percona15oysteing
 
MySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. RyengMySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. Ryeng郁萍 王
 
What Your Database Query is Really Doing
What Your Database Query is Really DoingWhat Your Database Query is Really Doing
What Your Database Query is Really DoingDave Stokes
 
MySQL 8.0: GIS — Are you ready?
MySQL 8.0: GIS — Are you ready?MySQL 8.0: GIS — Are you ready?
MySQL 8.0: GIS — Are you ready?Norvald Ryeng
 
MySQL Performance Tips & Best Practices
MySQL Performance Tips & Best PracticesMySQL Performance Tips & Best Practices
MySQL Performance Tips & Best PracticesIsaac Mosquera
 
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 webinaroysteing
 
SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQLDag H. Wanvik
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index TuningManikanda kumar
 
My sql explain cheat sheet
My sql explain cheat sheetMy sql explain cheat sheet
My sql explain cheat sheetAchievers Tech
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL QueriesAchievers Tech
 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server DefaultsMorgan Tocker
 
What you wanted to know about MySQL, but could not find using inernal instrum...
What you wanted to know about MySQL, but could not find using inernal instrum...What you wanted to know about MySQL, but could not find using inernal instrum...
What you wanted to know about MySQL, but could not find using inernal instrum...Sveta Smirnova
 
How to analyze and tune sql queries for better performance
How to analyze and tune sql queries for better performanceHow to analyze and tune sql queries for better performance
How to analyze and tune sql queries for better performanceoysteing
 
How Booking.com avoids and deals with replication lag
How Booking.com avoids and deals with replication lagHow Booking.com avoids and deals with replication lag
How Booking.com avoids and deals with replication lagJean-François Gagné
 

Viewers also liked (20)

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
 
MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions
 
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 8.0: Common Table Expressions
MySQL 8.0: Common Table ExpressionsMySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions
 
Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016
 
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
 
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
 
MySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. RyengMySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. Ryeng
 
What Your Database Query is Really Doing
What Your Database Query is Really DoingWhat Your Database Query is Really Doing
What Your Database Query is Really Doing
 
MySQL 8.0: GIS — Are you ready?
MySQL 8.0: GIS — Are you ready?MySQL 8.0: GIS — Are you ready?
MySQL 8.0: GIS — Are you ready?
 
MySQL Performance Tips & Best Practices
MySQL Performance Tips & Best PracticesMySQL Performance Tips & Best Practices
MySQL Performance Tips & Best Practices
 
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
 
SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQL
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
My sql explain cheat sheet
My sql explain cheat sheetMy sql explain cheat sheet
My sql explain cheat sheet
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server Defaults
 
What you wanted to know about MySQL, but could not find using inernal instrum...
What you wanted to know about MySQL, but could not find using inernal instrum...What you wanted to know about MySQL, but could not find using inernal instrum...
What you wanted to know about MySQL, but could not find using inernal instrum...
 
How to analyze and tune sql queries for better performance
How to analyze and tune sql queries for better performanceHow to analyze and tune sql queries for better performance
How to analyze and tune sql queries for better performance
 
How Booking.com avoids and deals with replication lag
How Booking.com avoids and deals with replication lagHow Booking.com avoids and deals with replication lag
How Booking.com avoids and deals with replication lag
 

Similar to How to analyze and tune sql queries for better performance vts2016

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
 
Optimizer overviewoow2014
Optimizer overviewoow2014Optimizer overviewoow2014
Optimizer overviewoow2014Mysql User Camp
 
MySQL Optimizer Cost Model
MySQL Optimizer Cost ModelMySQL Optimizer Cost Model
MySQL Optimizer Cost ModelOlav Sandstå
 
Mysql Performance Schema - fossasia 2016
Mysql Performance Schema - fossasia 2016Mysql Performance Schema - fossasia 2016
Mysql Performance Schema - fossasia 2016Mayank Prasad
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizerMaria Colgan
 
MySQL Performance Schema : fossasia
MySQL Performance Schema : fossasiaMySQL Performance Schema : fossasia
MySQL Performance Schema : fossasiaMayank Prasad
 
MySQL-Performance Schema- What's new in MySQL-5.7 DMRs
MySQL-Performance Schema- What's new in MySQL-5.7 DMRsMySQL-Performance Schema- What's new in MySQL-5.7 DMRs
MySQL-Performance Schema- What's new in MySQL-5.7 DMRsMayank Prasad
 
Advanced SQL - Quebec 2014
Advanced SQL - Quebec 2014Advanced SQL - Quebec 2014
Advanced SQL - Quebec 2014Connor McDonald
 
Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0oysteing
 
12 1-man-operation center-ug(2)
12 1-man-operation center-ug(2)12 1-man-operation center-ug(2)
12 1-man-operation center-ug(2)Ron DeLong
 
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
제3회난공불락 오픈소스 인프라세미나 - MySQL PerformanceTommy Lee
 
IDERA Live | Leverage the Query Store for Better SQL Server Performance
IDERA Live | Leverage the Query Store for Better SQL Server PerformanceIDERA Live | Leverage the Query Store for Better SQL Server Performance
IDERA Live | Leverage the Query Store for Better SQL Server PerformanceIDERA Software
 
MySQL Optimizer: What's New in 8.0
MySQL Optimizer: What's New in 8.0MySQL Optimizer: What's New in 8.0
MySQL Optimizer: What's New in 8.0Manyi Lu
 
Confoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsConfoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsDave Stokes
 
Instrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaInstrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaMark Leith
 
RivieraJUG - MySQL Indexes and Histograms
RivieraJUG - MySQL Indexes and HistogramsRivieraJUG - MySQL Indexes and Histograms
RivieraJUG - MySQL Indexes and HistogramsFrederic Descamps
 
Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Morgan Tocker
 
Oracle super cluster for oracle e business suite
Oracle super cluster for oracle e business suiteOracle super cluster for oracle e business suite
Oracle super cluster for oracle e business suiteOTN Systems Hub
 
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Dutch PHP Conference 2021 - MySQL Indexes and HistogramsDutch PHP Conference 2021 - MySQL Indexes and Histograms
Dutch PHP Conference 2021 - MySQL Indexes and HistogramsDave Stokes
 

Similar to How to analyze and tune sql queries for better performance vts2016 (20)

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
 
Optimizer overviewoow2014
Optimizer overviewoow2014Optimizer overviewoow2014
Optimizer overviewoow2014
 
MySQL Optimizer Cost Model
MySQL Optimizer Cost ModelMySQL Optimizer Cost Model
MySQL Optimizer Cost Model
 
Mysql Performance Schema - fossasia 2016
Mysql Performance Schema - fossasia 2016Mysql Performance Schema - fossasia 2016
Mysql Performance Schema - fossasia 2016
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizer
 
MySQL Performance Schema : fossasia
MySQL Performance Schema : fossasiaMySQL Performance Schema : fossasia
MySQL Performance Schema : fossasia
 
MySQL-Performance Schema- What's new in MySQL-5.7 DMRs
MySQL-Performance Schema- What's new in MySQL-5.7 DMRsMySQL-Performance Schema- What's new in MySQL-5.7 DMRs
MySQL-Performance Schema- What's new in MySQL-5.7 DMRs
 
Advanced SQL - Quebec 2014
Advanced SQL - Quebec 2014Advanced SQL - Quebec 2014
Advanced SQL - Quebec 2014
 
Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0
 
12 1-man-operation center-ug(2)
12 1-man-operation center-ug(2)12 1-man-operation center-ug(2)
12 1-man-operation center-ug(2)
 
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
 
IDERA Live | Leverage the Query Store for Better SQL Server Performance
IDERA Live | Leverage the Query Store for Better SQL Server PerformanceIDERA Live | Leverage the Query Store for Better SQL Server Performance
IDERA Live | Leverage the Query Store for Better SQL Server Performance
 
MySQL Optimizer: What's New in 8.0
MySQL Optimizer: What's New in 8.0MySQL Optimizer: What's New in 8.0
MySQL Optimizer: What's New in 8.0
 
Confoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsConfoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & Histograms
 
AWR, ASH with EM13 at HotSos 2016
AWR, ASH with EM13 at HotSos 2016AWR, ASH with EM13 at HotSos 2016
AWR, ASH with EM13 at HotSos 2016
 
Instrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaInstrumenting plugins for Performance Schema
Instrumenting plugins for Performance Schema
 
RivieraJUG - MySQL Indexes and Histograms
RivieraJUG - MySQL Indexes and HistogramsRivieraJUG - MySQL Indexes and Histograms
RivieraJUG - MySQL Indexes and Histograms
 
Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7
 
Oracle super cluster for oracle e business suite
Oracle super cluster for oracle e business suiteOracle super cluster for oracle e business suite
Oracle super cluster for oracle e business suite
 
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Dutch PHP Conference 2021 - MySQL Indexes and HistogramsDutch PHP Conference 2021 - MySQL Indexes and Histograms
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
 

More from oysteing

POLARDB: A database architecture for the cloud
POLARDB: A database architecture for the cloudPOLARDB: A database architecture for the cloud
POLARDB: A database architecture for the cloudoysteing
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Traceoysteing
 
POLARDB: A database architecture for the cloud
POLARDB: A database architecture for the cloudPOLARDB: A database architecture for the cloud
POLARDB: A database architecture for the cloudoysteing
 
POLARDB for MySQL - Parallel Query
POLARDB for MySQL - Parallel QueryPOLARDB for MySQL - Parallel Query
POLARDB for MySQL - Parallel Queryoysteing
 
JSON_TABLE -- The best of both worlds
JSON_TABLE -- The best of both worldsJSON_TABLE -- The best of both worlds
JSON_TABLE -- The best of both worldsoysteing
 
MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0oysteing
 
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
Common Table Expressions (CTE) & Window Functions in MySQL 8.0Common Table Expressions (CTE) & Window Functions in MySQL 8.0
Common Table Expressions (CTE) & Window Functions in MySQL 8.0oysteing
 

More from oysteing (7)

POLARDB: A database architecture for the cloud
POLARDB: A database architecture for the cloudPOLARDB: A database architecture for the cloud
POLARDB: A database architecture for the cloud
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
POLARDB: A database architecture for the cloud
POLARDB: A database architecture for the cloudPOLARDB: A database architecture for the cloud
POLARDB: A database architecture for the cloud
 
POLARDB for MySQL - Parallel Query
POLARDB for MySQL - Parallel QueryPOLARDB for MySQL - Parallel Query
POLARDB for MySQL - Parallel Query
 
JSON_TABLE -- The best of both worlds
JSON_TABLE -- The best of both worldsJSON_TABLE -- The best of both worlds
JSON_TABLE -- The best of both worlds
 
MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0
 
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
Common Table Expressions (CTE) & Window Functions in MySQL 8.0Common Table Expressions (CTE) & Window Functions in MySQL 8.0
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
 

Recently uploaded

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 

Recently uploaded (20)

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 

How to analyze and tune sql queries for better performance vts2016

  • 1. Copyright © 2016 Oracle and/or its affiliates. All rights reserved. | How to Analyze and Tune MySQL Queries for Better Performance Øystein Grøvlen Senior Principal Software Engineer MySQL Optimizer Team, Oracle March/April, 2016 Please Stand By. This session will begin promptly at the time indicated on the agenda. Thank You.
  • 2. Copyright © 2016 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.
  • 3. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 3 Program Agenda Cost-based query optimization in MySQL Tools for monitoring, analyzing, and tuning queries Data access and index selection Join optimizer Sorting Influencing the optimizer 1 2 3 4 5 6
  • 4. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 4 Program Agenda Cost-based query optimization in MySQL Tools for monitoring, analyzing, and tuning queries Data access and index selection Join optimizer Sorting Influencing the optimizer 1 2 3 4 5 6
  • 5. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 5 MySQL Optimizer SELECT a, b FROM t1, t2, t3 WHERE t1.a = t2.b AND t2.b = t3.c AND t2.d > 20 AND t2.d < 30; MySQL Server Cost based optimizations Heuristics Cost Model Optimizer Table/index info (data dictionary) Statistics (storage engines) t2 t3 t1 Table scan Range scan Ref access JOIN JOIN
  • 6. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 6 Cost-based Query Optimization • Assign cost to operations • Assign cost to partial or alternative plans • Search for plan with lowest cost • Cost-based optimizations: General idea Access method Subquery strategyJoin order t2 t3 t1 Table scan Range scan Ref access JOIN JOIN
  • 7. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 7 Optimizer Cost Model t1 Cost estimate Row estimate Cost Model Cost formulas Access methods Join Subquery Cost constants CPU IO Metadata: - Row and index size - Index information - Uniqueness Statistics: - Table size - Cardinality - Range estimates Cost model configuration Range scan JOIN New in MySQL 5.7
  • 8. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 8 Cost Model Example Table scan: • IO-cost: #pages in table * IO_BLOCK_READ_COST • CPU cost: #rows * ROW_EVALUATE_COST Range scan (on secondary index): • IO-cost: #rows_in_range * IO_BLOCK_READ_COST • CPU cost: #rows_in_range * ROW_EVALUATE_COST SELECT SUM(o_totalprice) FROM orders WHERE o_orderdate BETWEEN '1994-01-01' AND '1994-12-31';
  • 9. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 9 Cost Model Example EXPLAIN SELECT SUM(o_totalprice) FROM orders WHERE o_orderdate BETWEEN '1994-01-01' AND '1994-12-31'; EXPLAIN SELECT SUM(o_totalprice) FROM orders WHERE o_orderdate BETWEEN '1994-01-01' AND '1994-06-30'; id select type table type possible keys key key len ref rows extra 1 SIMPLE orders ALL i_o_orderdate NULL NULL NULL 15000000 Using where Id select type table type possible keys key key len ref rows extra 1 SIMPLE orders range i_o_orderdate i_o_orderdate 4 NULL 2235118 Using index condition
  • 10. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 10 Cost Model Example: Optimizer Trace join_optimization / row_estimation / table : orders / range_analysis "table_scan": { "rows": 15000000, "cost": 3.12e6 } /* table_scan */, "potential_range_indices": [ { "index": "PRIMARY", "usable": false, "cause": "not_applicable" }, { "index": "i_o_orderdate", "usable": true, "key_parts": [ "o_orderDATE", "o_orderkey" ] } ] /* potential_range_indices */, … "analyzing_range_alternatives": { "range_scan_alternatives": [ { "index": "i_o_orderdate", "ranges": [ "1994-01-01 <= o_orderDATE <= 1994-12-31" ], "index_dives_for_eq_ranges": true, "rowid_ordered": false, "using_mrr": false, "index_only": false, "rows": 4489990, "cost": 5.39e6, "chosen": false, "cause": "cost" } ] /* range_scan_alternatives */, … } /* analyzing_range_alternatives */
  • 11. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 11 Cost Model vs Real World Data in Memory Data on Disk Data on SSD Table scan 6.8 seconds 36 seconds 15 seconds Index scan 5.2 seconds 2.5 hours 30 minutes Measured Execution Times Force Index Scan: SELECT SUM(o_totalprice) FROM orders FORCE INDEX (i_o_orderdate) WHERE o_orderdate BETWEEN '1994-01-01' AND '1994-12-31';
  • 12. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 12 Performance Schema SELECT event_name, count_read, avg_timer_read/1000000000.0 "Avg Read Time (ms)", sum_number_of_bytes_read "Bytes Read" FROM performance_schema.file_summary_by_event_name WHERE event_name='wait/io/file/innodb/innodb_data_file'; Disk I/O event_name count_read Avg Read Time (ms) Bytes Read wait/io/file/innodb/innodb_data_file 2188853 4.2094 35862167552 event_name count_read Avg Read Time (ms) Bytes Read wait/io/file/innodb/innodb_data_file 115769 0.0342 1896759296 Index Scan Table Scan
  • 13. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 13 Program Agenda Cost-based query optimization in MySQL Tools for monitoring, analyzing, and tuning queries Data access and index selection Join optimizer Sorting Influencing the optimizer 1 2 3 4 5 6
  • 14. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 14 Useful tools • MySQL Enterprise Monitor (MEM), Query Analyzer – Commercial product • Performance schema, MySQL sys schema • EXPLAIN – Tabular EXPLAIN – Structured EXPLAIN (FORMAT=JSON) – Visual EXPLAIN (MySQL Workbench) • Optimizer trace • Slow log • Status variables (SHOW STATUS LIKE 'Sort%')
  • 15. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 15 MySQL Enterprise Monitor, Query Analyzer
  • 16. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 16 Query Analyzer Query Details
  • 17. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 17 Performance Schema • events_statements_history events_statements_history_long – Most recent statements executed • events_statements_summary_by_digest – Summary for similar statements (same statement digest) • file_summary_by_event_name – Interesting event: wait/io/file/innodb/innodb_data_file • table_io_waits_summary_by_table table_io_waits_summary_by_index_usage – Statistics on storage engine access per table and index Some useful tables
  • 18. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 18 Performance Schema • Tables: events_statements_current (Current statement for each thread) events_statements_history (10 most recent statements per thread) events_statements_history_long (10000 most recent statements) • Columns: THREAD_ID, EVENT_ID, END_EVENT_ID, EVENT_NAME, SOURCE, TIMER_START, TIMER_END, TIMER_WAIT, LOCK_TIME, SQL_TEXT, DIGEST, DIGEST_TEXT, CURRENT_SCHEMA, OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, OBJECT_INSTANCE_BEGIN, MYSQL_ERRNO, RETURNED_SQLSTATE, MESSAGE_TEXT, ERRORS, WARNINGS, ROWS_AFFECTED, ROWS_SENT, ROWS_EXAMINED, CREATED_TMP_DISK_TABLES, CREATED_TMP_TABLES, SELECT_FULL_JOIN, SELECT_FULL_RANGE_JOIN, SELECT_RANGE, SELECT_RANGE_CHECK, SELECT_SCAN, SORT_MERGE_PASSES, SORT_RANGE, SORT_ROWS, SORT_SCAN, NO_INDEX_USED, NO_GOOD_INDEX_USED, NESTING_EVENT_ID, NESTING_EVENT_TYPE Statement events
  • 19. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 19 Performance Schema • Normalization of queries to group statements that are similar to be grouped and summarized: SELECT * FROM orders WHERE o_custkey=10 AND o_totalprice>20 SELECT * FROM orders WHERE o_custkey = 20 AND o_totalprice > 100 SELECT * FROM orders WHERE o_custkey = ? AND o_totalprice > ? • events_statements_summary_by_digest DIGEST, DIGEST_TEXT, COUNT_STAR, SUM_TIMER_WAIT, MIN_TIMER_WAIT, AVG_TIMER_WAIT, MAX_TIMER_WAIT, SUM_LOCK_TIME, SUM_ERRORS, SUM_WARNINGS, SUM_ROWS_AFFECTED, SUM_ROWS_SENT, SUM_ROWS_EXAMINED, SUM_CREATED_TMP_DISK_TABLES, SUM_CREATED_TMP_TABLES, SUM_SELECT_FULL_JOIN, SUM_SELECT_FULL_RANGE_JOIN, SUM_SELECT_RANGE, SUM_SELECT_RANGE_CHECK, SUM_SELECT_SCAN, SUM_SORT_MERGE_PASSES, SUM_SORT_RANGE, SUM_SORT_ROWS, SUM_SORT_SCAN, SUM_NO_INDEX_USED, SUM_NO_GOOD_INDEX_USED, FIRST_SEEN, LAST_SEEN Statement digest
  • 20. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 20 MySQL sys Schema • A collection of views, procedures and functions, designed to make reading raw Performance Schema data easier • Implements many common DBA and Developer use cases – File IO usage per user – Which indexes is never used? – Which queries use full table scans? • Examples of very useful functions: – format_time() , format_bytes(), format_statement() • Included with MySQL 5.7 • Bundled with MySQL Workbench
  • 21. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 21 MySQL sys Schema statement_analysis: Lists a normalized statement view with aggregated statistics, ordered by the total execution time per normalized statement mysql> SELECT * FROM sys.statement_analysis LIMIT 1G *************************** 1. row *************************** query: INSERT INTO `mem__quan` . `nor ... nDuration` = IF ( VALUES ( ... db: mem full_scan: 0 exec_count: 1110067 err_count: 0 warn_count: 0 total_latency: 1.93h max_latency: 5.03 s avg_latency: 6.27 ms lock_latency: 00:18:29.18 Example rows_sent: 0 rows_sent_avg: 0 rows_examined: 0 rows_examined_avg: 0 tmp_tables: 0 tmp_disk_tables: 0 rows_sorted: 0 sort_merge_passes: 0 digest: d48316a218e95b1b8b72db5e6b177788! first_seen: 2014-05-20 10:42:17
  • 22. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 22 EXPLAIN • Use EXPLAIN to print the final query plan: • Explain for a running query (New in MySQL 5.7): EXPLAIN FOR CONNECTION connection_id; Understand the query plan EXPLAIN SELECT * FROM t1 JOIN t2 ON t1.a = t2.a WHERE b > 10 AND c > 10; +----+--------+-------+------------+------+---------------+-----+---------+-----+------+----------+------------+ | id | select…| table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------+-------+------------+------+---------------+-----+---------+-----+------+----------+------------+ | 1 | SIMPLE | t1 | NULL | range| idx1 | idx1| 4 | NULL| 12 | 33.33 | Using where| | 1 | SIMPLE | t2 | NULL | ref | idx2 | idx2| 4 | t1.a| 1 | 100.00 | NULL | +----+--------+-------+------------+------+---------------+-----+---------+-----+------+----------+------------+
  • 23. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 23 Added in MySQL 5.7 Structured EXPLAIN • JSON format: • Contains more information: – Used index parts – Pushed index conditions – Cost estimates – Data estimates EXPLAIN FORMAT=JSON SELECT * FROM t1 WHERE b > 10 AND c > 10; EXPLAIN { "query_block": { "select_id": 1, "cost_info": { "query_cost": "17.81" }, "table": { "table_name": "t1", "access_type": "range", "possible_keys": [ "idx1" ], "key": "idx1", "used_key_parts": [ "b" ], "key_length": "4", "rows_examined_per_scan": 12, "rows_produced_per_join": 3, "filtered": "33.33", "index_condition": "(`test`.`t1`.`b` > 10)", "cost_info": { "read_cost": "17.01", "eval_cost": "0.80", "prefix_cost": "17.81", "data_read_per_join": "63" }, ……… "attached_condition": "(`test`.`t1`.`c` > 10)" } } } EXPLAIN FORMAT=JSON SELECT …
  • 24. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 24 EXPLAIN { "query_block": { "select_id": 1, "nested_loop": [ { "table": { "table_name": "t1", "access_type": "ALL", "rows": 10, "filtered": 100, "attached_condition": "(t1.a = 9)" } /* table */ }, { "table": { "table_name": "t2", "access_type": "ALL", "rows": 10, "filtered": 100, "using_join_buffer": "Block Nested Loop", "attached_condition": "((t2.a = 9) and ((t2.b <= 3) or ((t2.b = 5) and (t1.b = 12))))" } /* table */ } ] /* nested_loop */ } /* query_block */ } Structured EXPLAIN Assigning Conditions to Tables EXPLAIN FORMAT=JSON SELECT * FROM t1, t2 WHERE t1.a=t2.a AND t2.a=9 AND (NOT (t1.a > 10 OR t2.b >3) OR (t1.b=t2.b+7 AND t2.b = 5));
  • 25. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 25 Optimizer Trace: Query Plan Debugging • EXPLAIN shows the selected plan • Optimizer trace shows WHY the plan was selected SET optimizer_trace= "enabled=on"; SELECT * FROM t1,t2 WHERE f1=1 AND f1=f2 AND f2>0; SELECT trace FROM information_schema.optimizer_trace INTO OUTFILE <filename> LINES TERMINATED BY ''; SET optimizer_trace="enabled=off"; QUERY SELECT * FROM t1,t2 WHERE f1=1 AND f1=f2 AND f2>0; TRACE "steps": [ { "join_preparation": { "select#": 1,… } … } …] MISSING_BYTES_BEYOND_MAX_MEM_SIZE 0 INSUFFICIENT_PRIVILEGES 0
  • 26. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 26 Program Agenda Cost-based query optimization in MySQL Tools for monitoring, analyzing, and tuning queries Data access and index selection Join optimizer Sorting Influencing the optimizer 1 2 3 4 5 6
  • 27. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 27 Selecting Access Method • For each table, find the best access method: – Check if the access method is useful – Estimate cost of using access method – Select the cheapest to be used • Choice of access method is cost based Finding the optimal method to read data from storage engine Main access methods:  Table scan  Index scan  Index look-up (ref access)  Range scan  Index merge  Loose index scan
  • 28. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 28 Ref Access EXPLAIN SELECT * FROM customer WHERE c_custkey = 570887; Single Table Queries id select type table type possible keys key key len ref rows extra 1 SIMPLE customer const PRIMARY PRIMARY 4 const 1 NULL EXPLAIN SELECT * FROM orders WHERE o_orderdate = '1992-09-12'; id select type table type possible keys key key len ref rows extra 1 SIMPLE orders ref i_o_orderdate i_o_orderdate 4 const 6271 NULL
  • 29. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 29 Ref Access Join Queries EXPLAIN SELECT * FROM orders JOIN customer ON c_custkey = o_custkey WHERE o_orderdate = '1992-09-12'; id select type table type possible keys key key len ref rows extra 1 SIMPLE orders ref i_o_orderdate, i_o_custkey i_o_orderdate 4 const 6271 Using where 1 SIMPLE customer eq_ref PRIMARY PRIMARY 4 dbt3.orders. o_custkey 1 NULL
  • 30. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 30 Range Optimizer • Goal: find the "minimal" ranges for each index that needs to be read • Example: SELECT * FROM t1 WHERE (key1 > 10 AND key1 < 20) AND key2 > 30 • Range scan using INDEX(key1): • Range scan using INDEX(key2): 10 20 30
  • 31. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 31 Range Optimizer: Case Study SELECT * FROM orders WHERE YEAR(o_orderdate) = 1997 AND MONTH(o_orderdate) = 5 AND o_clerk = 'Clerk#000001866'; Why table scan? id select type table type possible keys key key len ref rows extra 1 SIMPLE orders ALL NULL NULL NULL NULL 15000000 Using where Index not considered mysql> SELECT * FROM orders WHERE year(o_orderdate) = 1997 AND MONTH(… ... 15 rows in set (8.91 sec)
  • 32. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 32 Range Optimizer: Case Study SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND '1997-05-31' AND o_clerk = 'Clerk#000001866'; Rewrite query to avoid functions on indexed columns id select type table type possible keys key key len ref rows extra 1 SIMPLE orders range i_o_orderdate i_o_orderdate 4 NULL 376352 Using index condition; Using where mysql> SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND … ... 15 rows in set (0.91 sec)
  • 33. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 33 Range Optimizer: Case Study CREATE INDEX i_o_clerk ON orders(o_clerk); SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND '1997-05-31' AND o_clerk = 'Clerk#000001866'; Adding another index id select type table type possible keys key key len ref rows extra 1 SIMPLE orders range i_o_orderdate, i_o_clerk i_o_clerk 16 NULL 1504 Using index condition; Using where mysql> SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND … ... 15 rows in set (0.01 sec)
  • 34. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 34 Range Access for Multi-Column Indexes • Table: • INDEX idx(a, b, c); • Logical storage layout of index: Example table with multi-part index 10 1 2 3 4 5 10 11 1 2 3 4 5 12 1 2 3 4 5 13 1 2 3 4 5 a b c 11 12 pk a b c
  • 35. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 35 Range Access for Multi-Column Indexes, cont • Equality on 1st index column? – Can add condition on 2nd index column to range condition • Example: SELECT * from t1 WHERE a IN (10,11,13) AND (b=2 OR b=4) • Resulting range scan: 10 1 2 3 4 5 11 1 2 3 4 5 12 1 2 3 4 5 13 1 2 3 4 5 a b c 2 4 2 4 2 4
  • 36. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 36 Range Access for Multi-Column Indexes, cont • Non-Equality on 1st index column: – Can NOT add condition on 2nd index column to range condition • Example: SELECT * from t1 WHERE a > 10 AND a < 13 AND (b=2 OR b=4) • Resulting range scan: 10 1 2 3 4 5 11 1 2 3 4 5 12 1 2 3 4 5 13 1 2 3 4 5 a b c a >10 AND a < 13
  • 37. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 37 Range Optimizer: Case Study CREATE INDEX i_o_clerk_date ON orders(o_clerk, o_orderdate); SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND '1997-05-31' AND o_clerk = 'Clerk#000001866'; Create multi-column index id select type table type possible keys key key len ref rows extra 1 SIMPLE orders range i_o_orderdate, i_o_clerk, i_o_clerk_date i_o_clerk_date 20 NULL 14 Using index condition mysql> SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' AND … ... 15 rows in set (0.00 sec)
  • 38. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 38 Performance Schema: Query History UPDATE performance_schema.setup_consumers SET enabled='YES' WHERE name = 'events_statements_history'; mysql> SELECT sql_text, (timer_wait)/1000000000.0 "t (ms)", rows_examined rows FROM performance_schema.events_statements_history ORDER BY timer_start; +---------------------------------------------------------------+--------+------+ | sql_text | t (ms) | rows | +---------------------------------------------------------------+--------+------+ | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 8.1690 | 1505 | | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 7.2120 | 1505 | | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 8.1613 | 1505 | | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 7.0535 | 1505 | | CREATE INDEX i_o_clerk_date ON orders(o_clerk,o_orderdate) |82036.4190 | 0 | | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.7259 | 15 | | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.5791 | 15 | | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.5423 | 15 | | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.6031 | 15 | | SELECT * FROM orders WHERE o_orderdate BETWEEN '1997-05-01' … | 0.2710 | 15 | +---------------------------------------------------------------+--------+------+ MySQL 5.7: Enabled by default
  • 39. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 39 Program Agenda Cost-based query optimization in MySQL Tools for monitoring, analyzing, and tuning queries Data access and index selection Join optimizer Sorting Influencing the optimizer 1 2 3 4 5 6
  • 40. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 40 Join Optimizer • Goal: Given a JOIN of N tables, find the best JOIN ordering • Strategy: – Start with all 1-table plans (Sorted based on size and key dependency) – Expand each plan with remaining tables • Depth-first – If “cost of partial plan” > “cost of best plan”: • “prune” plan – Heuristic pruning: • Prune less promising partial plans • May in rare cases miss most optimal plan (turn off with set optimizer_prune_level = 0) ”Greedy search strategy” t1 t2 t2 t2 t2 t3 t3 t3 t4t4 t4 t4t4 t3 t3 t2 t4t2 t3 N! possible plans
  • 41. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 41 JOIN Optimizer Illustrated SELECT City.Name, Language FROM Language, Country, City WHERE City.CountryCode = Country.Code AND City.ID = Country.Capital AND City.Population >= 1000000 AND Language.Country = Country.Code; Language Country City LanguageCountry Country City CityCity City cost=26568 cost=32568 cost=627 cost=1245 cost=862 start
  • 42. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 42 Join Optimizer: Case study SELECT o_year, SUM(CASE WHEN nation = 'FRANCE' THEN volume ELSE 0 END) / SUM(volume) AS mkt_share FROM ( SELECT EXTRACT(YEAR FROM o_orderdate) AS o_year, l_extendedprice * (1 - l_discount) AS volume, n2.n_name AS nation FROM part JOIN lineitem ON p_partkey = l_partkey JOIN supplier ON s_suppkey = l_suppkey JOIN orders ON l_orderkey = o_orderkey JOIN customer ON o_custkey = c_custkey JOIN nation n1 ON c_nationkey = n1.n_nationkey JOIN region ON n1.n_regionkey = r_regionkey JOIN nation n2 ON s_nationkey = n2.n_nationkey WHERE r_name = 'EUROPE' AND o_orderdate BETWEEN '1995-01-01' AND '1996-12-31' AND p_type = 'PROMO BRUSHED STEEL' ) AS all_nations GROUP BY o_year ORDER BY o_year; DBT-3 Query 8: National Market Share Query
  • 43. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 43 Join Optimizer: Case Study MySQL Workbench: Visual EXPLAIN Execution time: 21 seconds
  • 44. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 44 Join Optimizer: Case study SELECT o_year, SUM(CASE WHEN nation = 'FRANCE' THEN volume ELSE 0 END) / SUM(volume) AS mkt_share FROM ( SELECT EXTRACT(YEAR FROM o_orderdate) AS o_year, l_extendedprice * (1 - l_discount) AS volume, n2.n_name AS nation FROM part STRAIGHT_JOIN lineitem ON p_partkey = l_partkey JOIN supplier ON s_suppkey = l_suppkey JOIN orders ON l_orderkey = o_orderkey JOIN customer ON o_custkey = c_custkey JOIN nation n1 ON c_nationkey = n1.n_nationkey JOIN region ON n1.n_regionkey = r_regionkey JOIN nation n2 ON s_nationkey = n2.n_nationkey WHERE r_name = 'EUROPE' AND o_orderdate BETWEEN '1995-01-01' AND '1996-12-31' AND p_type = 'PROMO BRUSHED STEEL' ) AS all_nations GROUP BY o_year ORDER BY o_year; Force early processing of high selectivity predicates Highest selectivity part before lineitem
  • 45. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 45 Join Optimizer: Case study Improved join order Execution time: 3 seconds
  • 46. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 46 MySQL 5.7: Cost Information in Structured EXPLAIN Accumulated cost Total query cost Cost per table Improvements to Query 8 in MySQL 5.7: • Filtering on non-indexed columns are taken into account – No need for hint to force part table to be processed early • Merge derived tables into outer query – No temporary table
  • 47. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 47 Program Agenda Cost-based query optimization in MySQL Tools for monitoring, analyzing, and tuning queries Data access and index selection Join optimizer Sorting Influencing the optimizer 1 2 3 4 5 6
  • 48. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 48 ORDER BY Optimizations • General solution; “Filesort”: – Store query result in temporary table before sorting – If data volume is large, may need to sort in several passes with intermediate storage on disk. • Optimizations: – Take advantage of index to generate query result in sorted order – For ”LIMIT n” queries, maintain priority queue of n top items in memory instead of filesort. (MySQL 5.6)
  • 49. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 49 Filesort SELECT * FROM orders ORDER BY o_totalprice ; SELECT c_name, o_orderkey, o_totalprice FROM orders JOIN customer ON c_custkey = o_custkey WHERE c_acctbal < -1000 ORDER BY o_totalprice ; id select type table type possible keys key key len ref rows extra 1 SIMPLE customer ALL PRIMARY NULL NULL NULL 1500000 Using where; Using temporary; Using filesort 1 SIMPLE orders ref i_o_custkey i_o_custkey 5 ... 7 NULL id select type table type possible keys key key len ref rows extra 1 SIMPLE orders ALL NULL NULL NULL NULL 15000000 Using filesort
  • 50. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 50 Filesort Status variables related to sorting: mysql> show status like 'Sort%'; +-------------------+--------+ | Variable_name | Value | +-------------------+--------+ | Sort_merge_passes | 1 | | Sort_range | 0 | | Sort_rows | 136170 | | Sort_scan | 1 | +-------------------+--------+ Status variables >0: Intermediate storage on disk. Consider increasing sort_buffer_size Number of sort operations (range scan or table/index scans) Number of rows sorted
  • 51. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 51 Filesort Sorting status per statement available from Performance Schema mysql> SELECT sql_text,sort_merge_passes,sort_range,sort_rows,sort_scan FROM performance_schema.events_statements_history ORDER BY timer_start DESC LIMIT 1; +--------------+-------------------+------------+-----------+-----------+ | sql_text | sort_merge_passes | sort_range | sort_rows | sort_scan | +--------------+-------------------+------------+-----------+-----------+ | SELECT ... | 1 | 0 | 136170 | 1 | +--------------+-------------------+------------+-----------+-----------+ Performance Schema
  • 52. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 52 mysql> FLUSH STATUS; Query OK, 0 rows affected (0.00 sec) mysql> SELECT AVG(o_totalprice) FROM ( SELECT * FROM orders ORDER BY o_totalprice DESC LIMIT 100000) td; +-------------------+ | AVG(o_totalprice) | +-------------------+ | 398185.986158 | +-------------------+ 1 row in set (24.65 sec) mysql> SHOW STATUS LIKE 'sort%'; +-------------------+--------+ | Variable_name | Value | +-------------------+--------+ | Sort_merge_passes | 1432 | | Sort_range | 0 | | Sort_rows | 100000 | | Sort_scan | 1 | +-------------------+--------+ 4 rows in set (0.00 sec) Filesort: Case Study Unnecessary large data volume! Many intermediate sorting steps!
  • 53. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 53 Filesort: Case Study mysql> SELECT AVG(o_totalprice) FROM (SELECT o_totalprice FROM orders ORDER BY o_totalprice DESC LIMIT 100000) td; +-------------------+ | AVG(o_totalprice) | +-------------------+ | 398185.986158 | +-------------------+ 1 row in set (8.18 sec) mysql> SELECT sql_text, sort_merge_passes FROM performance_schema. events_statements_history ORDER BY timer_start DESC LIMIT 1; +----------------------------------------------------+-------------------+ | sql_text | sort_merge_passes | +----------------------------------------------------+-------------------+ | SELECT AVG(o_totalprice) FROM (SELECT o_totalprice | 229 | +----------------------------------------------------+-------------------+ Reduce amount of data to be sorted
  • 54. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 54 Filesort: Case Study mysql> SET sort_buffer_size = 1024*1024; mysql> SELECT AVG(o_totalprice) FROM (SELECT o_totalprice FROM orders ORDER BY o_totalprice DESC LIMIT 100000) td; +-------------------+ | AVG(o_totalprice) | +-------------------+ | 398185.986158 | +-------------------+ 1 row in set (7.24 sec) mysql> SELECT sql_text, sort_merge_passes FROM performance_schema. events_statements_history ORDER BY timer_start DESC LIMIT 1; +----------------------------------------------------+-------------------+ | sql_text | sort_merge_passes | +----------------------------------------------------+-------------------+ | SELECT AVG(o_totalprice) FROM (SELECT o_totalprice | 57 | +----------------------------------------------------+-------------------+ Increase sort buffer (1 MB) Default is 256 kB
  • 55. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 55 Filesort: Case Study mysql> SET sort_buffer_size = 8*1024*1024; mysql> SELECT AVG(o_totalprice) FROM (SELECT o_totalprice FROM orders ORDER BY o_totalprice DESC LIMIT 100000) td; +-------------------+ | AVG(o_totalprice) | +-------------------+ | 398185.986158 | +-------------------+ 1 row in set (6.30 sec) mysql> SELECT sql_text, sort_merge_passes FROM performance_schema. events_statements_history ORDER BY timer_start DESC LIMIT 1; +----------------------------------------------------+-------------------+ | sql_text | sort_merge_passes | +----------------------------------------------------+-------------------+ | SELECT AVG(o_totalprice) FROM (SELECT o_totalprice | 0 | +----------------------------------------------------+-------------------+ Increase sort buffer even more (8 MB)
  • 56. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 56 Using Index to Avoid Sorting CREATE INDEX i_o_totalprice ON orders(o_totalprice); SELECT o_orderkey, o_totalprice FROM orders ORDER BY o_totalprice ; id select type table type possible keys key key len ref rows extra 1 SIMPLE orders index NULL i_o_totalprice 6 NULL 15000000 Using index SELECT * FROM orders ORDER BY o_totalprice ; However, still (due to total cost): id select type table type possible keys key key len ref rows extra 1 SIMPLE orders ALL NULL NULL NULL NULL 15000000 Using filesort
  • 57. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 57 Using Index to Avoid Sorting SELECT AVG(o_totalprice) FROM (SELECT o_totalprice FROM orders ORDER BY o_totalprice DESC LIMIT 100000) td; Case study revisited id select type table Type possible keys key key len ref rows extra 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 100000 NULL 2 DERIVED orders index NULL i_o_totalprice 6 NULL 15000000 Using index mysql> SELECT AVG(o_totalprice) FROM ( SELECT o_totalprice FROM orders ORDER BY o_totalprice DESC LIMIT 100000) td; ... 1 row in set (0.06 sec)
  • 58. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 58 Program Agenda Cost-based query optimization in MySQL Tools for monitoring, analyzing, and tuning queries Data access and index selection Join optimizer Sorting Influencing the optimizer 1 2 3 4 5 6
  • 59. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 59 Influencing the Optimizer • Add indexes • Force use of specific indexes: – USE INDEX, FORCE INDEX, IGNORE INDEX • Force specific join order: – STRAIGHT_JOIN • Adjust session variables – optimizer_switch flags: set optimizer_switch="index_merge=off" – Buffer sizes: set sort_buffer=8*1024*1024; – Other variables: set optimizer_search_depth = 10; When the optimizer does not do what you want
  • 60. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 60 MySQL 5.7: New Optimizer Hints • Ny hint syntax: – SELECT /*+ HINT1(args) HINT2(args) */ … FROM … • New hints: – BKA(tables)/NO_BKA(tables), BNL(tables)/NO_BNL(tables) – MRR(table indexes)/NO_MRR(table indexes) – SEMIJOIN/NO_SEMIJOIN(strategies), SUBQUERY(strategy) – NO_ICP(table indexes) – NO_RANGE_OPTIMIZATION(table indexes) – QB_NAME(name) • Finer granularilty than optimizer_switch session variable
  • 61. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 61 • Disable semi-join with hint: EXPLAIN SELECT * FROM t2 WHERE t2.a IN (SELECT /*+ NO_SEMIJOIN() */ a FROM t3); • No hint, optimizer chooses semi-join algorithm LooseScan: EXPLAIN SELECT * FROM t2 WHERE t2.a IN (SELECT a FROM t3); MySQL 5.7: Hint Example: SEMIJOIN id select type table type possible keys key key len ref rows extra 1 SIMPLE t3 index a a 4 NULL 3 Using where; LooseScan 1 SIMPLE t2 ref a a 4 test.t3.a 1 Using index id select type table type possible keys key key len ref rows extra 1 PRIMARY t2 index null a 4 NULL 4 Using where; Using index 2 DEPENDENT SUBQUERY t3 Index_ subquery a a 4 func 1 Using index
  • 62. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 62 MySQL 5.7: Hint Example: SEMIJOIN • Force Semi-join Materialization to be used EXPLAIN SELECT /*+ SEMIJOIN(@subq MATERIALIZATION) */ * FROM t2 WHERE t2.a IN (SELECT /*+ QB_NAME(subq) */ a FROM t3); 3 rows in set, 1 warning (0.01 sec) id select type table type possible keys key key len ref rows extra 1 SIMPLE t2 index a a 4 NULL 4 Using where; Using index 1 SIMPLE <subquery2> eq_ref <auto_key> <auto_key> 4 test.t2.a 1 NULL 2 MATERIALIZED t3 index a a 4 NULL 3 Using index
  • 63. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 63 MySQL 5.7: Query Rewrite Plugin • Rewrite problematic queries without the need to make application changes – Add hints – Modify join order – Much more … • Add rewrite rules to table: INSERT INTO query_rewrite.rewrite_rules (pattern, replacement ) VALUES ("SELECT * FROM t1 WHERE a > ? AND b = ?", "SELECT * FROM t1 FORCE INDEX (a_idx) WHERE a > ? AND b = ?"); • New pre- and post-parse query rewrite APIs – Users can write their own plug-ins
  • 64. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 64 More information • My blog: – http://oysteing.blogspot.com/ • Optimizer team blog: – http://mysqloptimizerteam.blogspot.com/ • MySQL Server Team blog – http://mysqlserverteam.com/ • MySQL forums: – Optimizer & Parser: http://forums.mysql.com/list.php?115 – Performance: http://forums.mysql.com/list.php?24
  • 65. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Q+A
  • 66. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 66
  • 67. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 6767
  • 68. 68