SlideShare uma empresa Scribd logo
1 de 58
Baixar para ler offline
Copyright © 2018 Oracle and/or its afliates. All rights reserved.
How to Take Advantage of Optiiier
Iiproveients in MySQL 8.0
Norvald H. Ryeng
Sofware Developient Senior Manager
MySQL Optiiier Teai
October, 2018
3Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Safe Harbor Stateient
The following is intended to outline our general product directon. It is intended for
inforiaton purposes only, and iay not be incorporated into any contract. It is not a
coiiitient to deliver any iaterial, code, or functonality, and should not be relied upon
in iaking purchasing decisions. The developient, release, and tiing of any features or
functonality described for Oracle’s products reiains at the sole discreton of Oracle.
4Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Prograi Agenda
Cost iodel iiproveients
Histograis
Optiiier hints
Iiproved perforiance with CTEs
Iiproveients in query executon
1
2
3
4
5
6
7
6Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Cost Model Iiproveients
7Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Optiiier Cost Model
t1 Cost estiate
Row estiate
Cost Model
Cost foriulas
Access
iethods
Join Subquery
Cost constants
CPU IO
Metadata:
- Row and index siie
- Index inforiaton
- Uniqueness
Statstcs:
- Table siie
- Cardinality
- Range estiates
Cost iodel
configuraton
Range
scan
JOIN
New in
MySQL 5.7
8Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Motvaton for Iiproving the MySQL Cost Model
● Produce iore correct cost estiates
– Beter decisions by the optiiier should iiprove perforiance
● Adapt to new hardware architectures
– SSD, larger ieiories, caches
● More iaintainable cost iodel iipleientaton
– Avoid hard coded “cost constants”
– Refactoring of existng cost iodel code
● Configurable and tunable
● Make iore of the optiiier cost-based
Faster
queries
9Copyright © 2018 Oracle and/or its afliates. All rights reserved.
New Storage Technologies
● Tiie to do a table scan of 10 iillion
records:
● Adjust cost iodel to support
diferent storage technologies
● Provide configurable cost constants
for diferent storage technologies
Meiory 5 s
SSD 20 - 146 s
Hard disk 32 - 1465 s
Provide a prograi that
could ieasure perforiance
and suggest good cost
constant configuraton for a
running MySQL server?
Provide a prograi that
could ieasure perforiance
and suggest good cost
constant configuraton for a
running MySQL server?
10Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Meiory Bufer Aware Cost Estiates
● Storage engines:
– Estiate for how iuch of data and
indexes are in a ieiory bufer
– Estiate for hit rate for ieiory
bufer
● Optiiier cost iodel:
– Take into account whether data is
already in ieiory or need to be read
froi disk
Server
Storage
engine
Disk data
Query
executor
Database
bufer
11Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Disk vs. Meiory Access
● New defaults for const constants:
● InnoDB reports percentage of data cached in bufer pool for each
table/index
● Note: Query plan iay change between executons
Cost MySQL 5.7 MySQL 8.0
Read a randoi disk page 1.0 1.0
Read a data page froi ieiory bufer 1.0 0.25
Evaluate query conditon 0.2 0.1
Coipare keys/rows 0.1 0.05
Saie cost constant
12Copyright © 2018 Oracle and/or its afliates. All rights reserved.
DBT-3 Query 8
Natoonal market share
SELECT o_year, SUM(CASE WHEN naton = '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 naton
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 naton n1 ON c_natonkey = n1.n_natonkey
JOIN region ON n1.n_regionkey = r_regionkey
JOIN naton n2 ON s_natonkey = n2.n_natonkey
WHERE r_name = 'EUROPE' AND o_orderdate BETWEEN '1995-01-01' AND '1996-12-31'
AND p_type = 'PROMO BRUSHED STEEL'
) AS all_natons GROUP BY o_year ORDER BY o_year; High selectvity predicate
13Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Plan A
Plan B
DBT-3 Query 8
Alteronatve query plaons
14Copyright © 2018 Oracle and/or its afliates. All rights reserved.
DBT-3 Query 8
Executoon tme (8.0.3)
Ion-memory Disk-bouond
Plaon A 5.8 secs 9 iin 47 secs
Plaon B 77.5 secs 3 mion 49 secs
Ion-memory Disk-bouond
MySQL 5.6 Plan B
MySQL 5.7 Plan A
MySQL 8.0 Plan A Plan B
Selected plaon
15Copyright © 2018 Oracle and/or its afliates. All rights reserved.
How to Configure your Own Cost Constants
UPDATE mysql.engine_cost SET cost_value=0.1
WHERE cost_name='memory_block_read_cost';
FLUSH OPTIMIZER_COSTS;
Note:
● Only new connectons will see updated cost constants
Future ideas:
● Create tool to tune cost constants to specific hardware
● Use iachine learning to find optial values for specific user load
Default 0.25
Make optiiier read new cost constants
16Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Histograis
17Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Histograis
● Provides the optiiier with inforiaton about coluin value distributon
● To create/recalculate histograi for a coluin:
ANALYZE TABLE table UPDATE HISTOGRAM ON column WITH n BUCKETS;
● May use saipling
– Saiple siie is based on available ieiory (histograi_generaton_iax_iei_siie)
● Autoiatcally chooses between two histograi types:
– Singleton: One value per bucket
– Equi-height: Multple values per bucket
– Max 1024 buckets
18Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Histograis Exaiple
EXPLAIN SELECT *
FROM customer JOIN orders ON c_custkey = o_custkey
WHERE c_acctbal < -1000 AND o_orderdate < '1993-01-01';
id
select
type
table type possible keys key key leon ref rows fltered extra
1 SIMPLE orders ALL
i_o_orderdate,
i_o_custkey
NULL NULL NULL 15000000 31.19
Using
where
1 SIMPLE custoier eq_ref PRIMARY PRIMARY 4
dbt3.orders.
o_custkey
1 33.33
Using
where
19Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Histograis Exaiple
ANALYZE TABLE customer UPDATE HISTOGRAM ON c_acctbal WITH 1024 BUCKETS;
EXPLAIN SELECT *
FROM customer JOIN orders ON c_custkey = o_custkey
WHERE c_acctbal < -1000 AND o_orderdate < '1993-01-01';
id
select
type
table type possible keys key key leon ref rows fltered extra
1 SIMPLE custoier ALL PRIMARY NULL NULL NULL 1500000 0.00
Using
where
1 SIMPLE orders ref
i_o_orderdate,
i_o_custkey
i_o_custkey 5
dbt3.
custoier.
c_custkey
1 31.19
Using
where
20Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Histograis Exaiple
0
2
4
6
8
10
12
14
16
orders → custoier custoier → orders
QueryExecutoonTime(secoonds)
21Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Singleton Histograi
● One value per bucket
● Each bucket stores:
– Value
– Cuiulatve frequency
● Well suited to estiate both
equality and range predicates
0 1 2 3 5 6 7 8 9 10
0
0.05
0.1
0.15
0.2
0.25
Frequeoncy
22Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Equi-Height Histograi
● Multple values per bucket
● Not quite equi-height
– Values are not split across buckets
– Frequent values in separate buckets
● Each bucket stores:
– Miniiui value
– Maxiiui value
– Cuiulatve frequency
– Nuiber of distnct values
● Best suited for range predicates
0 - 0 1 - 1 2 - 3 5 - 6 7 - 10
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
Frequeoncy
23Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Usage
● Create or refresh histograi(s) for coluin(s):
ANALYZE TABLE table UPDATE HISTOGRAM ON column [, column] WITH n BUCKETS;
– Note: Will only update histograi, not other statstcs
● Drop histograi:
ANALYZE TABLE table DROP HISTOGRAM ON column [, column];
● Based on entre table or saipling:
– Depends on available ieiory: histogram_geoneratoon_max_mem_size (default: 20 MB)
● New storage engine API for saipling
– Default iipleientaton: Full table scan even when saipling
– Storage engines iay iipleient iore efcient saipling
24Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Storage
● Stored in a JSON coluin in data dictonary
● Can be inspected in Inforiaton Scheia view
SELECT JSON_PRETTY(histogram)
FROM informaton_schema.column_statstcs
WHERE schema_name = 'dbt3_sf1'
AND table_name ='lineitei'
AND column_name = 'l_linenuiber';
25Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Histograi Content
{
"buckets": [[1, 0.24994938524948698], [2, 0.46421066400720523],
[3, 0.6427401784471978], [4, 0.7855470933802572],
[5, 0.8927398868395817], [6, 0.96423707532558], [7, 1] ],
"data-type": "int",
"null-values": 0.0,
"collaton-id": 8,
"last-updated": "2018-02-03 21:05:21.690872",
"saipling-rate": 0.20829115437457252,
"histograi-type": "singleton",
"nuiber-of-buckets-specified": 1024
}
27Copyright © 2018 Oracle and/or its afliates. All rights reserved.
When to Create Histograis
● Histograis are useful for coluins that are
– Not the first coluin of any index, and
– Used in WHERE conditons of
● JOIN queries
● Queries with IN subqueries
● ORDER BY … LIMIT queries
● Best fit
– Low cardinality coluins (e.g., gender, orderStatus, dayOfWeek, enuis)
– Coluins with uneven distributon (skew)
– Stable distributon over tie
28Copyright © 2018 Oracle and/or its afliates. All rights reserved.
When Not to Create Histograis
● First coluin of an index
● Coluin that is never used in WHERE clauses
● Monotonically increasing coluin values (e.g., tiestaips)
– Histograis will need frequent updates
– Consider creatng and index instead
29Copyright © 2018 Oracle and/or its afliates. All rights reserved.
How Many Buckets?
● If possible, enough to create a singleton histograi
● Equi-height: 100 buckets should be enough
– 1 % resoluton
30Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Optiiier Hints
31Copyright © 2018 Oracle and/or its afliates. All rights reserved.
New Optiiier Hints in 8.0
● Join order
– JOIN_ORDER(tables)
– JOIN_PREFIX(tables)
– JOIN_SUFFIX(tables)
– JOIN_FIXED_ORDER()
● Setng systei variables
– SET_VAR(sysvar=value)
● View/derived table/CTE ierge
– MERGE(views)
– NO_MERGE(views)
● Index ierge
– INDEX_MERGE(indexes)
– NO_INDEX_MERGE(indexes)
32Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Join Order Hints
● No need to reorganiie the FROM clause to add join order
hints like you will for STRAIGHT_JOIN
● /*+ JOIN_ORDER(t1, t2, …) */
● /*+ JOIN_PREFIX(t1, t2, …) */
● /*+ JOIN_SUFFIX(…, ty, tz) */
● /*+ JOIN_FIXED_ORDER() */
– Replaceient for STRAIGHT_JOIN
33Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Join Order Hint Exaiple
EXPLAIN SELECT /*+ JOIN_ORDER(customer, orders) */ *
FROM customer JOIN orders ON c_custkey = o_custkey
WHERE c_acctbal < -1000 AND o_orderdate < '1993-01-01';
Alternatves with saie efect for this query:
JOIN_PREFIX(customer) JOIN_SUFFIX(orders) JOIN_FIXED_ORDER()
id
select
type
table type possible keys key
key
leon
ref rows fltered extra
1 SIMPLE custoier ALL PRIMARY NULL NULL NULL 1500000 33.33
Using
where
1 SIMPLE orders ref
i_o_orderdate,
i_o_custkey
i_o_custkey 5
dbt3.
custoier.
c_custkey
15 31.19
Using
where
35Copyright © 2018 Oracle and/or its afliates. All rights reserved.
View/Derived Table/CTE Merge Hints
● Views, derived tables and CTEs are, if possible, ierged into outer
query
● NO_MERGE can override default behavior
SELECT /*+ NO_MERGE(dt) */ *
FROM t1 JOIN (SELECT x, y FROM t2) dt ON t1.x = dt.x;
● MERGE will force a ierge
SELECT /*+ MERGE(dt) */ *
FROM t1 JOIN (SELECT x, y FROM t2) dt ON t1.x = dt.x;
● Can also use MERGE/NO_MERGE hints for views and CTEs
SELECT /*+ NO_MERGE(v) */ * FROM t1 JOIN v ON t1.x = v.x;
36Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Index Merge Exaiple
10INDEX(a)
10INDEX(b)
a=10 AND b=10Result:
Intersecton
SELECT * FROM t1 WHERE a=10 AND b=10
37Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Index Merge Hints
● Index ierge: Merge rows froi iultple range scans on a
single table
● Algorithis: union, intersecton, sort union
● Users can specify which indexes to use for index ierge
– /*+ INDEX_MERGE(t1 idx1, idx2, …) */
– /*+ NO_INDEX_MERGE(t1 idx1, idx2, …) */
38Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Index Merge Hint Exaiple
EXPLAIN SELECT count(*) FROM users
WHERE user_type=2 AND status=0 AND pareont_id=0;
mysql> SELECT count(*) FROM users WHERE user_type=2 AND status=0 AND
parent_id=0;
...
1 row in set (1.37 sec)
mysql> SELECT /*+ INDEX_MERGE(users user_type, status) */ count(*)
-> FROM users WHERE user_type=2 AND status=0 AND parent_id=0;
...
1 row in set (0.18 sec)
id
select
type
table type possible keys key
key
leon
ref rows Extra
1 SIMPLE users
index_
ierge
parent_id,
status,
user_type
user_type,
status,
parent_id
1,1,4 NULL 2511
Using intersect (user_type, status,
parent_id);
Using where; Using index
Low selectvity predicate
39Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Hints to Set Session Variables
● Set a session variable for the duraton of a single stateient
● Exaiples:
SELECT /*+ SET_VAR(sort_bufer_size = 16M) */ naie FROM people ORDER BY naie;
INSERT /*+ SET_VAR(foreigon_key_checks = OFF) */ INTO t2 VALUES (1, 1), (2, 2), (3, 3);
SELECT /*+ SET_VAR(optmizer_switch = 'coonditoon_faonout_flter = of') */ *
FROM custoier JOIN orders ON c_custkey = o_custkey
WHERE c_acctbal < 0 AND o_orderdate < '1993-01-01';
● Note: Not all session variables are setable through hints:
iysql> SELECT /*+ SET_VAR(max_allowed_packet=128M) */ * FROM t1;
Eipty set, 1 warning (0,00 sec)
Warning (Code 4537): Variable 'iax_allowed_packet' cannot be set using SET_VAR hint.
40Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Skip Index Dives for FORCE INDEX Hint
● Estiate nuiber of rows in a range
– The optiiier asks the storage engine for nuiber of rows to be scanned froi an index
– Index dives: InnoDB finds first and last row in range and estiates distance
– Exaiple:
SELECT * FROM t1 WHERE (key1 > 10 AND key1 < 20) AND key2 > 30
Indexes on key1 and key2 will be used to get estiates for the two ranges
● MySQL 8.0 will skip index dives when all of these are satsfied:
– FORCE INDEX applies to a single index
– Only a single table is accessed in the query
– No subqueries
– No GROUP BY, DISTINCT or ORDER BY
41Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Skip Index Dives Perforiance
● Sysbench scheia
– 8 tables
– 1 iillion rows per table
– Secondary index on integer coluin k
– k values are randoily and uniforily
distributed 1–1000000
● Queries
SELECT c FROM sbtest WHERE k = ?
SELECT c FROM sbtest FORCE INDEX(k_1)
WHERE k = ?
● 10–13 % iiproveient
0 50 100 150 200 250 300
0
200000
400000
600000
800000
1000000
Default FORCE INDEX
Threads
Queriespersecoond
42Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Invisible Index
● Index is iaintained by the storage engine, but invisible to the
optiiier
– Override with SET optiiier_switch='use_invisible_indexes=on';
● Priiary key can't be invisible
● Use case: Check for perforiance drope before reioving index
ALTER TABLE t1 ALTER INDEX idx INVISIBLE;
SHOW INDEXES FROM t1;
+---------+------------------+----------------------+---------------+
| Table | Key_naie | Coluin_naie | Visible |
+---------+------------------+----------------------+---------------+
| t1 | idx | a | NO |
+---------+------------------+----------------------+---------------+
43Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Iiproved Perforiance with CTEs
44Copyright © 2018 Oracle and/or its afliates. All rights reserved.
CTEs Instead of Derived Tables
● A derived table is a subquery in the FROM clause:
SELECT … FROM (subquery) AS derived, t1 …
● A CTE is just like a derived table, but the declaraton coies
before the query block:
WITH derived AS (subquery)
SELECT … FROM derived, t1 …
● May precede SELECT, UPDATE, DELETE and be used in
subqueries:
WITH derived AS (subquery)
DELETE FROM t1 WHERE t1.a IN (SELECT b FROM derived);
45Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Beter Perforiance with CTEs
● Derived tables can't be referenced twice
SELECT …
FROM (SELECT a, b, SUM(c) AS s FROM t1 GROUP BY a, b) AS d1
JOIN (SELECT a, b, SUM(c) AS s FROM t1 GROUP BY a, b) AS d2 ON d1.b = d2.a;
● CTEs can
WITH d AS (SELECT a, b, SUM(c) AS s FROM t1 GROUP BY a, b)
SELECT … FROM d AS d1 JOIN d AS d2 ON d1.b = d2.a;
● Beter perforiance with iaterialiiaton
– Multple CTE references are only iaterialiied once
– Derived tables and views will be iaterialiied once per reference
46Copyright © 2018 Oracle and/or its afliates. All rights reserved.
DBT-3 Query 15 Top Supplier
Using view
CREATE VIEW revenue0 (supplier_no, total_revenue) AS
(
SELECT l_suppkey,
SUM(l_extendedprice * (1- l_discount))
FROM lineitem
WHERE l_shipdate >= '1996-07-01'
AND l_shipdate < DATE_ADD('1996-07-01',
INTERVAL '90' DAY)
GROUP BY l_suppkey
);
Using CTE
WITH revenue0 (supplier_no, total_revenue) AS
(
SELECT l_suppkey,
SUM(l_extendedprice * (1-l_discount))
FROM lineitem
WHERE l_shipdate >= '1996-07-01'
AND l_shipdate < DATE_ADD('1996-07-01',
INTERVAL '90' DAY)
GROUP BY l_suppkey
)
SELECT s_suppkey, s_name, s_address, s_phone, total_revenue
FROM supplier, revenue0
WHERE s_suppkey = supplier_no AND total_revenue = (SELECT MAX(total_revenue) FROM revenue0)
ORDER BY s_suppkey;
47Copyright © 2018 Oracle and/or its afliates. All rights reserved.
DBT-3 Query 15 with View Materialiiaton
ORDER JOIN
Supplier
(eq_ref)
GROUP Lineitei
(range)
Materialiie
GROUP Lineitei
(range)
Materialiie
SELECT
Subquery
Revenue0
Revenue0
WHERE
FROM
48Copyright © 2018 Oracle and/or its afliates. All rights reserved.
DBT-3 Query 15 with CTE Materialiiaton
ORDER JOIN
Supplier
(eq_ref)
GROUP Lineitei
(range)
Materialiie
SELECT
Revenue0
WHERE
FROM
Subquery
49Copyright © 2018 Oracle and/or its afliates. All rights reserved.
DBT-3 Query 15 Perforiance
View CTE
0
2
4
6
8
10
12
14
16
18
Queryexecutontie(seconds)
View CTE
50Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Iiproveients in Query Executon
51Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Index Skip-Scan — New ion 8.0.13
● Used for iult-coluin index
● Conditon not on first part of index
● Cost-based choice
Before MySQL 8.0.13:
Full index scan
MySQL 8.0.13:
Skip-scan
pk a b c
INDEX idx(a,b,c)
SELECT * FROM t1 WHERE b < 3;
1
1 2 3 4 5
2
1 2 3 4 5
3
1 2 3 4 5
4
1 2 3 4 5
a
b
c
Full index scan
1
1 2 3 4 5
2
1 2 3 4 5
3
1 2 3 4 5
4
1 2 3 4 5
a
b
c
b < 3 b < 3 b < 3 b < 3
52Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Index Skip-Scan Perforiance Exaiple
● DBT-3 scale factor 10 database
● Create index on lineitem
– (l_shipdate, l_quantty, l_shipmode)
– 60 iillion rows
– Cardinality:
● l_shipdate: 2526
● l_quantty: 50
● Query:
SELECT l_shipdate, l_quantty FROM lineitem
WHERE l_quantty > ? AND l_shipmode = 'XXX';
● Skip-scan is faster when less than 80 % of the rows are accessed!
●
0 10 20 30 40 50 60
0
5
10
15
20
25
Index Scan Skip Scan
l_quaontty > ?
53Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Thaonks for the patch,
Facebook!
54Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Iiproved Scan Perforiance
● Motvaton
– InnoDB already uses an internal bufer to fetch records in batches
– The optiiier knows the operaton and estiated nuiber of records to read
● The optiiier knows how large the bufer should be
● 8.0 extends the handler API
– Provide a bufer to the storage engine when expectng iore than one row
● Exaiple queries with iiproved perforiance:
SELECT * FROM t;
SELECT * FROM t WHERE pk BETWEEN 1000 AND 10000;
SELECt * FROM t1, t2 WHERE t1.pk=t2.pk; /* Bufer for the outer table */
55Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Iiproved Scan Perforiance
● 18 out of 22 queries got iiproved
perforiance
● 4 queries show iore than 10 %
iiproveient
● 2 queries show iore than 15 %
iiproveient
56Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Internal Teiporary Table Storage Engine
● Variable-length rows
● Supports JSON, TEXT, BLOB and geoietry types
– New ion MySQL 8.0.13!
● Supports overfow to disk
– No overhead for convertng to InnoDB/MyISAM when table becoies big
● Coiion ieiory pool for all internal teiporary tables
– No fixed liiit per table
– teiptable_iax_rai (default 1 GB)
● Perforiance scheia events for ieiory/disk usage
– ieiory/teiptable/physical_rai, ieiory/teiptable/physical_disk
57Copyright © 2018 Oracle and/or its afliates. All rights reserved.
DBT-3 with TeipTable vs. MEMORY
● MySQL 8.0.13
● DBT-3, Scale factor 10
● innodb_bufer_pool_siie = 32G (CPU-
bound)
● internal_tip_iei_storage_engine =
"TeipTable" or "MEMORY"
● Other 20 queries have saie perforiance
for both engines
Q10 Q11
0%
20%
40%
60%
80%
100%
Query Executoon Time Relatve to MEMORY
MEMORY TeipTable
58Copyright © 2018 Oracle and/or its afliates. All rights reserved.
JSON Data in TeipTable
● MySQL 8.0.13
● JSON variant of sysbench tests
– Range siie: 500
– Data siie: 127 GB
● 64 threads
htp://iysqlserverteai.coi/iysql-8-0-support-for-blobs-in-teiptable-engine
0%
300%
600%
900%
Traonsactoon Througput Relatve to IononoDB
InnoDB TeipTable
59Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Descending Index
CREATE TABLE t1 (
a INT,
b INT,
INDEX a_b (a DESC, b ASC)
);
● In 5.7: Index in ascending order is created, server scans it backwards
● In 8.0: Index in descending order is created, server scans it forwards
– Works on B-tree indexes only
– Benefits:
● Use indexes instead of filesort for ORDER BY clause with ASC/DESC sort key
● Forward index scan is slightly faster than backward index scan
60Copyright © 2018 Oracle and/or its afliates. All rights reserved.
Feature descriptons and design details
directly froi the source.
htp://mysqlserverteam.com/
61Copyright © 2018 Oracle and/or its afliates. All rights reserved.
How to Take Advantage of Optimizer Improvements in MySQL 8.0

