SlideShare a Scribd company logo
1 of 60
Download to read offline
Common Table Expressions
Igor Babaev | Principal Software Engineer, MariaDB
Sergei Petrunia | Senior Software Engineer, MariaDB
{igor,sergey}@mariadb.com
2
Common Table Expressions

A standard SQL feature

Two kinds of CTEs
●
Recursive
●
Non-recursive

Supported by Oracle, MS SQL Server, PostgreSQL, SQLite, …

Available in MariaDB 10.2 (Stable since May, 2017)

Available in MySQL 8.0 ( RC since Sept, 2017)
3
Plan

Non-recursive CTEs

Use cases

Optimizations

Recursive CTEs

Basics

Transitive closure

Paths

(Non-)linear recursion

Mutual recursion
4
Plan

Non-recursive CTEs

Use cases

Optimizations

Recursive CTEs

Basics

Transitive closure

Paths

(Non-)linear recursion

Mutual recursion
5
CTE name
CTE Body
CTE Usage
with engineers as (
select *
from employees
where dept='Engineering'
)
select *
from engineers
where ...
WITH
CTE syntax

Similar to DERIVED
tables

“Query-local VIEWs”
6
select *
from
(
select *
from employees
where
dept='Engineering'
) as engineers
where
...
with engineers as (
select *
from employees
where dept='Engineering'
)
select *
from engineers
where
...
CTEs are like derived tables
7
with engineers as (
select * from employees
where dept in ('Development','Support')
),
eu_engineers as (
select * from engineers where country IN ('NL',...)
)
select
...
from
eu_engineers;
Use case #1: CTEs refer to CTEs

More readable than nested FROM(SELECT …)
8
with engineers as (
select * from employees
where dept in ('Development','Support')
),
select *
from
engineers E1
where not exists (select 1
from engineers E2
where E2.country=E1.country
and E2.name <> E1.name);
Use case #2: Multiple uses of CTE

Anti-self-join
9
select *
from
sales_product_year CUR,
sales_product_year PREV,
where
CUR.product=PREV.product and
CUR.year=PREV.year + 1 and
CUR.total_amt > PREV.total_amt
with sales_product_year as (
select
product,
year(ship_date) as year,
sum(price) as total_amt
from
item_sales
group by
product, year
)
Use case #2: example 2

Year-over-year comparisons
10
select *
from
sales_product_year S1
where
total_amt > (select
0.1*sum(total_amt)
from
sales_product_year S2
where
S2.year=S1.year)
with sales_product_year as (
select
product,
year(ship_date) as year,
sum(price) as total_amt
from
item_sales
group by
product, year
)
Use case #2: example 3

Compare individuals against their group
11
Conclusions so far

Non-recursive CTEs are “Query-local VIEWs”

One CTE can refer to another

Better than nested FROM (SELECT …)

Can refer to a CTE from multiple places

Better than copy-pasting FROM(SELECT …)

CTE adoption

TPC-H (1999) - no CTEs

SQL:1999 introduces CTEs

TPC-DS (2011) - 38 of 100 queries use CTEs.
12
Plan

Non-recursive CTEs

Use cases

Optimizations

Recursive CTEs

Basics

Transitive closure

Paths

(Non-)linear recursion

Mutual recursion
13
with engineers as (
select * from employees
where
dept='Engineering' or dept='Support'
)
select
...
from
engineers,
other_table, ...
Base algorithm: materialize in a temporary table

Always works

Often not optimal
14
with engineers as (
select * from employees
where
dept='Development'
)
select
...
from
engineers E,
support_cases SC
where
E.name=SC.assignee and
SC.created='2016-09-30' and
E.location='Amsterdam'
select
...
from
employees E,
support_cases SC
where
E.dept='Development' and
E.name=SC.assignee and
SC.created='2016-09-30' and
E.location='Amsterdam'
Optimization #1: CTE Merging

Join optimizer can pick any plan

e.g. support employee→
15
Optimization #1: CTE Merging (2)

Requirement

CTE is just a JOIN : no GROUP BY, DISTINCT, etc

Output

CTE is merged into parent’s join

Optimizer can pick the best query plan

This is the same as ALGORITHM=MERGE for VIEWs
16
with sales_per_year as (
select
year(order.date) as year
sum(order.amount) as sales
from
order
group by
year
)
select *
from sales_per_year
where
year in ('2015','2016')
with sales_per_year as (
select
year(order.date) as year
sum(order.amount) as sales
from
order
where
year in ('2015','2016')
group by
year
)
select *
from sales_per_year
Optimization #2: condition pushdown
17
Condition pushdown summary

Used when merging is not possible

CTE has a GROUP BY

Makes temp. table smaller

Allows to filter out whole GROUP-BY groups

Besides CTEs, works for derived tables and VIEWs

Based on Galina Shalygina’s GSOC 2016 project:

“Pushing conditions into non-mergeable views and derived
tables in MariaDB”
18
with product_sales as (
select
product_name,
year(sale_date),
count(*) as count
from
product_sales
group by product, year)
select *
from
product_sales P1,
product_sales P2
where
P1.year = 2010 AND
P2.year = 2011 AND ...
Optimization #3: CTE reuse
The idea

Fill the CTE once

Then use multiple times

Interferes with condition
pushdown
19
CTE Merge Condition
pushdown
CTE reuse
MariaDB 10.2 ✔ ✔ ✘
MS SQL Server ✔ ✔ ✘
PostgreSQL ✘ ✘ ✔
MySQL 8.0.0 ✔ ✘ ✔
CTE Optimizations summary

Merge and condition pushdown are most important

MariaDB supports them, like MS SQL.

PostgreSQL’s approach is *weird*

“CTEs are optimization barriers”

MySQL: “try merging, otherwise reuse”
Recursive CTEs
21
Plan

Non-recursive CTEs

Use cases

Optimizations

Recursive CTEs

Basics

Transitive closure

Paths

(Non-)linear recursion

Mutual recursion
22
wheel
boltcapnut
tire valve
rimtirespokes
Chicago
Nashville Atlanta
Orlando
Recursive CTEs

SQL is poor at “recursive” data structures/algorithms

First attempt: Oracle’s CONNECT BY syntax (80’s)

Superseded by Recursive CTEs

SQL:1999, implementations in 2007-2009
●
Trees ●
Graphs
23
Recursive part
Anchor part
Recursive use of CTE
“recursive”
Recursive CTE syntax
with recursive ancestors as (
select * from folks
where name = 'Alex'
union [all]
select f.*
from folks as f, ancestors AS a
where
f.id = a.father or f.id = a.mother
)
select * from ancestors;
24
Sister AmyAlex
Mom Dad
Grandpa Bill
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
| 98 | Sister Amy | 20 | 30 |
+------+--------------+--------+--------+
Recursive CTE computation

