MySQL 8.0 What s New in the Optimizer

Similar documents
MySQL 8.0: Common Table Expressions

Safe Harbor Statement

Histogram Support in MySQL 8.0

#MySQL #oow16. MySQL Server 8.0. Geir Høydalsvik

When and How to Take Advantage of New Optimizer Features in MySQL 5.6. Øystein Grøvlen Senior Principal Software Engineer, MySQL Oracle

MySQL for Developers with Developer Techniques Accelerated

Unit 1 - Chapter 4,5

CSC Web Programming. Introduction to SQL

Combining SQL and NoSQL with MySQL

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Exact Numeric Data Types

More SQL in MySQL 8.0

Covering indexes. Stéphane Combaudon - SQLI

What Is New in Optmizer and Executor?

What's New in MySQL 5.7?

Querying Data with Transact SQL

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Practical MySQL indexing guidelines

MySQL Server 8.0. Geir Høydalsvik. Copyright 2016, 2017, Oracle and/or its its affiliates. All All rights reserved.

Sheeri Cabral. Are You Getting the Best Out of Your MySQL Indexes? Slides

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Copyright 2017, Oracle and/or its aff iliates. All rights reserved.

MariaDB Optimizer. Current state, comparison with other branches, development plans

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

Oracle 1Z0-882 Exam. Volume: 100 Questions. Question No: 1 Consider the table structure shown by this output: Mysql> desc city:


MySQL Query Tuning 101. Sveta Smirnova, Alexander Rubin April, 16, 2015

MTA Database Administrator Fundamentals Course

DB2 SQL Class Outline

20461: Querying Microsoft SQL Server 2014 Databases

MySQL Indexing. Best Practices for MySQL 5.6. Peter Zaitsev CEO, Percona MySQL Connect Sep 22, 2013 San Francisco,CA

Course Outline. MySQL Database Administration & Design. Course Description: Pre-requisites: Course Content:

SQL functions fit into two broad categories: Data definition language Data manipulation language

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

State of the Dolphin Developing new Apps in MySQL 8

MySQL Indexing. Best Practices. Peter Zaitsev, CEO Percona Inc August 15, 2012

Querying Data with Transact-SQL

1Z MySQL 5 Database Administrator Certified Professional Exam, Part II Exam.

Meet the Sergei Golubchik MariaDB Corporation

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

20761B: QUERYING DATA WITH TRANSACT-SQL

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

MySQL 5.0 Certification Study Guide

Oracle Syllabus Course code-r10605 SQL

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Advanced query optimization techniques on large queries. Peter Boros Percona Webinar

GridDB Advanced Edition SQL reference

Querying Data with Transact-SQL

5. Single-row function

Oracle Database 11g: SQL and PL/SQL Fundamentals

InnoDB: What s new in 8.0

SQL Coding Guidelines

Querying Microsoft SQL Server

CGS 3066: Spring 2017 SQL Reference

Getting Started with SQL

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

MySQL 101. Designing effective schema for InnoDB. Yves Trudeau April 2015

Modern SQL: Evolution of a dinosaur

MyRocks Engineering Features and Enhancements. Manuel Ung Facebook, Inc. Dublin, Ireland Sept th, 2017

Query Optimizer MySQL vs. PostgreSQL

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Model Question Paper. Credits: 4 Marks: 140

20761 Querying Data with Transact SQL

CSCU9Q5 Introduction to MySQL. Data Definition & Manipulation (Over ~two Lectures)

Advanced MySQL Query Tuning

Optimizer Standof. MySQL 5.6 vs MariaDB 5.5. Peter Zaitsev, Ovais Tariq Percona Inc April 18, 2012

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

<Insert Picture Here> Looking at Performance - What s new in MySQL Workbench 6.2

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

MySQL JSON. Morgan Tocker MySQL Product Manager. Copyright 2015 Oracle and/or its affiliates. All rights reserved.

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

Understanding the Optimizer