Mais conteúdo relacionado

Mais procurados

MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
SQL Analytics Powering Telemetry Analysis at Comcast
SQL Analytics Powering Telemetry Analysis at ComcastSQL Analytics Powering Telemetry Analysis at Comcast
SQL Analytics Powering Telemetry Analysis at Comcast
Databricks
 

Mais procurados (20)

All About JSON and ClickHouse - Tips, Tricks and New Features-2022-07-26-FINA...
All About JSON and ClickHouse - Tips, Tricks and New Features-2022-07-26-FINA...All About JSON and ClickHouse - Tips, Tricks and New Features-2022-07-26-FINA...
All About JSON and ClickHouse - Tips, Tricks and New Features-2022-07-26-FINA...
 
Altinity Quickstart for ClickHouse
Altinity Quickstart for ClickHouseAltinity Quickstart for ClickHouse
Altinity Quickstart for ClickHouse
 
Postgresql database administration volume 1
Postgresql database administration volume 1Postgresql database administration volume 1
Postgresql database administration volume 1
 
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZEMySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
 
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
 
Deep Dive on Amazon Aurora with PostgreSQL Compatibility (DAT305-R1) - AWS re...
Deep Dive on Amazon Aurora with PostgreSQL Compatibility (DAT305-R1) - AWS re...Deep Dive on Amazon Aurora with PostgreSQL Compatibility (DAT305-R1) - AWS re...
Deep Dive on Amazon Aurora with PostgreSQL Compatibility (DAT305-R1) - AWS re...
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and Underscores
 
