SlideShare a Scribd company logo
Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
LATERAL Derived Tables in MySQL 8.0
Norvald H. Ryeng
Software Development Senior Manager
MySQL Optimizer Team
May 29, 2019
2Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
LATERAL what?
3Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Derived Tables
● Derived tables are subqueries in FROM clauses
SELECT … FROM t1, (subquery) AS derived, t2 …
● Two execution methods
– Materialized
– Merged into the outer query
4Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
5Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
LATERAL Derived Tables
● Can refer to other tables in the same FROM clause
SELECT … FROM t1, LATERAL (SELECT … FROM … WHERE … = t1.col) AS derived, t2 …
– Only to tables that appear before it in the FROM clause
– Including other derived tables
● Two execution methods
– Materialized
– Merged into the outer query
● SQL feature T491
6Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
LATERAL Derived Tables
● Can refer to other tables in the same FROM clause
SELECT … FROM t1, LATERAL (SELECT … FROM … WHERE … = t1.col) AS derived, t2 …
– Only to tables that appear before it in the FROM clause
– Including other derived tables
● Two execution methods
– Materialized
– Merged into the outer query
● SQL feature T491
✓
7Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Implicitly LATERAL Table Functions
● Table functions are implicitly LATERAL
– Not allowed to explicitly specify LATERAL
● MySQL has one table function: JSON_TABLE
SELECT people.*
FROM t1,
JSON_TABLE(t1.json_col, '$.people[*]' COLUMNS (
name VARCHAR(40) PATH '$.name',
address VARCHAR(100) PATH '$.address'))
AS people;
8Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
LATERAL Without Lateral References
● Declared as LATERAL, but contains no lateral references
SELECT … FROM t1, LATERAL (SELECT 1) AS derived, t2 …
– Optimized by MySQL as if LATERAL was not present
– No performance penalty
● Don't add LATERAL to all your derived tables!
– Won't get any warning when accidentaly using a lateral reference
– Confusing the next person to read your query
9Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Examples, please!
10Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Example Setup
CREATE TABLE cities (
city_name VARCHAR(40),
population BIGINT,
country_name VARCHAR(40)
);
INSERT INTO cities VALUES
('Shanghai', 24183300, 'China'),
('Beijing', 20794000, 'China'),
…
;
11Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
The Largest City of Each Country, Option 1
SELECT dt.population, dt.city_name, c.country_name
FROM
(SELECT DISTINCT country_name FROM cities) AS c,
LATERAL (
SELECT city_name, population
FROM cities
WHERE cities.country_name = c.country_name
ORDER BY population DESC
LIMIT 1
) AS dt;
12Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
The Largest City of Each Country, Option 2
SELECT dt.pop, dt2.city_name, dt.country_name
FROM
(
SELECT country_name, MAX(population) AS pop
FROM cities
GROUP BY country_name
) AS dt,
LATERAL (
SELECT city_name
FROM cities
WHERE cities.country_name = dt.country_name
AND cities.population = dt.pop
) AS dt2;
13Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
The Largest City of Each Country, No LATERAL!
SELECT dt.pop, cities.city_name, dt.country_name
FROM
(
SELECT country_name, MAX(population) AS pop
FROM cities
GROUP BY country_name
) AS dt
JOIN cities
ON cities.country_name = dt.country_name
AND cities.population = dt.pop;
14Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
The Largest City of Each Country, No LATERAL!
SELECT dt.pop, cities.city_name, dt.country_name
FROM
(
SELECT country_name, MAX(population) AS pop
FROM cities
GROUP BY country_name
) AS dt
JOIN cities
ON cities.country_name = dt.country_name
AND cities.population = dt.pop;
This is how MySQL rewrites
the previous query after
merging the lateral derived
table into the outer query.
15Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Feature descriptions and design details
directly from the source.
http://mysqlserverteam.com/
16Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
17Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
The preceding 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.
LATERAL Derived Tables in MySQL 8.0

More Related Content

What's hot (20)

PDF
Ctes percona live_2017
Guilhem Bichot
 
PDF
Grouping & Summarizing Data in R
Jeffrey Breen
 
PDF
4 R Tutorial DPLYR Apply Function
Sakthi Dasans
 
PDF
pandas - Python Data Analysis
Andrew Henshaw
 
PDF
Hive Functions Cheat Sheet
Hortonworks
 
PDF
Introduction to Data Mining with R and Data Import/Export in R
Yanchang Zhao
 
PDF
RMySQL Tutorial For Beginners
Rsquared Academy
 
PDF
R Programming: Importing Data In R
Rsquared Academy
 
PDF
3 R Tutorial Data Structure
Sakthi Dasans
 