Consider a dataset
25
with recursive ancestors as (
select * from folks
where name = 'Alex'
union
select f.*
from folks as f, ancestors AS a
where
f.id = a.father or f.id = a.mother
)
select * from ancestors;
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
+------+--------------+--------+--------+
Result table
Step #1: execute the anchor part
Computation
26
with recursive ancestors as (
select * from folks
where name = 'Alex'
union
select f.*
from folks as f, ancestors AS a
where
f.id = a.father or f.id = a.mother
)
select * from ancestors;
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
+------+--------------+--------+--------+
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
| 98 | Sister Amy | 20 | 30 |
+------+--------------+--------+--------+
Step #2: execute the recursive part
Computation
Result table
27
with recursive ancestors as (
select * from folks
where name = 'Alex'
union
select f.*
from folks as f, ancestors AS a
where
f.id = a.father or f.id = a.mother
)
select * from ancestors;
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
+------+--------------+--------+--------+
Result table
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
| 98 | Sister Amy | 20 | 30 |
+------+--------------+--------+--------+
Step #2: Add results the result table
Computation
28
with recursive ancestors as (
select * from folks
where name = 'Alex'
union
select f.*
from folks as f, ancestors AS a
where
f.id = a.father or f.id = a.mother
)
select * from ancestors;
Result table+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
+------+--------------+--------+--------+
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
| 98 | Sister Amy | 20 | 30 |
+------+--------------+--------+--------+
Computation
Step #3: Execute the recursive part again
29
with recursive ancestors as (
select * from folks
where name = 'Alex'
union
select f.*
from folks as f, ancestors AS a
where
f.id = a.father or f.id = a.mother
)
select * from ancestors;
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
+------+--------------+--------+--------+
Result table
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
| 98 | Sister Amy | 20 | 30 |
+------+--------------+--------+--------+
Computation
●
Step #2: Add results the result table
●
Dad and Mom are already there
30
with recursive ancestors as (
select * from folks
where name = 'Alex'
union
select f.*
from folks as f, ancestors AS a
where
f.id = a.father or f.id = a.mother
)
select * from ancestors;
Result table
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
| 98 | Sister Amy | 20 | 30 |
+------+--------------+--------+--------+
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
+------+--------------+--------+--------+
Computation
Step #4: Execute the recursive part again
31
with recursive ancestors as (
select * from folks
where name = 'Alex'
union
select f.*
from folks as f, ancestors AS a
where
f.id = a.father or f.id = a.mother
)
select * from ancestors;
Result table
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
| 98 | Sister Amy | 20 | 30 |
+------+--------------+--------+--------+
+------+--------------+--------+--------+
| id | name | father | mother |
+------+--------------+--------+--------+
| 100 | Alex | 20 | 30 |
| 20 | Dad | 10 | NULL |
| 30 | Mom | NULL | NULL |
| 10 | Grandpa Bill | NULL | NULL |
+------+--------------+--------+--------+
Computation
●
Step #4: No [new] results
●
The process finishes.
32
1. Compute anchor_part
2. Compute recursive_part
to get the new data
3. if (new data is non-empty)
goto 2;
with recursive R as (
select anchor_part
union [all]
select recursive_part
from R, …
)
select …
Summary so far
33
Plan

Non-recursive CTEs

Use cases

Optimizations

Recursive CTEs

Basics

Transitive closure

Paths

(Non-)linear recursion

Mutual recursion
34
bus_routes
+------------+------------+
| origin | dst |
+------------+------------+
| New York | Boston |
| Boston | New York |
| New York | Washington |
| Washington | Boston |
| Washington | Raleigh |
+------------+------------+
New York
Boston Washington
Raleigh
Transitive closure
35
with recursive bus_dst as
(
select origin as dst
from bus_routes
where origin='New York'
union
select bus_routes.dst
from bus_dst, bus_routes
where
bus_dst.dst= bus_routes.origin
)
select * from bus_dst
Transitive closure
New York
Boston Washington
Raleigh
● bus_dst is where one can be
● Start from New York (with a datatype trick)
36
with recursive bus_dst as
(
select origin as dst
from bus_routes
where origin='New York'
union
select bus_routes.dst
from bus_dst, bus_routes
where
bus_dst.dst= bus_routes.origin
)
select * from bus_dst
Transitive closure
New York
Boston Washington
Raleigh
● Put into the work table
+------------+
| dst |
+------------+
| New York |
+------------+
37
with recursive bus_dst as
(
select origin as dst
from bus_routes
where origin='New York'
union
select bus_routes.dst
from bus_dst, bus_routes
where
bus_dst.dst= bus_routes.origin
)
select * from bus_dst
+------------+
| dst |
+------------+
| New York |
+------------+
Transitive closure
New York
Boston Washington
Raleigh
● Join bus_dst with bus_routes.
● New destinations: Boston, Washington
38
with recursive bus_dst as
(
select origin as dst
from bus_routes
where origin='New York'
union
select bus_routes.dst
from bus_routes, bus_dst
where
bus_dst.dst= bus_routes.origin
)
select * from bus_dst
+------------+
| dst |
+------------+
| New York |
| Boston |
| Washington |
+------------+
Transitive closure
New York
Boston Washington
Raleigh
● Add new destinations to the temp. table
39
with recursive bus_dst as
(
select origin as dst
from bus_routes
where origin='New York'
union
select bus_routes.dst
from bus_routes, bus_dst
where
bus_dst.dst= bus_routes.origin
)
select * from bus_dst
+------------+
| dst |
+------------+
| New York |
| Boston |
| Washington |
+------------+
Transitive closure
New York
Boston Washington
Raleigh
●
Join bus_dst with bus_routes
– Raleigh, Boston, New York
40
with recursive bus_dst as
(
select origin as dst
from bus_routes
where origin='New York'
union
select bus_routes.dst
from bus_routes, bus_dst
where
bus_dst.dst= bus_routes.origin
)
select * from bus_dst
+------------+
| dst |
+------------+
| New York |
| Boston |
| Washington |
| Raleigh |
+------------+
Transitive closure
New York
Boston Washington
Raleigh
●
Join bus_dst with bus_routes
– Raleigh, Boston, New York
41
Summary so far

Can compute transitive closure

UNION prevents loops.
New York
Boston Washington
Raleigh
42
Plan

Non-recursive CTEs

Use cases

Optimizations

Recursive CTEs

Basics

Transitive closure

Paths

(Non-)linear recursion