Oracle ASM Training
Oracle ASM TrainingOracle ASM Training
Oracle ASM Training
 
Introduction to Elasticsearch
Introduction to ElasticsearchIntroduction to Elasticsearch
Introduction to Elasticsearch
 
10 Good Reasons to Use ClickHouse
10 Good Reasons to Use ClickHouse10 Good Reasons to Use ClickHouse
10 Good Reasons to Use ClickHouse
 
Adventures with the ClickHouse ReplacingMergeTree Engine
Adventures with the ClickHouse ReplacingMergeTree EngineAdventures with the ClickHouse ReplacingMergeTree Engine
Adventures with the ClickHouse ReplacingMergeTree Engine
 
PostgreSQL replication
PostgreSQL replicationPostgreSQL replication
PostgreSQL replication
 
Azure Synapse Analytics Overview (r2)
Azure Synapse Analytics Overview (r2)Azure Synapse Analytics Overview (r2)
Azure Synapse Analytics Overview (r2)
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
SQL Server Clustering and High Availability
SQL Server Clustering and High AvailabilitySQL Server Clustering and High Availability
SQL Server Clustering and High Availability
 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group Replication
 
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
 
SQL Analytics Powering Telemetry Analysis at Comcast
SQL Analytics Powering Telemetry Analysis at ComcastSQL Analytics Powering Telemetry Analysis at Comcast
SQL Analytics Powering Telemetry Analysis at Comcast
 
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdfProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
 

