copyright © 2013, oracle and/or its affiliates. all rights ...€¦ · mysql 5.6 optimizer...

28
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 1

Upload: others

Post on 25-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 1

Page 2: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Overview of Optimizer Features in 5.6

Manyi Lu

Senior Manager, MySQL Optimizer Team

Page 3: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 3

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

decision. The development, release, and timing of any features or

functionality described for Oracle’s products remains at the sole

discretion of Oracle.

Safe Harbor Statement

Page 4: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 4

MySQL Optimizer

Find optimizer query execution plan

– Index selection

– Join order

– Query transformation

Cost-based optimization

– Find cost of different plans, choose the best

Page 5: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 5

MySQL Optimizer

Understanding the optimizer is crucial in designing efficient schemas and queries.

Many needs for workarounds are going away

Know where to look to see what the optimizer is doing

Joins just got a lot faster - know how to make the most of it

Page 6: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 6

MySQL 5.6 Optimizer Improvements

Improved subquery execution

File sort optimizations with small limit

Index condition pushdown

Batched key access and multi range read

Postponed materialization

Page 7: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 7

MySQL 5.6 Optimizer Improvements

Many table join

In() query with large number of values

Persistent optimizer statistics

Explain for insert, delete, and update

Optimizer traces

Structured explain in JSON format

Page 8: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 8

Subquery Optimizations

Prior to 5.6: Users try to avoid subqueries due to poor performance

5.6: optimized IN subqueries

SELECT title FROM film WHERE film_id IN

(SELECT film_id FROM film_actor);

Algorithms:

– Table pullout

– Semi-join

– Materialization

Example: DBT3 Query #18

– Execution time reduces from days to seconds

Page 9: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 9

File Sort Optimization with Small Limit

Web use case – list first 100 products sorted by name

Avoid intermediate sorted files

Produce ordered result set using a single table scan

Example above: 20 million rows, default sort buffer

=>3X better execution time, drops from 40s to 10s

Page 10: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 10

Multi Range Read (MRR)

Non-MRR:

Random disk access to base table data

MRR:

More sequential scan of the base table data

Page 11: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 11

MySQL 5.5: Data Access without MRR

Index Table

Index

scan

Random access

Page 12: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 12

MySQL 5.6: Data Access with MRR

Example with InnoDB

Index Table

Index

scan

PKs in

index order

PKs in

PK order

Sort

Sweep-

read rows

Collect

PKs in

buffer

Page 13: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 13

MySQL 5.6:Regular Nested Loop Join

Without join buffering

Index Table2 Table1

Table

scan

Page 14: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 14

MySQL 5.6: Batched Key Access (BKA)

MRR Applied to Join Buffering

Index

PKs in join

buffer order

PKs in

PK order

Sort

Table2 Sweep-

read rows

Collect

PKs in

buffer Table1

Join buffer

Page 15: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 15

Batched Key Access & Multi Range Read

Query execution time without

BKA and MRR

Query execution time with

BKA and MRR

DBT3 Q13 Customer distribution query

Improves performance of disk bound queries

Page 16: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 16

Index Condition Pushdown

With ICP disabled

15s (buffer pool 128Mb)

1.4s (buffer pool 1.5Gb)

CREATE TABLE person (

personid INTEGER PRIMARY KEY,

firstname CHAR(20),

lastname CHAR(20),

postalcode INTEGER,

age INTEGER,

address CHAR(50),

KEY k1(postalcode, age)

) ENGINE=innodb;

SELECT firstname, lastname from person

where postalcode BETWEEN 5000 and 5500 AND age BETWEEN 21 AND 22;

With ICP enabled

Execution time drops to 90

ms for both

Page 17: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 17

Postpone Materialization of Views/Subqueries in FROM

Late materialization:

– Fast EXPLAINs for views/subqueries

– Avoid materialization when possible, faster bail out

EXPLAIN SELECT * FROM (SELECT * FROM a_big_table);

Page 18: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 18

Postpone Materialization of Views/Subqueries in FROM Adding index for derived table

– Allow ref access to derived tables

SELECT … FROM derived_table AS dt

join table AS t WHERE dt.fld = t.dlf

=> 240X better execution time (drops from ~8 min to ~2 sec)

Page 19: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 19

Improved Performance for Many-Table Joins

(Number of tables)! possible

combinations

Drastically reduce cost of

finding the optimal query plan

More optimal final chosen plan

5.5: tables ordered in

increasing number of rows

5.6: take key dependency into

account

A query joining 24 tables

Page 20: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 20

Queries with Many Values in the IN clause

Before

– two index dive for each value to estimate the number of rows

– Takes longer to optimize the query than to execute it

Now

– Use average number of rows per value from the statistics

– Significantly reduce the optimization time

– eq_range_index_dive_limit = 10 by default

SELECT * FROM t1 WHERE col1 IN (large number of value);

Page 21: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 21

Persistent Optimizer Statistics (InnoDB)

More accurate statistics

More stable statistics

Turn on by default

Automatically recalculates statistics by default

ANALYSE TABLE

Can be manually updated (Good for testing purpose)

Page 22: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 22

Explain for Data Modifying Statements

Page 23: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 23

Explain for Data Modifying Statements, cont

Page 24: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 24

Structured Explain

EXPLAIN FORMAT=JSON

More precise than traditional explain

More information

Visualized in MySQL workbench

mysql> EXPLAIN FORMAT=JSON SELECT *

FROM t2 WHERE I > 1 AND j < 3;

{

"query_block": {

"select_id": 1,

"table": {

"table_name": "t2",

"access_type": ”range",

"possible_keys": [

“PRIMIARY”

],

“key”: “PRIMARY”,

“key_length”: “4”,

"rows": 2,

"filtered": 100,

”index_condition": "(`test`.`t2`.`i` > 1)”,

"attached_condition": "(`test`.`t2`.`j` < 3)"

}

}

} |

Page 25: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 25

Visual Explain in MySQL Workbench

Page 26: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 26

Optimizer Traces "rows_estimation": [ {

"table": "`t1`",

"range_analysis": {

"table_scan": {

"rows": 5,

"cost": 4.1

},

"potential_range_indices": [ {

"index": "v_idx",

"usable": true,

"key_parts": [

"v",

"i1” ]

}

],

"best_covering_index_scan": {

"index": "v_idx",

"cost": 2.0063,

"chosen": true

},

Explain shows the generated plan

Trace shows how the plan was generated,

decision points, cost etc

Developers, support, advanced customers

First step in 5.6.3, more tracing to come

SET SESSION. OPTIMIZER_TRACE=‘enabled=on’;

SELECT (SELECT 1 FROM t6 WHERE d=c)

AS RESULT FROM t5;

SELECT* FROM

INFORMATIONM_SCHEMA.OPIMIZER_TRACE;

Page 27: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 27

Page 28: Copyright © 2013, Oracle and/or its affiliates. All rights ...€¦ · MySQL 5.6 Optimizer Improvements Many table join In() query with large number of values Persistent optimizer

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 28