PDF
Export Data using R Studio
Rupak Roy
 
PDF
Data profiling with Apache Calcite
Julian Hyde
 
PDF
Introduction to Pandas and Time Series Analysis [PyCon DE]
Alexander Hendorf
 
PPT
SQL Server 2008 Performance Enhancements
infusiondev
 
PPTX
R Get Started I
Sankhya_Analytics
 
PDF
5 R Tutorial Data Visualization
Sakthi Dasans
 
PPTX
R Get Started II
Sankhya_Analytics
 
PDF
Next Generation Programming in R
Florian Uhlitz
 
PDF
Basic Tutorial of Association Mapping by Avjinder Kaler
Avjinder (Avi) Kaler
 
PPTX
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Serban Tanasa
 
PPT
Myth busters - performance tuning 101 2007
paulguerin
 
Ctes percona live_2017
Guilhem Bichot
 
Grouping & Summarizing Data in R
Jeffrey Breen
 
4 R Tutorial DPLYR Apply Function
Sakthi Dasans
 
pandas - Python Data Analysis
Andrew Henshaw
 
Hive Functions Cheat Sheet
Hortonworks
 
Introduction to Data Mining with R and Data Import/Export in R
Yanchang Zhao
 
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Programming: Importing Data In R
Rsquared Academy
 
3 R Tutorial Data Structure
Sakthi Dasans
 
Export Data using R Studio
Rupak Roy
 
Data profiling with Apache Calcite
Julian Hyde
 
Introduction to Pandas and Time Series Analysis [PyCon DE]
Alexander Hendorf
 
SQL Server 2008 Performance Enhancements
infusiondev
 
R Get Started I
Sankhya_Analytics
 
5 R Tutorial Data Visualization
Sakthi Dasans
 
R Get Started II
Sankhya_Analytics
 
Next Generation Programming in R
Florian Uhlitz
 
Basic Tutorial of Association Mapping by Avjinder Kaler
Avjinder (Avi) Kaler
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Serban Tanasa
 
Myth busters - performance tuning 101 2007
paulguerin
 

Similar to LATERAL Derived Tables in MySQL 8.0 (20)

PDF
18c and 19c features for DBAs
Connor McDonald
 
PDF
Latin America Tour 2019 - 18c and 19c featues
Connor McDonald
 
PDF
AskTOM Office Hours on Database Triggers
Steven Feuerstein
 
PDF
MySQL Optimizer: What’s New in 8.0
oysteing
 
PPTX
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
PPTX
Statements,joins and operators in sql by thanveer danish melayi(1)
Muhammed Thanveer M
 
PPTX
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Connor McDonald
 
PDF
Reading The Source Code of Presto
Taro L. Saito
 
PDF
MySQL 8.0: Common Table Expressions
oysteing
 
PPTX
Sangam 18 - The New Optimizer in Oracle 12c
Connor McDonald
 
PPTX
Melbourne Groundbreakers Tour - Upgrading without risk
Connor McDonald
 
PDF
MySQL 8.0: Common Table Expressions
oysteing
 
PPT
Single-Row Functions in orcale Data base
Salman Memon
 
PPSX
Oracle Training in Kochi | Trivandrum |Thrissur
IndiaOptions Softwares
 
PPT
Les20[1]Working with Composite Datatypes
siavosh kaviani
 
PDF
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
oysteing
 
PPTX
MySQL Optimizer: What's New in 8.0
Manyi Lu
 
PPT
Introduction to Standard Query Language.ppt
HajarMeseehYaseen
 
18c and 19c features for DBAs
Connor McDonald
 
Latin America Tour 2019 - 18c and 19c featues
Connor McDonald
 
AskTOM Office Hours on Database Triggers
Steven Feuerstein
 
MySQL Optimizer: What’s New in 8.0
oysteing
 
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
Statements,joins and operators in sql by thanveer danish melayi(1)
Muhammed Thanveer M
 
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Connor McDonald
 
Reading The Source Code of Presto
Taro L. Saito
 
MySQL 8.0: Common Table Expressions
oysteing
 
Sangam 18 - The New Optimizer in Oracle 12c
Connor McDonald
 
Melbourne Groundbreakers Tour - Upgrading without risk
Connor McDonald
 
MySQL 8.0: Common Table Expressions
oysteing
 
Single-Row Functions in orcale Data base
Salman Memon
 
Oracle Training in Kochi | Trivandrum |Thrissur
IndiaOptions Softwares
 
Les20[1]Working with Composite Datatypes
siavosh kaviani
 
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
oysteing
 
MySQL Optimizer: What's New in 8.0
Manyi Lu
 
Introduction to Standard Query Language.ppt
HajarMeseehYaseen
 