Mutual recursion
43
bus_routes
+------------+------------+
| origin | dst |
+------------+------------+
| New York | Boston |
| Boston | New York |
| New York | Washington |
| Washington | Boston |
| Washington | Raleigh |
+------------+------------+
Computing “Paths”
New York
Boston Washington
Raleigh
●
Want paths like New York Washington Raleigh→ →
44
with recursive paths (cur_path, cur_dest) as
(
select origin, origin
from bus_routes
where origin='New York'
union
select
concat(paths.cur_path, ',',
bus_routes.dest),
bus_routes.dest
from paths, bus_routes
where
paths.cur_dest= bus_routes.origin and
locate(bus_routes.dest, paths.cur_path)=0
)
select * from paths
Computing “Paths”
New York
Boston Washington
Raleigh
Collect a path
Don’t construct loops
45
select
concat(paths.cur_path, ',',
bus_routes.dest),
bus_routes.dest
from paths, bus_routes
where
paths.cur_dest= bus_routes.origin and
locate(bus_routes.dest, paths.cur_path)=0
+-----------------------------+------------+
| cur_path | cur_dest |
+-----------------------------+------------+
| New York | New York |
| New York,Boston | Boston |
| New York,Washington | Washington |
| New York,Washington,Boston | Boston |
| New York,Washington,Raleigh | Raleigh |
+-----------------------------+------------+
Computing “Paths”
New York
Boston Washington
Raleigh
46
How recursion stops

Tree or Directed Acyclic Graph walking

Execution is guaranteed to stop

Computing transitive closure

Use UNION

Computing “Paths” over graph with loops

Put condition into WHERE to stop loops/growth

Safety measure: @@max_recursive_iterations

Like in SQL Server

MySQL-8.0: @@cte_max_recursion_depth
47
Plan

Non-recursive CTEs

Use cases

Optimizations

Recursive CTEs

Basics

Transitive closure

Paths

(Non-)linear recursion

Mutual recursion
48
with recursive R as (
select anchor_part
union [all]
select recursive_part
from R, …
)
select …
[Non-]linear recursion
The SQL standard requires
that recursion is linear:

recursive_part must refer to
R only once

No self-joins

Not from subqueries

Not from inner side of an
outer join

...
49
R
x a
+y
+b
Linearity of SELECT statements
50
with recursive R as (
select anchor_part
union [all]
select recursive_part
from R, …
)
select …
Linear recursion

New data is generated by “wave-front” elements

Contents of R are always growing
51
Plan

Non-recursive CTEs

Use cases

Optimizations

Recursive CTEs

Basics

Transitive closure

Paths

(Non-)linear recursion

Mutual recursion
52
with recursive C1 as (
select …
from anchor_table
union
select …
from C2
),
C2 as (
select …
from C1
)
select ...
Mutual recursion

Multiple CTEs refer to each
other

Useful for “bi-partite” graphs

MariaDB supports it

No other database does
53
Modules and objects
M1
v3
v9v9 v4

A module consumes objects and produces other objects
v
objects
v3
v9
v4
...
m
modules
m1
...
m1
...
(m, v)
m1,v3
...
m1,v9
module_arguments
module_results
(m, v)
m1,v4
...
54
Modules and objects
M1 M2 M3
v3
v9v9 v4
v7 v1
v6 v10

A module consumes objects and produces other objects
55
Modules and objects

What objects can be produced from objects v3, v9, v7
M1 M2 M3
v3
v9v9 v4
v7 v1
v6 v10
56
Query part #1: objects produced from modules
with recursive
reached_objects as
(
select v, "init"
from objects
where v in ('v3','v7','v9')
union
select module_results.v, module_results.m
from module_results, applied_modules
where module_results.m = applied_modules.m
),
57
Query part #2: modules ready to be applied
applied_modules as
(
select * from modules where 1=0
union
select modules.m
from
modules
where
not exists (select * from module_arguments
where module_arguments.m = modules.m and
module_arguments.v not in
(select v from reached_objects))
)
select * from reached_objects;
58
Query result
+------+------+
| v | init |
+------+------+
| v3 | init |
| v7 | init |
| v9 | init |
| v4 | m1 |
| v1 | m2 |
| v6 | m2 |
| v10 | m3 |
+------+------+
59
Conclusions

MariaDB 10.2 has Common Table Expressions

Both Recursive and Non-recursive are supported

MariaDB 10.3 add EXCEPT and INTERSECT
- Recursive references in EXCEPT and INTERSECT are allowed

Non-recursive

“Query-local VIEWs”

Competitive set of query optimizations

Recursive

Useful for tree/graph-walking queries

Mutual and non-linear recursions are supported.
Thanks!
Q&A

More Related Content

What's hot

Limitless and recursion-free recursion limits!
Limitless and recursion-free recursion limits!Limitless and recursion-free recursion limits!
Limitless and recursion-free recursion limits!akaptur
 
ゲーム理論BASIC 第40回 -仁-
ゲーム理論BASIC 第40回 -仁-ゲーム理論BASIC 第40回 -仁-
ゲーム理論BASIC 第40回 -仁-ssusere0a682
 
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-ssusere0a682
 
ゲーム理論BASIC 第42回 -仁に関する定理の証明3-
ゲーム理論BASIC 第42回 -仁に関する定理の証明3-ゲーム理論BASIC 第42回 -仁に関する定理の証明3-
ゲーム理論BASIC 第42回 -仁に関する定理の証明3-ssusere0a682
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine LearningBig_Data_Ukraine
 
ゲーム理論BASIC 第39回 -交渉集合とカーネル-
ゲーム理論BASIC 第39回 -交渉集合とカーネル-ゲーム理論BASIC 第39回 -交渉集合とカーネル-
ゲーム理論BASIC 第39回 -交渉集合とカーネル-ssusere0a682
 
Digital signal processing (2nd ed) (mitra) solution manual
Digital signal processing (2nd ed) (mitra) solution manualDigital signal processing (2nd ed) (mitra) solution manual
Digital signal processing (2nd ed) (mitra) solution manualRamesh Sundar
 

What's hot (12)

Date and time functions in mysql
Date and time functions in mysqlDate and time functions in mysql
Date and time functions in mysql
 
Oracle sql ppt2
Oracle sql ppt2Oracle sql ppt2
Oracle sql ppt2
 
Integral table
Integral tableIntegral table
Integral table
 
tsoulkas_cumulants
tsoulkas_cumulantstsoulkas_cumulants
tsoulkas_cumulants
 
Limitless and recursion-free recursion limits!
Limitless and recursion-free recursion limits!Limitless and recursion-free recursion limits!
Limitless and recursion-free recursion limits!
 