After completing this course, participants will be able to:

Query Optimizer MySQL vs. PostgreSQL

MySQL Schema Best Practices

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Lecture 19 Query Processing Part 1

Querying Data with Transact-SQL

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Querying Data with Transact-SQL

Database Applications (15-415)

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Writing High Performance SQL Statements. Tim Sharp July 14, 2014

SQL Interview Questions

Modern Development With MySQL

Mysql Create Table Example Primary Key Foreign

Creating and Managing Tables Schedule: Timing Topic

NoSQL + SQL = MySQL. Nicolas De Rico Principal Solutions Architect

Chapter 8: Working With Databases & Tables

<Insert Picture Here> Boosting performance with MySQL partitions

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Chapter-14 SQL COMMANDS

Querying Data with Transact-SQL

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

SQL Commands & Mongo DB New Syllabus

AVANTUS TRAINING PTE LTD

PASSWORDS TREES AND HIERARCHIES. CS121: Relational Databases Fall 2017 Lecture 24

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

Transcription:

MySQL 8.0 What s New in the Optimizer Manyi Lu Director MySQL Optimizer & GIS Team, Oracle October 2016 Copyright Copyright 2 015, 2016,Oracle Oracle and/or and/or its its affiliates. affiliates. All All rights rights reserved. 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. 2

New Features in 8.0.0 Ø Invisible index Ø Descending index Ø Common table expressions Ø Improved performance of scans Ø JSON aggregation Ø Hints Ø Better IPv6 and UUID support 3

Invisible Index ALTER TABLE t1 ALTER INDEX idx INVISIBLE; mysql> SHOW INDEXES FROM t1; +- - - - - - - - - +- - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + Table Key_name Column_name Visible +- - - - - - - - - +- - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - + t1 idx a NO +- - - - - - - - - +- - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - + Maintained by the SE, ignored by the Optimizer Primary key cannot be INVISIBLE Use case: Check for performance drop BEFORE dropping an index 4

New Features in 8.0 Ø Invisible index Ø Descending index Ø Common table expressions Ø Improved performance of scans Ø JSON aggregation Ø Hints Ø Better IPv6 and UUID support 5

Descending Index (lab release 8.0.0 Optimizer) 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 btree index only Benefits: Forward index scan is faster than backward index scan Use indexes instead of filesort for ORDER BY clause with ASC/DESC sort key Download from http://labs.mysql.com/ 6

New Features in 8.0 Ø Invisible index Ø Descending index Ø Common table expressions Ø Improved performance of scans Ø JSON aggregation Ø Hints Ø Better UUID and IPv6support 7

Common Table Expression (8.0.0 optimizer lab release) SELECT FROM (subquery) AS derived, t1... WITH derived AS subquery SELECT FROM derived, t1...; A derived table is a subquery in the FROM clause CTE is just a derived table, but you put its declaration before the outer SELECT instead of in FROM clause. 8