Ad

Recently uploaded (20)

PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Ad

LATERAL Derived Tables in MySQL 8.0

  • 1. Copyright © 2019 Oracle and/or its affiliates. All rights reserved. LATERAL Derived Tables in MySQL 8.0 Norvald H. Ryeng Software Development Senior Manager MySQL Optimizer Team May 29, 2019
  • 2. 2Copyright © 2019 Oracle and/or its affiliates. All rights reserved. LATERAL what?
  • 3. 3Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Derived Tables ● Derived tables are subqueries in FROM clauses SELECT … FROM t1, (subquery) AS derived, t2 … ● Two execution methods – Materialized – Merged into the outer query
  • 4. 4Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
  • 5. 5Copyright © 2019 Oracle and/or its affiliates. All rights reserved. LATERAL Derived Tables ● Can refer to other tables in the same FROM clause SELECT … FROM t1, LATERAL (SELECT … FROM … WHERE … = t1.col) AS derived, t2 … – Only to tables that appear before it in the FROM clause – Including other derived tables ● Two execution methods – Materialized – Merged into the outer query ● SQL feature T491
  • 6. 6Copyright © 2019 Oracle and/or its affiliates. All rights reserved. LATERAL Derived Tables ● Can refer to other tables in the same FROM clause SELECT … FROM t1, LATERAL (SELECT … FROM … WHERE … = t1.col) AS derived, t2 … – Only to tables that appear before it in the FROM clause – Including other derived tables ● Two execution methods – Materialized – Merged into the outer query ● SQL feature T491 ✓
  • 7. 7Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Implicitly LATERAL Table Functions ● Table functions are implicitly LATERAL – Not allowed to explicitly specify LATERAL ● MySQL has one table function: JSON_TABLE SELECT people.* FROM t1, JSON_TABLE(t1.json_col, '$.people[*]' COLUMNS ( name VARCHAR(40) PATH '$.name', address VARCHAR(100) PATH '$.address')) AS people;
  • 8. 8Copyright © 2019 Oracle and/or its affiliates. All rights reserved. LATERAL Without Lateral References ● Declared as LATERAL, but contains no lateral references SELECT … FROM t1, LATERAL (SELECT 1) AS derived, t2 … – Optimized by MySQL as if LATERAL was not present – No performance penalty ● Don't add LATERAL to all your derived tables! – Won't get any warning when accidentaly using a lateral reference – Confusing the next person to read your query
  • 9. 9Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Examples, please!
  • 10. 10Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Example Setup CREATE TABLE cities ( city_name VARCHAR(40), population BIGINT, country_name VARCHAR(40) ); INSERT INTO cities VALUES ('Shanghai', 24183300, 'China'), ('Beijing', 20794000, 'China'), … ;
  • 11. 11Copyright © 2019 Oracle and/or its affiliates. All rights reserved. The Largest City of Each Country, Option 1 SELECT dt.population, dt.city_name, c.country_name FROM (SELECT DISTINCT country_name FROM cities) AS c, LATERAL ( SELECT city_name, population FROM cities WHERE cities.country_name = c.country_name ORDER BY population DESC LIMIT 1 ) AS dt;
  • 12. 12Copyright © 2019 Oracle and/or its affiliates. All rights reserved. The Largest City of Each Country, Option 2 SELECT dt.pop, dt2.city_name, dt.country_name FROM ( SELECT country_name, MAX(population) AS pop FROM cities GROUP BY country_name ) AS dt, LATERAL ( SELECT city_name FROM cities WHERE cities.country_name = dt.country_name AND cities.population = dt.pop ) AS dt2;
  • 13. 13Copyright © 2019 Oracle and/or its affiliates. All rights reserved. The Largest City of Each Country, No LATERAL! SELECT dt.pop, cities.city_name, dt.country_name FROM ( SELECT country_name, MAX(population) AS pop FROM cities GROUP BY country_name ) AS dt JOIN cities ON cities.country_name = dt.country_name AND cities.population = dt.pop;
  • 14. 14Copyright © 2019 Oracle and/or its affiliates. All rights reserved. The Largest City of Each Country, No LATERAL! SELECT dt.pop, cities.city_name, dt.country_name FROM ( SELECT country_name, MAX(population) AS pop FROM cities GROUP BY country_name ) AS dt JOIN cities ON cities.country_name = dt.country_name AND cities.population = dt.pop; This is how MySQL rewrites the previous query after merging the lateral derived table into the outer query.
  • 15. 15Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Feature descriptions and design details directly from the source. http://mysqlserverteam.com/
  • 16. 16Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
  • 17. 17Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Safe Harbor Statement The preceding 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.