ゲーム理論BASIC 第40回 -仁-
ゲーム理論BASIC 第40回 -仁-ゲーム理論BASIC 第40回 -仁-
ゲーム理論BASIC 第40回 -仁-
 
MySQL Functions
MySQL FunctionsMySQL Functions
MySQL Functions
 
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
 
ゲーム理論BASIC 第42回 -仁に関する定理の証明3-
ゲーム理論BASIC 第42回 -仁に関する定理の証明3-ゲーム理論BASIC 第42回 -仁に関する定理の証明3-
ゲーム理論BASIC 第42回 -仁に関する定理の証明3-
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
ゲーム理論BASIC 第39回 -交渉集合とカーネル-
ゲーム理論BASIC 第39回 -交渉集合とカーネル-ゲーム理論BASIC 第39回 -交渉集合とカーネル-
ゲーム理論BASIC 第39回 -交渉集合とカーネル-
 
Digital signal processing (2nd ed) (mitra) solution manual
Digital signal processing (2nd ed) (mitra) solution manualDigital signal processing (2nd ed) (mitra) solution manual
Digital signal processing (2nd ed) (mitra) solution manual
 

Similar to M|18 Taking Advantage of Common Table Expressions

Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Sergey Petrunya
 
Optimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesOptimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesSergey Petrunya
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and OperatorsMohan Kumar.R
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.pptKISHOYIANKISH
 
Cat database
Cat databaseCat database
Cat databasetubbeles
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQLGeorgi Sotirov
 
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 Design and Develop SQL DDL statements which demonstrate the use of SQL objec... Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...bhavesh lande
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesDamien Seguy
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLTen Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLanandology
 
Using PostgreSQL statistics to optimize performance
Using PostgreSQL statistics to optimize performance Using PostgreSQL statistics to optimize performance
Using PostgreSQL statistics to optimize performance Alexey Ermakov
 
MySQL 8.0 Preview: What Is Coming?
MySQL 8.0 Preview: What Is Coming?MySQL 8.0 Preview: What Is Coming?
MySQL 8.0 Preview: What Is Coming?Gabriela Ferrara
 
Most useful queries
Most useful queriesMost useful queries
Most useful queriesSam Depp
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL IndexingMYXPLAIN
 
Demystifying cost based optimization
Demystifying cost based optimizationDemystifying cost based optimization
Demystifying cost based optimizationRiyaj Shamsudeen
 
Mysql alter-command
Mysql alter-commandMysql alter-command
Mysql alter-commandbeben benzy
 
k-means Clustering and Custergram with R
k-means Clustering and Custergram with Rk-means Clustering and Custergram with R
k-means Clustering and Custergram with RDr. Volkan OBAN
 

Similar to M|18 Taking Advantage of Common Table Expressions (20)

Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2
 
Optimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesOptimizer features in recent releases of other databases
Optimizer features in recent releases of other databases
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
 
Cat database
Cat databaseCat database
Cat database
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
 
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 Design and Develop SQL DDL statements which demonstrate the use of SQL objec... Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queries
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLTen Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
 
Hanya contoh saja dari xampp
Hanya contoh saja dari xamppHanya contoh saja dari xampp
Hanya contoh saja dari xampp
 
Sql select
Sql select Sql select
Sql select
 
Using PostgreSQL statistics to optimize performance
Using PostgreSQL statistics to optimize performance Using PostgreSQL statistics to optimize performance
Using PostgreSQL statistics to optimize performance
 
My sql1
My sql1My sql1
My sql1
 
My SQL
My SQLMy SQL
My SQL
 
MySQL 8.0 Preview: What Is Coming?
MySQL 8.0 Preview: What Is Coming?MySQL 8.0 Preview: What Is Coming?
MySQL 8.0 Preview: What Is Coming?
 
Most useful queries
Most useful queriesMost useful queries
Most useful queries
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL Indexing
 
Demystifying cost based optimization
Demystifying cost based optimizationDemystifying cost based optimization
Demystifying cost based optimization
 
Mysql alter-command
Mysql alter-commandMysql alter-command
Mysql alter-command
 
k-means Clustering and Custergram with R
k-means Clustering and Custergram with Rk-means Clustering and Custergram with R
k-means Clustering and Custergram with R
 

More from MariaDB plc

MariaDB Paris Workshop 2023 - MaxScale 23.02.x
MariaDB Paris Workshop 2023 - MaxScale 23.02.xMariaDB Paris Workshop 2023 - MaxScale 23.02.x
MariaDB Paris Workshop 2023 - MaxScale 23.02.xMariaDB plc
 
MariaDB Paris Workshop 2023 - Newpharma
MariaDB Paris Workshop 2023 - NewpharmaMariaDB Paris Workshop 2023 - Newpharma
MariaDB Paris Workshop 2023 - NewpharmaMariaDB plc
 
MariaDB Paris Workshop 2023 - Cloud
MariaDB Paris Workshop 2023 - CloudMariaDB Paris Workshop 2023 - Cloud
MariaDB Paris Workshop 2023 - CloudMariaDB plc
 
MariaDB Paris Workshop 2023 - MariaDB Enterprise
MariaDB Paris Workshop 2023 - MariaDB EnterpriseMariaDB Paris Workshop 2023 - MariaDB Enterprise
MariaDB Paris Workshop 2023 - MariaDB EnterpriseMariaDB plc
 
MariaDB Paris Workshop 2023 - Performance Optimization
MariaDB Paris Workshop 2023 - Performance OptimizationMariaDB Paris Workshop 2023 - Performance Optimization
MariaDB Paris Workshop 2023 - Performance OptimizationMariaDB plc
 
MariaDB Paris Workshop 2023 - MaxScale
MariaDB Paris Workshop 2023 - MaxScale MariaDB Paris Workshop 2023 - MaxScale
MariaDB Paris Workshop 2023 - MaxScale MariaDB plc
 
MariaDB Paris Workshop 2023 - novadys presentation
MariaDB Paris Workshop 2023 - novadys presentationMariaDB Paris Workshop 2023 - novadys presentation
MariaDB Paris Workshop 2023 - novadys presentationMariaDB plc
 
MariaDB Paris Workshop 2023 - DARVA presentation
MariaDB Paris Workshop 2023 - DARVA presentationMariaDB Paris Workshop 2023 - DARVA presentation
MariaDB Paris Workshop 2023 - DARVA presentationMariaDB plc
 
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server MariaDB plc
 
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-BackupMariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-BackupMariaDB plc
 
Einführung : MariaDB Tech und Business Update Hamburg 2023
Einführung : MariaDB Tech und Business Update Hamburg 2023Einführung : MariaDB Tech und Business Update Hamburg 2023
Einführung : MariaDB Tech und Business Update Hamburg 2023MariaDB plc
 