Semelhante a How to Take Advantage of Optimizer Improvements in MySQL 8.0

The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
Geir Høydalsvik
 

Semelhante a How to Take Advantage of Optimizer Improvements in MySQL 8.0 (20)

Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0
 
OpenWorld 2018 - SQL Tuning in 20 mins
OpenWorld 2018 - SQL Tuning in 20 minsOpenWorld 2018 - SQL Tuning in 20 mins
OpenWorld 2018 - SQL Tuning in 20 mins
 
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Wellington APAC Groundbreakers tour - Upgrading to the 12c OptimizerWellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
 
Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7
 
Using The Mysql Binary Log As A Change Stream
Using The Mysql Binary Log As A Change StreamUsing The Mysql Binary Log As A Change Stream
Using The Mysql Binary Log As A Change Stream
 
MySQL 8.0 GIS Overview
MySQL 8.0 GIS OverviewMySQL 8.0 GIS Overview
MySQL 8.0 GIS Overview
 
Meetup my sql5.6_cluster
Meetup my sql5.6_clusterMeetup my sql5.6_cluster
Meetup my sql5.6_cluster
 
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
 
Time series denver an introduction to prometheus
Time series denver   an introduction to prometheusTime series denver   an introduction to prometheus
Time series denver an introduction to prometheus
 
MySQL Performance Schema : fossasia
MySQL Performance Schema : fossasiaMySQL Performance Schema : fossasia
MySQL Performance Schema : fossasia
 
Apache kylin 2.0: from classic olap to real-time data warehouse
Apache kylin 2.0: from classic olap to real-time data warehouseApache kylin 2.0: from classic olap to real-time data warehouse
Apache kylin 2.0: from classic olap to real-time data warehouse
 
MySQL Manchester TT - Performance Tuning
MySQL Manchester TT  - Performance TuningMySQL Manchester TT  - Performance Tuning
MySQL Manchester TT - Performance Tuning
 