Common Table Expression VS Derived Table Advantages over derived table Better readability Derived table requires inside out reading SELECT FROM (SELECT FROM (SELECT FROM # understand this, go up Can be referenced more than once Derived table can't be referenced twice in FROM SELECT FROM (subquery) AS derived, derived 9

Common Table Expression VS Derived Table Advantages over Derived table Better performance SELECT FROM (subquery) AS derived, (same subquery again) AS derived1 Two independent derived tables: if materialization is used, two materializations are done. Performance problem (more space, more time, longer locks) Chainable Derived table doesn t allow chaining SELECT FROM (subquery) AS derived, (SELECT FROM derived,...) AS derived1 Error : Derived table can't refer to previously created derived table 10

Common Table Expression VS Derived Table WITH cte1(txt) AS (SELECT "This "), cte2(txt) AS (SELECT CONCAT(cte1.txt,"is a ") FROM cte1), cte3(txt) AS (SELECT "nice query" UNION SELECT "query that rocks" UNION SELECT "query"), cte4(txt) AS (SELECT concat(cte2.txt, cte3.txt) FROM cte2, cte3) SELECT MAX(txt), MIN(txt) FROM cte4; +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - + MAX(txt) MIN(txt) +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - + This is a query that rocks This is a nice query +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - + 11

Recursive CTE WITH RECURSIVE cte AS ( SELECT... FROM table_name /* "seed" SELECT */ UNION ALL SELECT... FROM cte, table_name) /* "recursive" SELECT */ SELECT... FROM cte; A recursive CTE refers to itself in a subquery The seed SELECT is executed once to create the initial data subset, the recursive SELECT is repeatedly executed to return subsets of data until the complete result set is obtained. Useful to dig in hierarchies (parent/child, part/subpart) Similar to Oracle's CONNECT BY 12

Recursive CTE Simple Example Print 1 to 10 : WITH RECURSIVE qn AS ( SELECT 1 AS a UNION ALL SELECT 1+a FROM qn WHERE a<10 ) SELECT * FROM qn; a 1 2 3 4 5 6 7 8 9 10 13

Recursive CTE Example Fibonacci Numbers Fibonacci numbers: WITH RECURSIVE qn AS ( SELECT 1 AS n, 1 AS un, 1 AS unp1 UNION ALL SELECT 1+n, unp1, un+unp1 FROM qn WHERE n<10) SELECT * FROM qn; n un unp1 1 1 1 2 1 2 3 2 3 4 3 5 5 5 8 6 8 13 7 13 21 8 21 34 9 34 55 10 55 89 14

Recursive CTE Example Hierarchy Traversal CEATE TABLE EMPLOYEES ( ID INT PRIMARY KEY, NAME VARCHAR(100), MANAGER_ID INT, INDEX (MANAGER_ID), FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES(ID) ); INSERT INTO EMPLOYEES VALUES (333, Yasmina", NULL), # CEO, Manager ID = NULL (198, "John", 333), # John is 198 reports to 333, Yasmina (692, "Tarek", 333), (29, "Pedro", 198), # Pedro is 29, reports to 198, John (4610, "Sarah", 29), (72, "Pierre", 29), (123, "Adil", 692); 15

Recursive CTE Example Hierarchy Traversal Print all employees and their management chain: WITH RECURSIVE EMPLOYEES_EXTENDED (ID, NAME, PATH) AS ( SELECT ID, NAME, CAST(ID AS CHAR(200)) FROM EMPLOYEES WHERE MANAGER_ID IS NULL UNION ALL SELECT S.ID, S.NAME, CONCAT(M.PATH, ",", S.ID) FROM EMPLOYEES_EXTENDED M JOIN EMPLOYEES S ON M.ID=S.MANAGER_ID ) SELECT * FROM EMPLOYEES_EXTENDED ORDER BY PATH; ID NAME PATH 333 Yasmina 333 198 John 333,198 29 Pedro 333,198,29 4610 Sarah 333,198,29,4610 # Sarah- >Pedro- >John- >Yasmina 72 Pierre 333,198,29,72 692 Tarek 333,692 123 Adil 333,692,123 16

New Features in 8.0 Ø Invisible index Ø Descending index Ø CTE Ø Improved performance of scans Ø JSON aggregation Ø Hints Ø Better UUID and IPv6support 17

Improved Performance of Scans Motivation: InnoDB already uses an internal buffer to fetch records in batches Optimizer knows the operation and the estimated number of records to read Why cannot optimizer tell InnoDB how much to read?? 8.0 extends the handler API: provides a buffer for each table or index where optimizer expects more than one row So far only InnoDB uses this API Queries with improved performance: SELECT * FROM t; SELECT * FROM t WHERE pk BETWEEN 1000 and 10 000; SELECT * FROM t1, t2 WHERE t1.pk = t2.pk; /* buffer for the outer table, no need for inner table which is accessed using eq_ref) 18

Improved Performance of Scans - DBT3 18 out of 22 queries got improved performance 4 queries have more than 10% improvement, 2 queries more than 15% improvements 19

New Features in 8.0 Ø Invisible index Ø Descending index Ø CTE Ø Improved performance of scans Ø JSON Aggregation Ø Hint Ø Better UUID and IPv6support 20

JSON Support 5.7: JSON datatype JSON functions Indexing of JSON through virtual column 8.0: Add more functions Improve performance

JSON Aggregation (8.0.0 optimizer lab release) Combine JSON documents in multiple rows into a JSON array CREATE TABLE t1(id INT, grp INT, jsoncol JSON); INSERT INTO t1 VALUES(1, 1, '{"key1":"value1","key2":"value2"}'); INSERT INTO t1 VALUES(2, 1, '{"keya":"valuea","keyb":"valueb"}'); SELECT JSON_ARRAYAGG(jsoncol) AS json FROM t1; [{"key1":"value1","key2":"value2"}, {"keya":"valuea","keyb":"valueb"}, {"keyx":"valuex","keyy":"valuey"}] INSERT INTO t1 VALUES(3, 2, '{"keyx":"valuex","keyy":"valuey"}'); 22

JSON Aggregation (8.0.0 optimizer lab release) Combine JSON documents in multiple rows into a JSON object CREATE TABLE t1(id INT, grp INT, jsoncol JSON); INSERT INTO t1 VALUES(1, 1, '{"key1":"value1","key2":"value2"}'); INSERT INTO t1 VALUES(2, 1, '{"keya":"valuea","keyb":"valueb"}'); SELECT JSON_OBJECTAGG(id, jsoncol) AS json FROM t1 GROUP BY grp; 1 {"1":{"key1":"value1","key2":"value2"}, "2":{"keyA":"valueA","keyB":"valueB"}} 2 {"3":{"keyX":"valueX","keyY":"valueY"}} INSERT INTO t1 VALUES(3, 2, '{"keyx":"valuex","keyy":"valuey"}'); 23

New Features in 8.0 Ø Invisible index Ø Descending index Ø CTE Ø Improved performance of scans Ø Hints Ø Better UUID and IPv6support 24

Improved HINTs 5.7: Introduced new hint syntax /*+...*/ Flexibility over optimizer switch, effect individual statement only Hints within statement take precedence over optimizer switch Hints apply at different scope levels: global, query block, table, index 8.0: Add more hints, gradually replace the old hint syntax

Hint: Join Order (8.0.0 optimizer lab Release) Hints to control table order for join execution 5.7: STRAIGHT_JOIN to force the listed order in FROM clause 8.0: JOIN_FIXED_ORDER /* replacement for STRAIGHT_JOIN*/ JOIN_ORDER /* use specified order */ t JOIN_PREFIX /* use specified order for first tables */ JOIN_SUFFIX /* use specified order for last tables */ 26

Join Order Hints Original query 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 1 SIMPLE orders ALL 1 SIMPLE customer eq_ ref i_o_orderdate, i_o_custkey key len PRIMARY PRIMARY 4 ref rows filtered extra NULL NULL NULL 15000000 31.19 dbt3.orders. o_custkey 1 33.33 Using where Using where

Join Order Hints Change join order with hint 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'; id select type table type possible keys key key len ref rows filtered extra 1 SIMPLE customer ALL PRIMARY NULL NULL NULL 1500000 31.19 1 SIMPLE orders ref i_o_orderdate, i_o_custkey i_o_custkey 5 dbt3. customer. c_custkey 15 33.33 Using where Using where Alternatives with same effect for this query: JOIN_PREFIX(customer) JOIN_SUFFIX(orders) JOIN_FIXED_ORDER()

Hint: Merge/Materialize Derived Table SELECT /*+ NO_MERGE(dt) */ * FROM t1 JOIN (SELECT x, y FROM t2) dt ON t1.x = dt.x; Derived table is subquery in a FROM clause 5.6: Always materialize 5.7: Merged into outer queries in most cases, materialized in some cases 8.0: Users can override default behavior with hints /*+ MERGE() */ /*+ NO_MERGE() */ 29

New Features in 8.0 Ø Invisible index Ø Descending index Ø CTE Ø Improved performance of scans Ø Hints Ø Better UUID and IPv6support 30

Improved Support for UUID mysql> select uuid(); +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + uuid() +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + aab5d5fd- 70c1-11e5- a4fb- b026b977eb28 +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + Five - separated hexadecimal numbers MySQL uses version 1, The first three numbers are generated from the low, middle, and high parts of a timestamp. 36 characters, inefficient for storage Convert to BINARY(16) datatype, only 16 bytes 31

Improved Support for UUID IS_UUID () /* Check validity */ UUID_TO_BIN (arg1) /* Convert UUID formatted text to binary(16) */ UUID_TO_BIN (arg1, arg2) /* If arg2 = true, shuffle low and high time parts) BIN_TO_UUID (arg1) /* Convert binary(16) to UUID formatted text */ BIN_TO_UUID (arg1, arg2) /* If arg2 = true, shuffle low and high time parts) Ease of use! More efficient indexing Low part of timestamp being first makes index inserts slow Shuffling low part with high part improves index efficiency 32

IPv4 vs IPv6 Getting all the networks that contain the given IP address SELECT inet_ntoa(network) AS network, inet_ntoa(netmask) AS netmask FROM network_table WHERE (inet_aton('192.168.0.30') & netmask) = network; SELECT inet6_ntoa(network) AS network, inet6_ntoa(netmask) AS netmask FROM network_table WHERE (inet6_aton('2001:0db8:85a3:0000:0000:8a2e:0370:7334') & netmask) = network; IPv4 address commonly stored as INT IPv6 address 128 bit, commonly stored as BINARY(16) The secondary query would give wrong results in 5.7, because & operator converts both operands from VARBINARY to BIGINT 33

Bitwise Operations on Binary Data Types Bit operators and functions: & ^ ~ << >> BIT_COUNT Aggregate bit functions: BIT_AND BIT_XOR BIT_OR Prior to 8.0, bitwise operations only worked on integer, truncation beyond 64 bits 8.0: Extends bit- wise operations to work with BINARY/BLOB 8.0: More functions to be added... 34

Improved Support for IPv6 Use BINARY[16] and Bitwise- operations SET @cidr = '2606:b400:85c:1040::1/64'; # Extract network length SET @net_len = SUBSTRING_INDEX(@cidr, '/', - 1 ); SELECT @net_len; - > 64 # Netmask SET @net_mask= ~INET6_ATON('::') << (128 - @net_len); SELECT INET6_NTOA(@net_mask); - > ffff:ffff:ffff:ffff:: SET @network = INET6_ATON(SUBSTRING_INDEX(@cidr, '/', 1 )) & @net_mask; # Print it in human- readable format SELECT INET6_NTOA(@network); - > 2606:b400:85c:1040:: 35

Improved Support for IPv6 #host mask SET @host_mask= ~INET6_ATON('::') >> @net_len; SELECT INET6_NTOA(@host_mask); - > ::ffff:ffff:ffff:ffff # Generate network range IPv6 address. SET @range_start = @network & @net_mask; SELECT INET6_NTOA(@range_start); - > 2606:b400:85c:1040:: SET @range_to = @network @host_mask; SELECT INET6_NTOA(@range_to); - > 2606:b400:85c:1040:ffff:ffff:ffff:ffff #Check if address in the range SET @address1 = INET6_ATON('2606:b400:85c:1040:c5e4:2bb8:cc09:c4e2 ;) SELECT @address1 between @range_start and @range_to: - > 1 36

What is on Our Roadmap? Advanced JSON functions Cost model improvements: histogram, data in memory vs disk Advanced SQL features e.g WINDOWING functions Improve prepared statement/cache query plan Optimization for GIS operations

38