Hochverfügbarkeitslösungen mit MariaDB
Hochverfügbarkeitslösungen mit MariaDBHochverfügbarkeitslösungen mit MariaDB
Hochverfügbarkeitslösungen mit MariaDBMariaDB plc
 
Die Neuheiten in MariaDB Enterprise Server
Die Neuheiten in MariaDB Enterprise ServerDie Neuheiten in MariaDB Enterprise Server
Die Neuheiten in MariaDB Enterprise ServerMariaDB plc
 
Global Data Replication with Galera for Ansell Guardian®
Global Data Replication with Galera for Ansell Guardian®Global Data Replication with Galera for Ansell Guardian®
Global Data Replication with Galera for Ansell Guardian®MariaDB plc
 
Introducing workload analysis
Introducing workload analysisIntroducing workload analysis
Introducing workload analysisMariaDB plc
 
Under the hood: SkySQL monitoring
Under the hood: SkySQL monitoringUnder the hood: SkySQL monitoring
Under the hood: SkySQL monitoringMariaDB plc
 
Introducing the R2DBC async Java connector
Introducing the R2DBC async Java connectorIntroducing the R2DBC async Java connector
Introducing the R2DBC async Java connectorMariaDB plc
 
MariaDB Enterprise Tools introduction
MariaDB Enterprise Tools introductionMariaDB Enterprise Tools introduction
MariaDB Enterprise Tools introductionMariaDB plc
 
Faster, better, stronger: The new InnoDB
Faster, better, stronger: The new InnoDBFaster, better, stronger: The new InnoDB
Faster, better, stronger: The new InnoDBMariaDB plc
 
The architecture of SkySQL
The architecture of SkySQLThe architecture of SkySQL
The architecture of SkySQLMariaDB plc
 

More from MariaDB plc (20)

MariaDB Paris Workshop 2023 - MaxScale 23.02.x
MariaDB Paris Workshop 2023 - MaxScale 23.02.xMariaDB Paris Workshop 2023 - MaxScale 23.02.x
MariaDB Paris Workshop 2023 - MaxScale 23.02.x
 
MariaDB Paris Workshop 2023 - Newpharma
MariaDB Paris Workshop 2023 - NewpharmaMariaDB Paris Workshop 2023 - Newpharma
MariaDB Paris Workshop 2023 - Newpharma
 
MariaDB Paris Workshop 2023 - Cloud
MariaDB Paris Workshop 2023 - CloudMariaDB Paris Workshop 2023 - Cloud
MariaDB Paris Workshop 2023 - Cloud
 
MariaDB Paris Workshop 2023 - MariaDB Enterprise
MariaDB Paris Workshop 2023 - MariaDB EnterpriseMariaDB Paris Workshop 2023 - MariaDB Enterprise
MariaDB Paris Workshop 2023 - MariaDB Enterprise
 
MariaDB Paris Workshop 2023 - Performance Optimization
MariaDB Paris Workshop 2023 - Performance OptimizationMariaDB Paris Workshop 2023 - Performance Optimization
MariaDB Paris Workshop 2023 - Performance Optimization
 
MariaDB Paris Workshop 2023 - MaxScale
MariaDB Paris Workshop 2023 - MaxScale MariaDB Paris Workshop 2023 - MaxScale
MariaDB Paris Workshop 2023 - MaxScale
 
MariaDB Paris Workshop 2023 - novadys presentation
MariaDB Paris Workshop 2023 - novadys presentationMariaDB Paris Workshop 2023 - novadys presentation
MariaDB Paris Workshop 2023 - novadys presentation
 
MariaDB Paris Workshop 2023 - DARVA presentation
MariaDB Paris Workshop 2023 - DARVA presentationMariaDB Paris Workshop 2023 - DARVA presentation
MariaDB Paris Workshop 2023 - DARVA presentation
 
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
 
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-BackupMariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
 
Einführung : MariaDB Tech und Business Update Hamburg 2023
Einführung : MariaDB Tech und Business Update Hamburg 2023Einführung : MariaDB Tech und Business Update Hamburg 2023
Einführung : MariaDB Tech und Business Update Hamburg 2023
 
Hochverfügbarkeitslösungen mit MariaDB
Hochverfügbarkeitslösungen mit MariaDBHochverfügbarkeitslösungen mit MariaDB
Hochverfügbarkeitslösungen mit MariaDB
 
Die Neuheiten in MariaDB Enterprise Server
Die Neuheiten in MariaDB Enterprise ServerDie Neuheiten in MariaDB Enterprise Server
Die Neuheiten in MariaDB Enterprise Server
 
Global Data Replication with Galera for Ansell Guardian®
Global Data Replication with Galera for Ansell Guardian®Global Data Replication with Galera for Ansell Guardian®
Global Data Replication with Galera for Ansell Guardian®
 
Introducing workload analysis
Introducing workload analysisIntroducing workload analysis
Introducing workload analysis
 
Under the hood: SkySQL monitoring
Under the hood: SkySQL monitoringUnder the hood: SkySQL monitoring
Under the hood: SkySQL monitoring
 
Introducing the R2DBC async Java connector
Introducing the R2DBC async Java connectorIntroducing the R2DBC async Java connector
Introducing the R2DBC async Java connector
 
MariaDB Enterprise Tools introduction
MariaDB Enterprise Tools introductionMariaDB Enterprise Tools introduction
MariaDB Enterprise Tools introduction
 
Faster, better, stronger: The new InnoDB
Faster, better, stronger: The new InnoDBFaster, better, stronger: The new InnoDB
Faster, better, stronger: The new InnoDB
 
The architecture of SkySQL
The architecture of SkySQLThe architecture of SkySQL
The architecture of SkySQL
 

Recently uploaded

SMOTE and K-Fold Cross Validation-Presentation.pptx
SMOTE and K-Fold Cross Validation-Presentation.pptxSMOTE and K-Fold Cross Validation-Presentation.pptx
SMOTE and K-Fold Cross Validation-Presentation.pptxHaritikaChhatwal1
 
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxmodul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxaleedritatuxx
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxMike Bennett
 
Decoding Patterns: Customer Churn Prediction Data Analysis Project
Decoding Patterns: Customer Churn Prediction Data Analysis ProjectDecoding Patterns: Customer Churn Prediction Data Analysis Project
Decoding Patterns: Customer Churn Prediction Data Analysis ProjectBoston Institute of Analytics
 
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Boston Institute of Analytics
 
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfblazblazml
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksdeepakthakur548787
 