002 Introducing Neo4j 5 for Administrators - NODES2022 AMERICAS Beginner 2 - ...
002 Introducing Neo4j 5 for Administrators - NODES2022 AMERICAS Beginner 2 - ...002 Introducing Neo4j 5 for Administrators - NODES2022 AMERICAS Beginner 2 - ...
002 Introducing Neo4j 5 for Administrators - NODES2022 AMERICAS Beginner 2 - ...
 
20190713_MySQL開発最新動向
20190713_MySQL開発最新動向20190713_MySQL開発最新動向
20190713_MySQL開発最新動向
 
times ten in-memory database for extreme performance
times ten in-memory database for extreme performancetimes ten in-memory database for extreme performance
times ten in-memory database for extreme performance
 
OpenWorld 2018 - 20 years of hints and tips
OpenWorld 2018 - 20 years of hints and tipsOpenWorld 2018 - 20 years of hints and tips
OpenWorld 2018 - 20 years of hints and tips
 
“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core
 
How to analyze and tune sql queries for better performance vts2016
How to analyze and tune sql queries for better performance vts2016How to analyze and tune sql queries for better performance vts2016
How to analyze and tune sql queries for better performance vts2016
 
One bridge to connect them all. Oracle GoldenGate for Big Data.UKOUG Tech 2018
One bridge to connect them all. Oracle GoldenGate for Big Data.UKOUG Tech 2018One bridge to connect them all. Oracle GoldenGate for Big Data.UKOUG Tech 2018
One bridge to connect them all. Oracle GoldenGate for Big Data.UKOUG Tech 2018
 
20190915_MySQL開発最新動向
20190915_MySQL開発最新動向20190915_MySQL開発最新動向
20190915_MySQL開発最新動向
 

Mais de Norvald Ryeng

Mais de Norvald Ryeng (6)

JSON Array Indexes in MySQL
JSON Array Indexes in MySQLJSON Array Indexes in MySQL
JSON Array Indexes in MySQL
 
Spatial Support in MySQL
Spatial Support in MySQLSpatial Support in MySQL
Spatial Support in MySQL
 
LATERAL Derived Tables in MySQL 8.0
LATERAL Derived Tables in MySQL 8.0LATERAL Derived Tables in MySQL 8.0
LATERAL Derived Tables in MySQL 8.0
 
More SQL in MySQL 8.0
More SQL in MySQL 8.0More SQL in MySQL 8.0
More SQL in MySQL 8.0
 
MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?
 
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?
 

Último

Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
AroojKhan71
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
MarinCaroMartnezBerg
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
shambhavirathore45
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
Lars Albertsson
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 

