SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Querying Hierarchical
Data with CTE
ALEXANDER ALDEV
DBaaS Architect
MariaDB Corporation
Common Table Expressions
● SQL:1999 standard
● Temporary named result set
● Supported by: Oracle, MS SQL, PostgreSQL, SQLLite, MySQL, …
● Available in MariaDB since 10.2
● Two types:
○ Non-recursive
○ Recursive
CTE Syntax
WITH engineers AS (
SELECT *
FROM employees
WHERE dept = ‘engineering’
)
SELECT * FROM engineers ...
CTE nameWITH keyword
CTE body
use in query
Similar to Derived Queries
WITH engineers AS (
SELECT *
FROM employees
WHERE dept =
‘engineering’
)
SELECT * FROM engineers ...
SELECT * FROM (
SELECT *
FROM employees
WHERE dept =
‘engineering’
) engineers
...
Use Case: Readability
WITH engineers AS (
SELECT *
FROM employees
WHERE dept = ‘engineering’
),
WITH eu_engineers AS AS (
SELECT * FROM engineers
WHERE country IN (‘BG’,’FI’,’DE’)
)
SELECT COUNT(*) FROM eu_engineers
Use Case: Multiple References to CTE
WITH engineers AS (
SELECT *
FROM employees
WHERE dept = ‘engineering’
)
SELECT * FROM engineers e1
WHERE NOT EXISTS( SELECT 1 FROM engineers e2
WHERE e1.country = e2.country
AND e1.first = e2.first )
Use Case: Year-over-Year comparison
WITH annual_sales AS (
SELECT product, YEAR(invoice_date) AS year,
SUM( amount ) AS total
FROM invoices
GROUP BY 1, 2
)
SELECT y1.year, y1.product, y1.total, y2.total last_yr_total
FROM annual_sales y1
JOIN annual_sales y2
ON y1.year = y2.year + 1
AND y1.product = y2.product
Recursive CTE
● SQL does not handle hierarchical data well
● Trees in format (parent_id, child_id)
● Graphs in format (point 1, point 2)
● Recursive CTEs allow for hierarchical data to be handled
● Also useful for search algorithms where next iteration depends on previous
Recursive CTE Syntax
WITH RECURSIVE ancestors AS (
SELECT * FROM family
WHERE name = ‘Alex’
UNION ALL
SELECT f.* FROM family f, ancestors
WHERE f.parent_id = ancestors.id
) SELECT * FROM ancestors;
SELECT * FROM engineers ...
RECURSIVE keyword
CTE anchor
recursive use
How CTEs work?
● Base execution strategy is to materialize
● Optimizations possible for non-recursive CTE
● Recursive CTEs use this flow:
1. Evaluate anchor expression
2. Evaluate recursive expression -> new data
3. Append new data to result
4. If new data is not-empty goto 2
Example:
Sudoku Solver
Example:
ETL Job Scheduler
THANK YOU!

Weitere ähnliche Inhalte

Was ist angesagt?

Oracle Course
Oracle CourseOracle Course
Oracle Course
rspaike
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
phanleson
 

Was ist angesagt? (20)

Jdbc
JdbcJdbc
Jdbc
 
Become a super modeler
Become a super modelerBecome a super modeler
Become a super modeler
 
Unit 3
Unit 3Unit 3
Unit 3
 
Oracle Course
Oracle CourseOracle Course
Oracle Course
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURES
 
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZEMySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
 
Dbms
DbmsDbms
Dbms
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Dbms
DbmsDbms
Dbms
 
Window functions with SQL Server 2016
Window functions with SQL Server 2016Window functions with SQL Server 2016
Window functions with SQL Server 2016
 
Function
FunctionFunction
Function
 
statement interface
statement interface statement interface
statement interface
 
Semi join
Semi joinSemi join
Semi join
 
Stored procedure in sql server
Stored procedure in sql serverStored procedure in sql server
Stored procedure in sql server
 
R factors
R   factorsR   factors
R factors
 
Aaa ped-8- Data manipulation: Data wrangling, aggregation, and group operations
Aaa ped-8- Data manipulation: Data wrangling, aggregation, and group operationsAaa ped-8- Data manipulation: Data wrangling, aggregation, and group operations
Aaa ped-8- Data manipulation: Data wrangling, aggregation, and group operations
 
1 ddl
1 ddl1 ddl
1 ddl
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Adbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queriesAdbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queries
 

Ähnlich wie Query hierarchical data the easy way, with CTEs

SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQL
Jerry Yang
 
In Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest LoserIn Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest Loser
paulguerin
 

Ähnlich wie Query hierarchical data the easy way, with CTEs (20)

MySQL Query Optimisation 101
MySQL Query Optimisation 101MySQL Query Optimisation 101
MySQL Query Optimisation 101
 
Introduction to the aerospike jdbc driver
Introduction to the aerospike jdbc driverIntroduction to the aerospike jdbc driver
Introduction to the aerospike jdbc driver
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
 
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
Common Table Expressions (CTE) & Window Functions in MySQL 8.0Common Table Expressions (CTE) & Window Functions in MySQL 8.0
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQL
 