Rithik Kumar Singh codealpha pythohn.pdf
Rithik Kumar Singh codealpha pythohn.pdfRithik Kumar Singh codealpha pythohn.pdf
Rithik Kumar Singh codealpha pythohn.pdfrahulyadav957181
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Cathrine Wilhelmsen
 
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelDecoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelBoston Institute of Analytics
 
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...Amil Baba Dawood bangali
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Seán Kennedy
 
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Boston Institute of Analytics
 
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...Jack Cole
 
Real-Time AI Streaming - AI Max Princeton
Real-Time AI  Streaming - AI Max PrincetonReal-Time AI  Streaming - AI Max Princeton
Real-Time AI Streaming - AI Max PrincetonTimothy Spann
 
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024Susanna-Assunta Sansone
 
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...Dr Arash Najmaei ( Phd., MBA, BSc)
 
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Thomas Poetter
 

Recently uploaded (20)

Data Analysis Project: Stroke Prediction
Data Analysis Project: Stroke PredictionData Analysis Project: Stroke Prediction
Data Analysis Project: Stroke Prediction
 
SMOTE and K-Fold Cross Validation-Presentation.pptx
SMOTE and K-Fold Cross Validation-Presentation.pptxSMOTE and K-Fold Cross Validation-Presentation.pptx
SMOTE and K-Fold Cross Validation-Presentation.pptx
 
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxmodul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptx
 
Decoding Patterns: Customer Churn Prediction Data Analysis Project
Decoding Patterns: Customer Churn Prediction Data Analysis ProjectDecoding Patterns: Customer Churn Prediction Data Analysis Project
Decoding Patterns: Customer Churn Prediction Data Analysis Project
 
Insurance Churn Prediction Data Analysis Project
Insurance Churn Prediction Data Analysis ProjectInsurance Churn Prediction Data Analysis Project
Insurance Churn Prediction Data Analysis Project
 
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
 
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing works
 
Rithik Kumar Singh codealpha pythohn.pdf
Rithik Kumar Singh codealpha pythohn.pdfRithik Kumar Singh codealpha pythohn.pdf
Rithik Kumar Singh codealpha pythohn.pdf
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)
 
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelDecoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
 
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...
 
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
 
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
 
Real-Time AI Streaming - AI Max Princeton
Real-Time AI  Streaming - AI Max PrincetonReal-Time AI  Streaming - AI Max Princeton
Real-Time AI Streaming - AI Max Princeton
 
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
 
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
 
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
 