How to Take Advantage of Optimizer Improvements in MySQL 8.0

  • 1. Copyright © 2018 Oracle and/or its afliates. All rights reserved. How to Take Advantage of Optiiier Iiproveients in MySQL 8.0 Norvald H. Ryeng Sofware Developient Senior Manager MySQL Optiiier Teai October, 2018
  • 2. 3Copyright © 2018 Oracle and/or its afliates. All rights reserved. Safe Harbor Stateient The following is intended to outline our general product directon. It is intended for inforiaton purposes only, and iay not be incorporated into any contract. It is not a coiiitient to deliver any iaterial, code, or functonality, and should not be relied upon in iaking purchasing decisions. The developient, release, and tiing of any features or functonality described for Oracle’s products reiains at the sole discreton of Oracle.
  • 3. 4Copyright © 2018 Oracle and/or its afliates. All rights reserved. Prograi Agenda Cost iodel iiproveients Histograis Optiiier hints Iiproved perforiance with CTEs Iiproveients in query executon 1 2 3 4 5 6 7
  • 4. 6Copyright © 2018 Oracle and/or its afliates. All rights reserved. Cost Model Iiproveients
  • 5. 7Copyright © 2018 Oracle and/or its afliates. All rights reserved. Optiiier Cost Model t1 Cost estiate Row estiate Cost Model Cost foriulas Access iethods Join Subquery Cost constants CPU IO Metadata: - Row and index siie - Index inforiaton - Uniqueness Statstcs: - Table siie - Cardinality - Range estiates Cost iodel configuraton Range scan JOIN New in MySQL 5.7
  • 6. 8Copyright © 2018 Oracle and/or its afliates. All rights reserved. Motvaton for Iiproving the MySQL Cost Model ● Produce iore correct cost estiates – Beter decisions by the optiiier should iiprove perforiance ● Adapt to new hardware architectures – SSD, larger ieiories, caches ● More iaintainable cost iodel iipleientaton – Avoid hard coded “cost constants” – Refactoring of existng cost iodel code ● Configurable and tunable ● Make iore of the optiiier cost-based Faster queries
  • 7. 9Copyright © 2018 Oracle and/or its afliates. All rights reserved. New Storage Technologies ● Tiie to do a table scan of 10 iillion records: ● Adjust cost iodel to support diferent storage technologies ● Provide configurable cost constants for diferent storage technologies Meiory 5 s SSD 20 - 146 s Hard disk 32 - 1465 s Provide a prograi that could ieasure perforiance and suggest good cost constant configuraton for a running MySQL server? Provide a prograi that could ieasure perforiance and suggest good cost constant configuraton for a running MySQL server?
  • 8. 10Copyright © 2018 Oracle and/or its afliates. All rights reserved. Meiory Bufer Aware Cost Estiates ● Storage engines: – Estiate for how iuch of data and indexes are in a ieiory bufer – Estiate for hit rate for ieiory bufer ● Optiiier cost iodel: – Take into account whether data is already in ieiory or need to be read froi disk Server Storage engine Disk data Query executor Database bufer
  • 9. 11Copyright © 2018 Oracle and/or its afliates. All rights reserved. Disk vs. Meiory Access ● New defaults for const constants: ● InnoDB reports percentage of data cached in bufer pool for each table/index ● Note: Query plan iay change between executons Cost MySQL 5.7 MySQL 8.0 Read a randoi disk page 1.0 1.0 Read a data page froi ieiory bufer 1.0 0.25 Evaluate query conditon 0.2 0.1 Coipare keys/rows 0.1 0.05 Saie cost constant
  • 10. 12Copyright © 2018 Oracle and/or its afliates. All rights reserved. DBT-3 Query 8 Natoonal market share SELECT o_year, SUM(CASE WHEN naton = '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 naton 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 naton n1 ON c_natonkey = n1.n_natonkey JOIN region ON n1.n_regionkey = r_regionkey JOIN naton n2 ON s_natonkey = n2.n_natonkey WHERE r_name = 'EUROPE' AND o_orderdate BETWEEN '1995-01-01' AND '1996-12-31' AND p_type = 'PROMO BRUSHED STEEL' ) AS all_natons GROUP BY o_year ORDER BY o_year; High selectvity predicate
  • 11. 13Copyright © 2018 Oracle and/or its afliates. All rights reserved. Plan A Plan B DBT-3 Query 8 Alteronatve query plaons
  • 12. 14Copyright © 2018 Oracle and/or its afliates. All rights reserved. DBT-3 Query 8 Executoon tme (8.0.3) Ion-memory Disk-bouond Plaon A 5.8 secs 9 iin 47 secs Plaon B 77.5 secs 3 mion 49 secs Ion-memory Disk-bouond MySQL 5.6 Plan B MySQL 5.7 Plan A MySQL 8.0 Plan A Plan B Selected plaon
  • 13. 15Copyright © 2018 Oracle and/or its afliates. All rights reserved. How to Configure your Own Cost Constants UPDATE mysql.engine_cost SET cost_value=0.1 WHERE cost_name='memory_block_read_cost'; FLUSH OPTIMIZER_COSTS; Note: ● Only new connectons will see updated cost constants Future ideas: ● Create tool to tune cost constants to specific hardware ● Use iachine learning to find optial values for specific user load Default 0.25 Make optiiier read new cost constants
  • 14. 16Copyright © 2018 Oracle and/or its afliates. All rights reserved. Histograis
  • 15. 17Copyright © 2018 Oracle and/or its afliates. All rights reserved. Histograis ● Provides the optiiier with inforiaton about coluin value distributon ● To create/recalculate histograi for a coluin: ANALYZE TABLE table UPDATE HISTOGRAM ON column WITH n BUCKETS; ● May use saipling – Saiple siie is based on available ieiory (histograi_generaton_iax_iei_siie) ● Autoiatcally chooses between two histograi types: – Singleton: One value per bucket – Equi-height: Multple values per bucket – Max 1024 buckets
  • 16. 18Copyright © 2018 Oracle and/or its afliates. All rights reserved. Histograis Exaiple EXPLAIN SELECT * FROM customer JOIN orders ON c_custkey = o_custkey WHERE c_acctbal < -1000 AND o_orderdate < '1993-01-01'; id select type table type possible keys key key leon ref rows fltered extra 1 SIMPLE orders ALL i_o_orderdate, i_o_custkey NULL NULL NULL 15000000 31.19 Using where 1 SIMPLE custoier eq_ref PRIMARY PRIMARY 4 dbt3.orders. o_custkey 1 33.33 Using where
  • 17. 19Copyright © 2018 Oracle and/or its afliates. All rights reserved. Histograis Exaiple ANALYZE TABLE customer UPDATE HISTOGRAM ON c_acctbal WITH 1024 BUCKETS; EXPLAIN SELECT * FROM customer JOIN orders ON c_custkey = o_custkey WHERE c_acctbal < -1000 AND o_orderdate < '1993-01-01'; id select type table type possible keys key key leon ref rows fltered extra 1 SIMPLE custoier ALL PRIMARY NULL NULL NULL 1500000 0.00 Using where 1 SIMPLE orders ref i_o_orderdate, i_o_custkey i_o_custkey 5 dbt3. custoier. c_custkey 1 31.19 Using where
  • 18. 20Copyright © 2018 Oracle and/or its afliates. All rights reserved. Histograis Exaiple 0 2 4 6 8 10 12 14 16 orders → custoier custoier → orders QueryExecutoonTime(secoonds)
  • 19. 21Copyright © 2018 Oracle and/or its afliates. All rights reserved. Singleton Histograi ● One value per bucket ● Each bucket stores: – Value – Cuiulatve frequency ● Well suited to estiate both equality and range predicates 0 1 2 3 5 6 7 8 9 10 0 0.05 0.1 0.15 0.2 0.25 Frequeoncy
  • 20. 22Copyright © 2018 Oracle and/or its afliates. All rights reserved. Equi-Height Histograi ● Multple values per bucket ● Not quite equi-height – Values are not split across buckets – Frequent values in separate buckets ● Each bucket stores: – Miniiui value – Maxiiui value – Cuiulatve frequency – Nuiber of distnct values ● Best suited for range predicates 0 - 0 1 - 1 2 - 3 5 - 6 7 - 10 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 Frequeoncy
  • 21. 23Copyright © 2018 Oracle and/or its afliates. All rights reserved. Usage ● Create or refresh histograi(s) for coluin(s): ANALYZE TABLE table UPDATE HISTOGRAM ON column [, column] WITH n BUCKETS; – Note: Will only update histograi, not other statstcs ● Drop histograi: ANALYZE TABLE table DROP HISTOGRAM ON column [, column]; ● Based on entre table or saipling: – Depends on available ieiory: histogram_geoneratoon_max_mem_size (default: 20 MB) ● New storage engine API for saipling – Default iipleientaton: Full table scan even when saipling – Storage engines iay iipleient iore efcient saipling
  • 22. 24Copyright © 2018 Oracle and/or its afliates. All rights reserved. Storage ● Stored in a JSON coluin in data dictonary ● Can be inspected in Inforiaton Scheia view SELECT JSON_PRETTY(histogram) FROM informaton_schema.column_statstcs WHERE schema_name = 'dbt3_sf1' AND table_name ='lineitei' AND column_name = 'l_linenuiber';
  • 23. 25Copyright © 2018 Oracle and/or its afliates. All rights reserved. Histograi Content { "buckets": [[1, 0.24994938524948698], [2, 0.46421066400720523], [3, 0.6427401784471978], [4, 0.7855470933802572], [5, 0.8927398868395817], [6, 0.96423707532558], [7, 1] ], "data-type": "int", "null-values": 0.0, "collaton-id": 8, "last-updated": "2018-02-03 21:05:21.690872", "saipling-rate": 0.20829115437457252, "histograi-type": "singleton", "nuiber-of-buckets-specified": 1024 }
  • 24. 27Copyright © 2018 Oracle and/or its afliates. All rights reserved. When to Create Histograis ● Histograis are useful for coluins that are – Not the first coluin of any index, and – Used in WHERE conditons of ● JOIN queries ● Queries with IN subqueries ● ORDER BY … LIMIT queries ● Best fit – Low cardinality coluins (e.g., gender, orderStatus, dayOfWeek, enuis) – Coluins with uneven distributon (skew) – Stable distributon over tie
  • 25. 28Copyright © 2018 Oracle and/or its afliates. All rights reserved. When Not to Create Histograis ● First coluin of an index ● Coluin that is never used in WHERE clauses ● Monotonically increasing coluin values (e.g., tiestaips) – Histograis will need frequent updates – Consider creatng and index instead
  • 26. 29Copyright © 2018 Oracle and/or its afliates. All rights reserved. How Many Buckets? ● If possible, enough to create a singleton histograi ● Equi-height: 100 buckets should be enough – 1 % resoluton
  • 27. 30Copyright © 2018 Oracle and/or its afliates. All rights reserved. Optiiier Hints
  • 28. 31Copyright © 2018 Oracle and/or its afliates. All rights reserved. New Optiiier Hints in 8.0 ● Join order – JOIN_ORDER(tables) – JOIN_PREFIX(tables) – JOIN_SUFFIX(tables) – JOIN_FIXED_ORDER() ● Setng systei variables – SET_VAR(sysvar=value) ● View/derived table/CTE ierge – MERGE(views) – NO_MERGE(views) ● Index ierge – INDEX_MERGE(indexes) – NO_INDEX_MERGE(indexes)
  • 29. 32Copyright © 2018 Oracle and/or its afliates. All rights reserved. Join Order Hints ● No need to reorganiie the FROM clause to add join order hints like you will for STRAIGHT_JOIN ● /*+ JOIN_ORDER(t1, t2, …) */ ● /*+ JOIN_PREFIX(t1, t2, …) */ ● /*+ JOIN_SUFFIX(…, ty, tz) */ ● /*+ JOIN_FIXED_ORDER() */ – Replaceient for STRAIGHT_JOIN
  • 30. 33Copyright © 2018 Oracle and/or its afliates. All rights reserved. Join Order Hint Exaiple EXPLAIN SELECT /*+ JOIN_ORDER(customer, orders) */ * FROM customer JOIN orders ON c_custkey = o_custkey WHERE c_acctbal < -1000 AND o_orderdate < '1993-01-01'; Alternatves with saie efect for this query: JOIN_PREFIX(customer) JOIN_SUFFIX(orders) JOIN_FIXED_ORDER() id select type table type possible keys key key leon ref rows fltered extra 1 SIMPLE custoier ALL PRIMARY NULL NULL NULL 1500000 33.33 Using where 1 SIMPLE orders ref i_o_orderdate, i_o_custkey i_o_custkey 5 dbt3. custoier. c_custkey 15 31.19 Using where
  • 31. 35Copyright © 2018 Oracle and/or its afliates. All rights reserved. View/Derived Table/CTE Merge Hints ● Views, derived tables and CTEs are, if possible, ierged into outer query ● NO_MERGE can override default behavior SELECT /*+ NO_MERGE(dt) */ * FROM t1 JOIN (SELECT x, y FROM t2) dt ON t1.x = dt.x; ● MERGE will force a ierge SELECT /*+ MERGE(dt) */ * FROM t1 JOIN (SELECT x, y FROM t2) dt ON t1.x = dt.x; ● Can also use MERGE/NO_MERGE hints for views and CTEs SELECT /*+ NO_MERGE(v) */ * FROM t1 JOIN v ON t1.x = v.x;
  • 32. 36Copyright © 2018 Oracle and/or its afliates. All rights reserved. Index Merge Exaiple 10INDEX(a) 10INDEX(b) a=10 AND b=10Result: Intersecton SELECT * FROM t1 WHERE a=10 AND b=10
  • 33. 37Copyright © 2018 Oracle and/or its afliates. All rights reserved. Index Merge Hints ● Index ierge: Merge rows froi iultple range scans on a single table ● Algorithis: union, intersecton, sort union ● Users can specify which indexes to use for index ierge – /*+ INDEX_MERGE(t1 idx1, idx2, …) */ – /*+ NO_INDEX_MERGE(t1 idx1, idx2, …) */
  • 34. 38Copyright © 2018 Oracle and/or its afliates. All rights reserved. Index Merge Hint Exaiple EXPLAIN SELECT count(*) FROM users WHERE user_type=2 AND status=0 AND pareont_id=0; mysql> SELECT count(*) FROM users WHERE user_type=2 AND status=0 AND parent_id=0; ... 1 row in set (1.37 sec) mysql> SELECT /*+ INDEX_MERGE(users user_type, status) */ count(*) -> FROM users WHERE user_type=2 AND status=0 AND parent_id=0; ... 1 row in set (0.18 sec) id select type table type possible keys key key leon ref rows Extra 1 SIMPLE users index_ ierge parent_id, status, user_type user_type, status, parent_id 1,1,4 NULL 2511 Using intersect (user_type, status, parent_id); Using where; Using index Low selectvity predicate
  • 35. 39Copyright © 2018 Oracle and/or its afliates. All rights reserved. Hints to Set Session Variables ● Set a session variable for the duraton of a single stateient ● Exaiples: SELECT /*+ SET_VAR(sort_bufer_size = 16M) */ naie FROM people ORDER BY naie; INSERT /*+ SET_VAR(foreigon_key_checks = OFF) */ INTO t2 VALUES (1, 1), (2, 2), (3, 3); SELECT /*+ SET_VAR(optmizer_switch = 'coonditoon_faonout_flter = of') */ * FROM custoier JOIN orders ON c_custkey = o_custkey WHERE c_acctbal < 0 AND o_orderdate < '1993-01-01'; ● Note: Not all session variables are setable through hints: iysql> SELECT /*+ SET_VAR(max_allowed_packet=128M) */ * FROM t1; Eipty set, 1 warning (0,00 sec) Warning (Code 4537): Variable 'iax_allowed_packet' cannot be set using SET_VAR hint.
  • 36. 40Copyright © 2018 Oracle and/or its afliates. All rights reserved. Skip Index Dives for FORCE INDEX Hint ● Estiate nuiber of rows in a range – The optiiier asks the storage engine for nuiber of rows to be scanned froi an index – Index dives: InnoDB finds first and last row in range and estiates distance – Exaiple: SELECT * FROM t1 WHERE (key1 > 10 AND key1 < 20) AND key2 > 30 Indexes on key1 and key2 will be used to get estiates for the two ranges ● MySQL 8.0 will skip index dives when all of these are satsfied: – FORCE INDEX applies to a single index – Only a single table is accessed in the query – No subqueries – No GROUP BY, DISTINCT or ORDER BY
  • 37. 41Copyright © 2018 Oracle and/or its afliates. All rights reserved. Skip Index Dives Perforiance ● Sysbench scheia – 8 tables – 1 iillion rows per table – Secondary index on integer coluin k – k values are randoily and uniforily distributed 1–1000000 ● Queries SELECT c FROM sbtest WHERE k = ? SELECT c FROM sbtest FORCE INDEX(k_1) WHERE k = ? ● 10–13 % iiproveient 0 50 100 150 200 250 300 0 200000 400000 600000 800000 1000000 Default FORCE INDEX Threads Queriespersecoond
  • 38. 42Copyright © 2018 Oracle and/or its afliates. All rights reserved. Invisible Index ● Index is iaintained by the storage engine, but invisible to the optiiier – Override with SET optiiier_switch='use_invisible_indexes=on'; ● Priiary key can't be invisible ● Use case: Check for perforiance drope before reioving index ALTER TABLE t1 ALTER INDEX idx INVISIBLE; SHOW INDEXES FROM t1; +---------+------------------+----------------------+---------------+ | Table | Key_naie | Coluin_naie | Visible | +---------+------------------+----------------------+---------------+ | t1 | idx | a | NO | +---------+------------------+----------------------+---------------+
  • 39. 43Copyright © 2018 Oracle and/or its afliates. All rights reserved. Iiproved Perforiance with CTEs
  • 40. 44Copyright © 2018 Oracle and/or its afliates. All rights reserved. CTEs Instead of Derived Tables ● A derived table is a subquery in the FROM clause: SELECT … FROM (subquery) AS derived, t1 … ● A CTE is just like a derived table, but the declaraton coies before the query block: WITH derived AS (subquery) SELECT … FROM derived, t1 … ● May precede SELECT, UPDATE, DELETE and be used in subqueries: WITH derived AS (subquery) DELETE FROM t1 WHERE t1.a IN (SELECT b FROM derived);
  • 41. 45Copyright © 2018 Oracle and/or its afliates. All rights reserved. Beter Perforiance with CTEs ● Derived tables can't be referenced twice SELECT … FROM (SELECT a, b, SUM(c) AS s FROM t1 GROUP BY a, b) AS d1 JOIN (SELECT a, b, SUM(c) AS s FROM t1 GROUP BY a, b) AS d2 ON d1.b = d2.a; ● CTEs can WITH d AS (SELECT a, b, SUM(c) AS s FROM t1 GROUP BY a, b) SELECT … FROM d AS d1 JOIN d AS d2 ON d1.b = d2.a; ● Beter perforiance with iaterialiiaton – Multple CTE references are only iaterialiied once – Derived tables and views will be iaterialiied once per reference
  • 42. 46Copyright © 2018 Oracle and/or its afliates. All rights reserved. DBT-3 Query 15 Top Supplier Using view CREATE VIEW revenue0 (supplier_no, total_revenue) AS ( SELECT l_suppkey, SUM(l_extendedprice * (1- l_discount)) FROM lineitem WHERE l_shipdate >= '1996-07-01' AND l_shipdate < DATE_ADD('1996-07-01', INTERVAL '90' DAY) GROUP BY l_suppkey ); Using CTE WITH revenue0 (supplier_no, total_revenue) AS ( SELECT l_suppkey, SUM(l_extendedprice * (1-l_discount)) FROM lineitem WHERE l_shipdate >= '1996-07-01' AND l_shipdate < DATE_ADD('1996-07-01', INTERVAL '90' DAY) GROUP BY l_suppkey ) SELECT s_suppkey, s_name, s_address, s_phone, total_revenue FROM supplier, revenue0 WHERE s_suppkey = supplier_no AND total_revenue = (SELECT MAX(total_revenue) FROM revenue0) ORDER BY s_suppkey;
  • 43. 47Copyright © 2018 Oracle and/or its afliates. All rights reserved. DBT-3 Query 15 with View Materialiiaton ORDER JOIN Supplier (eq_ref) GROUP Lineitei (range) Materialiie GROUP Lineitei (range) Materialiie SELECT Subquery Revenue0 Revenue0 WHERE FROM
  • 44. 48Copyright © 2018 Oracle and/or its afliates. All rights reserved. DBT-3 Query 15 with CTE Materialiiaton ORDER JOIN Supplier (eq_ref) GROUP Lineitei (range) Materialiie SELECT Revenue0 WHERE FROM Subquery
  • 45. 49Copyright © 2018 Oracle and/or its afliates. All rights reserved. DBT-3 Query 15 Perforiance View CTE 0 2 4 6 8 10 12 14 16 18 Queryexecutontie(seconds) View CTE
  • 46. 50Copyright © 2018 Oracle and/or its afliates. All rights reserved. Iiproveients in Query Executon
  • 47. 51Copyright © 2018 Oracle and/or its afliates. All rights reserved. Index Skip-Scan — New ion 8.0.13 ● Used for iult-coluin index ● Conditon not on first part of index ● Cost-based choice Before MySQL 8.0.13: Full index scan MySQL 8.0.13: Skip-scan pk a b c INDEX idx(a,b,c) SELECT * FROM t1 WHERE b < 3; 1 1 2 3 4 5 2 1 2 3 4 5 3 1 2 3 4 5 4 1 2 3 4 5 a b c Full index scan 1 1 2 3 4 5 2 1 2 3 4 5 3 1 2 3 4 5 4 1 2 3 4 5 a b c b < 3 b < 3 b < 3 b < 3
  • 48. 52Copyright © 2018 Oracle and/or its afliates. All rights reserved. Index Skip-Scan Perforiance Exaiple ● DBT-3 scale factor 10 database ● Create index on lineitem – (l_shipdate, l_quantty, l_shipmode) – 60 iillion rows – Cardinality: ● l_shipdate: 2526 ● l_quantty: 50 ● Query: SELECT l_shipdate, l_quantty FROM lineitem WHERE l_quantty > ? AND l_shipmode = 'XXX'; ● Skip-scan is faster when less than 80 % of the rows are accessed! ● 0 10 20 30 40 50 60 0 5 10 15 20 25 Index Scan Skip Scan l_quaontty > ?
  • 49. 53Copyright © 2018 Oracle and/or its afliates. All rights reserved. Thaonks for the patch, Facebook!
  • 50. 54Copyright © 2018 Oracle and/or its afliates. All rights reserved. Iiproved Scan Perforiance ● Motvaton – InnoDB already uses an internal bufer to fetch records in batches – The optiiier knows the operaton and estiated nuiber of records to read ● The optiiier knows how large the bufer should be ● 8.0 extends the handler API – Provide a bufer to the storage engine when expectng iore than one row ● Exaiple queries with iiproved perforiance: SELECT * FROM t; SELECT * FROM t WHERE pk BETWEEN 1000 AND 10000; SELECt * FROM t1, t2 WHERE t1.pk=t2.pk; /* Bufer for the outer table */
  • 51. 55Copyright © 2018 Oracle and/or its afliates. All rights reserved. Iiproved Scan Perforiance ● 18 out of 22 queries got iiproved perforiance ● 4 queries show iore than 10 % iiproveient ● 2 queries show iore than 15 % iiproveient
  • 52. 56Copyright © 2018 Oracle and/or its afliates. All rights reserved. Internal Teiporary Table Storage Engine ● Variable-length rows ● Supports JSON, TEXT, BLOB and geoietry types – New ion MySQL 8.0.13! ● Supports overfow to disk – No overhead for convertng to InnoDB/MyISAM when table becoies big ● Coiion ieiory pool for all internal teiporary tables – No fixed liiit per table – teiptable_iax_rai (default 1 GB) ● Perforiance scheia events for ieiory/disk usage – ieiory/teiptable/physical_rai, ieiory/teiptable/physical_disk
  • 53. 57Copyright © 2018 Oracle and/or its afliates. All rights reserved. DBT-3 with TeipTable vs. MEMORY ● MySQL 8.0.13 ● DBT-3, Scale factor 10 ● innodb_bufer_pool_siie = 32G (CPU- bound) ● internal_tip_iei_storage_engine = "TeipTable" or "MEMORY" ● Other 20 queries have saie perforiance for both engines Q10 Q11 0% 20% 40% 60% 80% 100% Query Executoon Time Relatve to MEMORY MEMORY TeipTable
  • 54. 58Copyright © 2018 Oracle and/or its afliates. All rights reserved. JSON Data in TeipTable ● MySQL 8.0.13 ● JSON variant of sysbench tests – Range siie: 500 – Data siie: 127 GB ● 64 threads htp://iysqlserverteai.coi/iysql-8-0-support-for-blobs-in-teiptable-engine 0% 300% 600% 900% Traonsactoon Througput Relatve to IononoDB InnoDB TeipTable
  • 55. 59Copyright © 2018 Oracle and/or its afliates. All rights reserved. Descending Index CREATE TABLE t1 ( a INT, b INT, INDEX a_b (a DESC, b ASC) ); ● In 5.7: Index in ascending order is created, server scans it backwards ● In 8.0: Index in descending order is created, server scans it forwards – Works on B-tree indexes only – Benefits: ● Use indexes instead of filesort for ORDER BY clause with ASC/DESC sort key ● Forward index scan is slightly faster than backward index scan
  • 56. 60Copyright © 2018 Oracle and/or its afliates. All rights reserved. Feature descriptons and design details directly froi the source. htp://mysqlserverteam.com/
  • 57. 61Copyright © 2018 Oracle and/or its afliates. All rights reserved.