Austen x talk
Austen x talkAusten x talk
Austen x talk
 
Java Database Connectivity with JDBC.pptx
Java Database Connectivity with JDBC.pptxJava Database Connectivity with JDBC.pptx
Java Database Connectivity with JDBC.pptx
 
Designing Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.ProtoDesigning Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.Proto
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
 
Sql analytic queries tips
Sql analytic queries tipsSql analytic queries tips
Sql analytic queries tips
 
Custom Star Creation for Ellucain's Enterprise Data Warehouse
Custom Star Creation for Ellucain's Enterprise Data WarehouseCustom Star Creation for Ellucain's Enterprise Data Warehouse
Custom Star Creation for Ellucain's Enterprise Data Warehouse
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select Topics
 
In Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest LoserIn Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest Loser
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearch
 
esProc introduction
esProc introductionesProc introduction
esProc introduction
 
MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0
 

Mehr von MariaDB plc

Mehr von 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
 

Kürzlich hochgeladen

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Kürzlich hochgeladen (20)

%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

Query hierarchical data the easy way, with CTEs

  • 1. Querying Hierarchical Data with CTE ALEXANDER ALDEV DBaaS Architect MariaDB Corporation
  • 2. Common Table Expressions ● SQL:1999 standard ● Temporary named result set ● Supported by: Oracle, MS SQL, PostgreSQL, SQLLite, MySQL, … ● Available in MariaDB since 10.2 ● Two types: ○ Non-recursive ○ Recursive
  • 3. CTE Syntax WITH engineers AS ( SELECT * FROM employees WHERE dept = ‘engineering’ ) SELECT * FROM engineers ... CTE nameWITH keyword CTE body use in query
  • 4. Similar to Derived Queries WITH engineers AS ( SELECT * FROM employees WHERE dept = ‘engineering’ ) SELECT * FROM engineers ... SELECT * FROM ( SELECT * FROM employees WHERE dept = ‘engineering’ ) engineers ...
  • 5. Use Case: Readability WITH engineers AS ( SELECT * FROM employees WHERE dept = ‘engineering’ ), WITH eu_engineers AS AS ( SELECT * FROM engineers WHERE country IN (‘BG’,’FI’,’DE’) ) SELECT COUNT(*) FROM eu_engineers
  • 6. Use Case: Multiple References to CTE WITH engineers AS ( SELECT * FROM employees WHERE dept = ‘engineering’ ) SELECT * FROM engineers e1 WHERE NOT EXISTS( SELECT 1 FROM engineers e2 WHERE e1.country = e2.country AND e1.first = e2.first )
  • 7. Use Case: Year-over-Year comparison WITH annual_sales AS ( SELECT product, YEAR(invoice_date) AS year, SUM( amount ) AS total FROM invoices GROUP BY 1, 2 ) SELECT y1.year, y1.product, y1.total, y2.total last_yr_total FROM annual_sales y1 JOIN annual_sales y2 ON y1.year = y2.year + 1 AND y1.product = y2.product
  • 8. Recursive CTE ● SQL does not handle hierarchical data well ● Trees in format (parent_id, child_id) ● Graphs in format (point 1, point 2) ● Recursive CTEs allow for hierarchical data to be handled ● Also useful for search algorithms where next iteration depends on previous
  • 9. Recursive CTE Syntax WITH RECURSIVE ancestors AS ( SELECT * FROM family WHERE name = ‘Alex’ UNION ALL SELECT f.* FROM family f, ancestors WHERE f.parent_id = ancestors.id ) SELECT * FROM ancestors; SELECT * FROM engineers ... RECURSIVE keyword CTE anchor recursive use
  • 10. How CTEs work? ● Base execution strategy is to materialize ● Optimizations possible for non-recursive CTE ● Recursive CTEs use this flow: 1. Evaluate anchor expression 2. Evaluate recursive expression -> new data 3. Append new data to result 4. If new data is not-empty goto 2

Hinweis der Redaktion

  1. Alex: 20 yrs experience in DWH, analytics and a couple of business functions, currently leading MariaDB’s DBaaS architecture.
  2. We’ll open two minikube consoles and do the following: Deploy a simple “Hello, world” NodeJS application. Use “docker build” and “kubectl create” Start a simple client that measures performance. Use “docker build” and “kubectl create” Scale the application from 1 to 3 and observe throughput. Use “kubectl get pods Fail a number of pods. Use “kubectl delete pod” Upgrade the application to v2.0. Use “kubectl set image”
  3. We’ll open two minikube consoles and do the following: Deploy a simple “Hello, world” NodeJS application. Use “docker build” and “kubectl create” Start a simple client that measures performance. Use “docker build” and “kubectl create” Scale the application from 1 to 3 and observe throughput. Use “kubectl get pods Fail a number of pods. Use “kubectl delete pod” Upgrade the application to v2.0. Use “kubectl set image”
  4. Last slide -- the remaining are backup and will be deleted