M|18 Taking Advantage of Common Table Expressions

  • 1. Common Table Expressions Igor Babaev | Principal Software Engineer, MariaDB Sergei Petrunia | Senior Software Engineer, MariaDB {igor,sergey}@mariadb.com
  • 2. 2 Common Table Expressions  A standard SQL feature  Two kinds of CTEs ● Recursive ● Non-recursive  Supported by Oracle, MS SQL Server, PostgreSQL, SQLite, …  Available in MariaDB 10.2 (Stable since May, 2017)  Available in MySQL 8.0 ( RC since Sept, 2017)
  • 3. 3 Plan  Non-recursive CTEs  Use cases  Optimizations  Recursive CTEs  Basics  Transitive closure  Paths  (Non-)linear recursion  Mutual recursion
  • 4. 4 Plan  Non-recursive CTEs  Use cases  Optimizations  Recursive CTEs  Basics  Transitive closure  Paths  (Non-)linear recursion  Mutual recursion
  • 5. 5 CTE name CTE Body CTE Usage with engineers as ( select * from employees where dept='Engineering' ) select * from engineers where ... WITH CTE syntax  Similar to DERIVED tables  “Query-local VIEWs”
  • 6. 6 select * from ( select * from employees where dept='Engineering' ) as engineers where ... with engineers as ( select * from employees where dept='Engineering' ) select * from engineers where ... CTEs are like derived tables
  • 7. 7 with engineers as ( select * from employees where dept in ('Development','Support') ), eu_engineers as ( select * from engineers where country IN ('NL',...) ) select ... from eu_engineers; Use case #1: CTEs refer to CTEs  More readable than nested FROM(SELECT …)
  • 8. 8 with engineers as ( select * from employees where dept in ('Development','Support') ), select * from engineers E1 where not exists (select 1 from engineers E2 where E2.country=E1.country and E2.name <> E1.name); Use case #2: Multiple uses of CTE  Anti-self-join
  • 9. 9 select * from sales_product_year CUR, sales_product_year PREV, where CUR.product=PREV.product and CUR.year=PREV.year + 1 and CUR.total_amt > PREV.total_amt with sales_product_year as ( select product, year(ship_date) as year, sum(price) as total_amt from item_sales group by product, year ) Use case #2: example 2  Year-over-year comparisons
  • 10. 10 select * from sales_product_year S1 where total_amt > (select 0.1*sum(total_amt) from sales_product_year S2 where S2.year=S1.year) with sales_product_year as ( select product, year(ship_date) as year, sum(price) as total_amt from item_sales group by product, year ) Use case #2: example 3  Compare individuals against their group
  • 11. 11 Conclusions so far  Non-recursive CTEs are “Query-local VIEWs”  One CTE can refer to another  Better than nested FROM (SELECT …)  Can refer to a CTE from multiple places  Better than copy-pasting FROM(SELECT …)  CTE adoption  TPC-H (1999) - no CTEs  SQL:1999 introduces CTEs  TPC-DS (2011) - 38 of 100 queries use CTEs.
  • 12. 12 Plan  Non-recursive CTEs  Use cases  Optimizations  Recursive CTEs  Basics  Transitive closure  Paths  (Non-)linear recursion  Mutual recursion
  • 13. 13 with engineers as ( select * from employees where dept='Engineering' or dept='Support' ) select ... from engineers, other_table, ... Base algorithm: materialize in a temporary table  Always works  Often not optimal
  • 14. 14 with engineers as ( select * from employees where dept='Development' ) select ... from engineers E, support_cases SC where E.name=SC.assignee and SC.created='2016-09-30' and E.location='Amsterdam' select ... from employees E, support_cases SC where E.dept='Development' and E.name=SC.assignee and SC.created='2016-09-30' and E.location='Amsterdam' Optimization #1: CTE Merging  Join optimizer can pick any plan  e.g. support employee→
  • 15. 15 Optimization #1: CTE Merging (2)  Requirement  CTE is just a JOIN : no GROUP BY, DISTINCT, etc  Output  CTE is merged into parent’s join  Optimizer can pick the best query plan  This is the same as ALGORITHM=MERGE for VIEWs
  • 16. 16 with sales_per_year as ( select year(order.date) as year sum(order.amount) as sales from order group by year ) select * from sales_per_year where year in ('2015','2016') with sales_per_year as ( select year(order.date) as year sum(order.amount) as sales from order where year in ('2015','2016') group by year ) select * from sales_per_year Optimization #2: condition pushdown
  • 17. 17 Condition pushdown summary  Used when merging is not possible  CTE has a GROUP BY  Makes temp. table smaller  Allows to filter out whole GROUP-BY groups  Besides CTEs, works for derived tables and VIEWs  Based on Galina Shalygina’s GSOC 2016 project:  “Pushing conditions into non-mergeable views and derived tables in MariaDB”
  • 18. 18 with product_sales as ( select product_name, year(sale_date), count(*) as count from product_sales group by product, year) select * from product_sales P1, product_sales P2 where P1.year = 2010 AND P2.year = 2011 AND ... Optimization #3: CTE reuse The idea  Fill the CTE once  Then use multiple times  Interferes with condition pushdown
  • 19. 19 CTE Merge Condition pushdown CTE reuse MariaDB 10.2 ✔ ✔ ✘ MS SQL Server ✔ ✔ ✘ PostgreSQL ✘ ✘ ✔ MySQL 8.0.0 ✔ ✘ ✔ CTE Optimizations summary  Merge and condition pushdown are most important  MariaDB supports them, like MS SQL.  PostgreSQL’s approach is *weird*  “CTEs are optimization barriers”  MySQL: “try merging, otherwise reuse”
  • 21. 21 Plan  Non-recursive CTEs  Use cases  Optimizations  Recursive CTEs  Basics  Transitive closure  Paths  (Non-)linear recursion  Mutual recursion
  • 22. 22 wheel boltcapnut tire valve rimtirespokes Chicago Nashville Atlanta Orlando Recursive CTEs  SQL is poor at “recursive” data structures/algorithms  First attempt: Oracle’s CONNECT BY syntax (80’s)  Superseded by Recursive CTEs  SQL:1999, implementations in 2007-2009 ● Trees ● Graphs
  • 23. 23 Recursive part Anchor part Recursive use of CTE “recursive” Recursive CTE syntax with recursive ancestors as ( select * from folks where name = 'Alex' union [all] select f.* from folks as f, ancestors AS a where f.id = a.father or f.id = a.mother ) select * from ancestors;
  • 24. 24 Sister AmyAlex Mom Dad Grandpa Bill +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | | 98 | Sister Amy | 20 | 30 | +------+--------------+--------+--------+ Recursive CTE computation  Consider a dataset
  • 25. 25 with recursive ancestors as ( select * from folks where name = 'Alex' union select f.* from folks as f, ancestors AS a where f.id = a.father or f.id = a.mother ) select * from ancestors; +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | +------+--------------+--------+--------+ Result table Step #1: execute the anchor part Computation
  • 26. 26 with recursive ancestors as ( select * from folks where name = 'Alex' union select f.* from folks as f, ancestors AS a where f.id = a.father or f.id = a.mother ) select * from ancestors; +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | +------+--------------+--------+--------+ +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | | 98 | Sister Amy | 20 | 30 | +------+--------------+--------+--------+ Step #2: execute the recursive part Computation Result table
  • 27. 27 with recursive ancestors as ( select * from folks where name = 'Alex' union select f.* from folks as f, ancestors AS a where f.id = a.father or f.id = a.mother ) select * from ancestors; +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | +------+--------------+--------+--------+ Result table +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | | 98 | Sister Amy | 20 | 30 | +------+--------------+--------+--------+ Step #2: Add results the result table Computation
  • 28. 28 with recursive ancestors as ( select * from folks where name = 'Alex' union select f.* from folks as f, ancestors AS a where f.id = a.father or f.id = a.mother ) select * from ancestors; Result table+------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | +------+--------------+--------+--------+ +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | | 98 | Sister Amy | 20 | 30 | +------+--------------+--------+--------+ Computation Step #3: Execute the recursive part again
  • 29. 29 with recursive ancestors as ( select * from folks where name = 'Alex' union select f.* from folks as f, ancestors AS a where f.id = a.father or f.id = a.mother ) select * from ancestors; +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | +------+--------------+--------+--------+ Result table +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | | 98 | Sister Amy | 20 | 30 | +------+--------------+--------+--------+ Computation ● Step #2: Add results the result table ● Dad and Mom are already there
  • 30. 30 with recursive ancestors as ( select * from folks where name = 'Alex' union select f.* from folks as f, ancestors AS a where f.id = a.father or f.id = a.mother ) select * from ancestors; Result table +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | | 98 | Sister Amy | 20 | 30 | +------+--------------+--------+--------+ +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | +------+--------------+--------+--------+ Computation Step #4: Execute the recursive part again
  • 31. 31 with recursive ancestors as ( select * from folks where name = 'Alex' union select f.* from folks as f, ancestors AS a where f.id = a.father or f.id = a.mother ) select * from ancestors; Result table +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | | 98 | Sister Amy | 20 | 30 | +------+--------------+--------+--------+ +------+--------------+--------+--------+ | id | name | father | mother | +------+--------------+--------+--------+ | 100 | Alex | 20 | 30 | | 20 | Dad | 10 | NULL | | 30 | Mom | NULL | NULL | | 10 | Grandpa Bill | NULL | NULL | +------+--------------+--------+--------+ Computation ● Step #4: No [new] results ● The process finishes.
  • 32. 32 1. Compute anchor_part 2. Compute recursive_part to get the new data 3. if (new data is non-empty) goto 2; with recursive R as ( select anchor_part union [all] select recursive_part from R, … ) select … Summary so far
  • 33. 33 Plan  Non-recursive CTEs  Use cases  Optimizations  Recursive CTEs  Basics  Transitive closure  Paths  (Non-)linear recursion  Mutual recursion
  • 34. 34 bus_routes +------------+------------+ | origin | dst | +------------+------------+ | New York | Boston | | Boston | New York | | New York | Washington | | Washington | Boston | | Washington | Raleigh | +------------+------------+ New York Boston Washington Raleigh Transitive closure
  • 35. 35 with recursive bus_dst as ( select origin as dst from bus_routes where origin='New York' union select bus_routes.dst from bus_dst, bus_routes where bus_dst.dst= bus_routes.origin ) select * from bus_dst Transitive closure New York Boston Washington Raleigh ● bus_dst is where one can be ● Start from New York (with a datatype trick)
  • 36. 36 with recursive bus_dst as ( select origin as dst from bus_routes where origin='New York' union select bus_routes.dst from bus_dst, bus_routes where bus_dst.dst= bus_routes.origin ) select * from bus_dst Transitive closure New York Boston Washington Raleigh ● Put into the work table +------------+ | dst | +------------+ | New York | +------------+
  • 37. 37 with recursive bus_dst as ( select origin as dst from bus_routes where origin='New York' union select bus_routes.dst from bus_dst, bus_routes where bus_dst.dst= bus_routes.origin ) select * from bus_dst +------------+ | dst | +------------+ | New York | +------------+ Transitive closure New York Boston Washington Raleigh ● Join bus_dst with bus_routes. ● New destinations: Boston, Washington
  • 38. 38 with recursive bus_dst as ( select origin as dst from bus_routes where origin='New York' union select bus_routes.dst from bus_routes, bus_dst where bus_dst.dst= bus_routes.origin ) select * from bus_dst +------------+ | dst | +------------+ | New York | | Boston | | Washington | +------------+ Transitive closure New York Boston Washington Raleigh ● Add new destinations to the temp. table
  • 39. 39 with recursive bus_dst as ( select origin as dst from bus_routes where origin='New York' union select bus_routes.dst from bus_routes, bus_dst where bus_dst.dst= bus_routes.origin ) select * from bus_dst +------------+ | dst | +------------+ | New York | | Boston | | Washington | +------------+ Transitive closure New York Boston Washington Raleigh ● Join bus_dst with bus_routes – Raleigh, Boston, New York
  • 40. 40 with recursive bus_dst as ( select origin as dst from bus_routes where origin='New York' union select bus_routes.dst from bus_routes, bus_dst where bus_dst.dst= bus_routes.origin ) select * from bus_dst +------------+ | dst | +------------+ | New York | | Boston | | Washington | | Raleigh | +------------+ Transitive closure New York Boston Washington Raleigh ● Join bus_dst with bus_routes – Raleigh, Boston, New York
  • 41. 41 Summary so far  Can compute transitive closure  UNION prevents loops. New York Boston Washington Raleigh
  • 42. 42 Plan  Non-recursive CTEs  Use cases  Optimizations  Recursive CTEs  Basics  Transitive closure  Paths  (Non-)linear recursion  Mutual recursion
  • 43. 43 bus_routes +------------+------------+ | origin | dst | +------------+------------+ | New York | Boston | | Boston | New York | | New York | Washington | | Washington | Boston | | Washington | Raleigh | +------------+------------+ Computing “Paths” New York Boston Washington Raleigh ● Want paths like New York Washington Raleigh→ →
  • 44. 44 with recursive paths (cur_path, cur_dest) as ( select origin, origin from bus_routes where origin='New York' union select concat(paths.cur_path, ',', bus_routes.dest), bus_routes.dest from paths, bus_routes where paths.cur_dest= bus_routes.origin and locate(bus_routes.dest, paths.cur_path)=0 ) select * from paths Computing “Paths” New York Boston Washington Raleigh Collect a path Don’t construct loops
  • 45. 45 select concat(paths.cur_path, ',', bus_routes.dest), bus_routes.dest from paths, bus_routes where paths.cur_dest= bus_routes.origin and locate(bus_routes.dest, paths.cur_path)=0 +-----------------------------+------------+ | cur_path | cur_dest | +-----------------------------+------------+ | New York | New York | | New York,Boston | Boston | | New York,Washington | Washington | | New York,Washington,Boston | Boston | | New York,Washington,Raleigh | Raleigh | +-----------------------------+------------+ Computing “Paths” New York Boston Washington Raleigh
  • 46. 46 How recursion stops  Tree or Directed Acyclic Graph walking  Execution is guaranteed to stop  Computing transitive closure  Use UNION  Computing “Paths” over graph with loops  Put condition into WHERE to stop loops/growth  Safety measure: @@max_recursive_iterations  Like in SQL Server  MySQL-8.0: @@cte_max_recursion_depth
  • 47. 47 Plan  Non-recursive CTEs  Use cases  Optimizations  Recursive CTEs  Basics  Transitive closure  Paths  (Non-)linear recursion  Mutual recursion
  • 48. 48 with recursive R as ( select anchor_part union [all] select recursive_part from R, … ) select … [Non-]linear recursion The SQL standard requires that recursion is linear:  recursive_part must refer to R only once  No self-joins  Not from subqueries  Not from inner side of an outer join  ...
  • 49. 49 R x a +y +b Linearity of SELECT statements
  • 50. 50 with recursive R as ( select anchor_part union [all] select recursive_part from R, … ) select … Linear recursion  New data is generated by “wave-front” elements  Contents of R are always growing
  • 51. 51 Plan  Non-recursive CTEs  Use cases  Optimizations  Recursive CTEs  Basics  Transitive closure  Paths  (Non-)linear recursion  Mutual recursion
  • 52. 52 with recursive C1 as ( select … from anchor_table union select … from C2 ), C2 as ( select … from C1 ) select ... Mutual recursion  Multiple CTEs refer to each other  Useful for “bi-partite” graphs  MariaDB supports it  No other database does
  • 53. 53 Modules and objects M1 v3 v9v9 v4  A module consumes objects and produces other objects v objects v3 v9 v4 ... m modules m1 ... m1 ... (m, v) m1,v3 ... m1,v9 module_arguments module_results (m, v) m1,v4 ...
  • 54. 54 Modules and objects M1 M2 M3 v3 v9v9 v4 v7 v1 v6 v10  A module consumes objects and produces other objects
  • 55. 55 Modules and objects  What objects can be produced from objects v3, v9, v7 M1 M2 M3 v3 v9v9 v4 v7 v1 v6 v10
  • 56. 56 Query part #1: objects produced from modules with recursive reached_objects as ( select v, "init" from objects where v in ('v3','v7','v9') union select module_results.v, module_results.m from module_results, applied_modules where module_results.m = applied_modules.m ),
  • 57. 57 Query part #2: modules ready to be applied applied_modules as ( select * from modules where 1=0 union select modules.m from modules where not exists (select * from module_arguments where module_arguments.m = modules.m and module_arguments.v not in (select v from reached_objects)) ) select * from reached_objects;
  • 58. 58 Query result +------+------+ | v | init | +------+------+ | v3 | init | | v7 | init | | v9 | init | | v4 | m1 | | v1 | m2 | | v6 | m2 | | v10 | m3 | +------+------+
  • 59. 59 Conclusions  MariaDB 10.2 has Common Table Expressions  Both Recursive and Non-recursive are supported  MariaDB 10.3 add EXCEPT and INTERSECT - Recursive references in EXCEPT and INTERSECT are allowed  Non-recursive  “Query-local VIEWs”  Competitive set of query optimizations  Recursive  Useful for tree/graph-walking queries  Mutual and non-linear recursions are supported.