Modern SQL: Evolution of a dinosaur @MarkusWinand @ModernSQL - - PowerPoint PPT Presentation

modern sql
SMART_READER_LITE
LIVE PREVIEW

Modern SQL: Evolution of a dinosaur @MarkusWinand @ModernSQL - - PowerPoint PPT Presentation

Modern SQL: Evolution of a dinosaur @MarkusWinand @ModernSQL http://www.almaden.ibm.com/cs/people/chamberlin/sequel-1974.pdf Modern SQL: Evolution of a dinosaur @MarkusWinand @ModernSQL


slide-1
SLIDE 1

Modern SQL:

Evolution of a dinosaur

@MarkusWinand • @ModernSQL

http://www.almaden.ibm.com/cs/people/chamberlin/sequel-1974.pdf

slide-2
SLIDE 2

Modern SQL:

Evolution of a dinosaur

@MarkusWinand • @ModernSQL

http://www.almaden.ibm.com/cs/people/chamberlin/sequel-1974.pdf

slide-3
SLIDE 3
slide-4
SLIDE 4

1974 1992

slide-5
SLIDE 5

SQL-92 — Tied to the Relational Idea

slide-6
SLIDE 6

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)
slide-7
SLIDE 7

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)

Atom image: https://commons.wikimedia.org/wiki/File:Stylised_atom_with_three_Bohr_model_orbits_and_stylised_nucleus.png

slide-8
SLIDE 8

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)

A B C

Atom image: https://commons.wikimedia.org/wiki/File:Stylised_atom_with_three_Bohr_model_orbits_and_stylised_nucleus.png

slide-9
SLIDE 9

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)

A B C

slide-10
SLIDE 10

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)
  • Schema independent of


processing purposes

  • “Normalization”

A B C

slide-11
SLIDE 11

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)
  • Schema independent of


processing purposes

  • “Normalization”

A B C C D B E

slide-12
SLIDE 12

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)
  • Schema independent of


processing purposes

  • “Normalization”

Relational Operations

  • Transform data for


each particular
 processing purposes

  • JOIN, UNION, nesting, …

A B C C D B E A B C D E

slide-13
SLIDE 13

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)
  • Schema independent of


processing purposes

  • “Normalization”

Relational Operations

  • Transform data for


each particular
 processing purposes

  • JOIN, UNION, nesting, …

A B C C D B E

A B C D E

A B E

slide-14
SLIDE 14

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)
  • Schema independent of


processing purposes

  • “Normalization”

Relational Operations

  • Transform data for


each particular
 processing purposes

  • JOIN, UNION, nesting, …

A B C C D B E

A B C D E A B E

C D E

slide-15
SLIDE 15

SQL-92 — Tied to the Relational Idea

Relational Data Model

  • “Atomic” types (domain)
  • Schema independent of


processing purposes

  • “Normalization”

Relational Operations

  • Transform data for


each particular
 processing purposes

  • JOIN, UNION, nesting, …

A B C C D B E

A B C D E A B E C D E

slide-16
SLIDE 16

1992 1999

slide-17
SLIDE 17

https://www.wiscorp.com/DBMS_-_GreatNews-TheRelationalModelIsDead_-_paper_-_sam.pdf

slide-18
SLIDE 18

SQL:1999 — Escaping the Relational Cage

To say that these SQL:1999 extensions are mere 
 “extended interpretations” of the relational data model
 is like saying that an intercontinental ballistic missile is
 merely an “extended interpretation” of a spear.

https://www.wiscorp.com/DBMS_-_GreatNews-TheRelationalModelIsDead_-_paper_-_sam.pdf

slide-19
SLIDE 19

SQL:1999 — Escaping the Relational Cage

To say that these SQL:1999 extensions are mere 
 “extended interpretations” of the relational data model
 is like saying that an intercontinental ballistic missile is
 merely an “extended interpretation” of a spear. With SQL/99 you can get the best of both worlds and


  • f course, you can get the worst of both worlds.


It’s up to the database practitioners to do the right thing.

https://www.wiscorp.com/DBMS_-_GreatNews-TheRelationalModelIsDead_-_paper_-_sam.pdf

slide-20
SLIDE 20

SQL:1999 — Escaping the Relational Cage

slide-21
SLIDE 21

Relational Model?

SQL:1999 — Escaping the Relational Cage

slide-22
SLIDE 22

Relational Model?

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

I was as confused as anyone else

slide-23
SLIDE 23

Relational Model?

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

?

I was as confused as anyone else

slide-24
SLIDE 24

Relational Model?

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

? I was as confused as anyone else By the early 1990s, however,
 I’d seen the light

slide-25
SLIDE 25

Relational Model?

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

? I was as confused as anyone else By the early 1990s, however,
 I’d seen the light

Domains Can Contain Anything!

slide-26
SLIDE 26

Relational Model?

  • Introduced rich types

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

? I was as confused as anyone else By the early 1990s, however,
 I’d seen the light

Domains Can Contain Anything!

slide-27
SLIDE 27

Relational Model?

  • Introduced rich types

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

A

? I was as confused as anyone else By the early 1990s, however,
 I’d seen the light

Domains Can Contain Anything!

slide-28
SLIDE 28

Relational Model?

  • Introduced rich types
  • arrays

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

A B

[ , ] [ ] []

? I was as confused as anyone else By the early 1990s, however,
 I’d seen the light

Domains Can Contain Anything!

slide-29
SLIDE 29

Relational Model?

  • Introduced rich types
  • arrays
  • Nested tables (multiset)

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

A B

[ , ] [ ] []

C

C D C D C D

? I was as confused as anyone else By the early 1990s, however,
 I’d seen the light

Domains Can Contain Anything!

slide-30
SLIDE 30

Relational Model?

  • Introduced rich types
  • arrays
  • Nested tables (multiset)
  • composite types (objects)

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

A B C D

[ , ]

{x: , y: }

[ ]

{x: , y: }

[]

{x: , y: }

C D C D C D

? I was as confused as anyone else By the early 1990s, however,
 I’d seen the light

Domains Can Contain Anything!

slide-31
SLIDE 31

Relational Model?

  • Introduced rich types
  • arrays
  • Nested tables (multiset)
  • composite types (objects)

Non-Relational Operations

  • Introduced recursive


queries that process
 their own output

  • Transitive closure

Chris Date

Date on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

? I was as confused as anyone else By the early 1990s, however,
 I’d seen the light

Domains Can Contain Anything!

slide-32
SLIDE 32

SQL:1999 — Recursion

CREATE TABLE t ( id INTEGER, parent INTEGER, )

slide-33
SLIDE 33

SQL:1999 — Recursion

CREATE TABLE t ( id INTEGER, parent INTEGER, )

slide-34
SLIDE 34

SQL:1999 — Recursion

CREATE TABLE t ( id INTEGER, parent INTEGER, )

slide-35
SLIDE 35

SQL:1999 — Recursion

slide-36
SLIDE 36

SQL:1999 — Recursion

slide-37
SLIDE 37

SQL:1999 — Recursion

SELECT t.id, t.parent FROM t WHERE t.id = ?

slide-38
SLIDE 38

SQL:1999 — Recursion

SELECT t.id, t.parent FROM t WHERE t.id = ? UNION ALL SELECT t.id, t.parent FROM t WHERE t.parent = ?

slide-39
SLIDE 39

SQL:1999 — Recursion

SELECT t.id, t.parent FROM t WHERE t.id = ? UNION ALL SELECT t.id, t.parent FROM t WHERE t.parent = ?

slide-40
SLIDE 40

SQL:1999 — Recursion

SELECT t.id, t.parent FROM t WHERE t.id = ? UNION ALL SELECT t.id, t.parent FROM t WHERE t.parent = ?

slide-41
SLIDE 41

SQL:1999 — Recursion

WITH RECURSIVE prev (id, parent) AS ( )
 SELECT t.id, t.parent FROM t WHERE t.id = ? UNION ALL SELECT t.id, t.parent FROM t JOIN prev ON t.parent = prev.id SELECT * FROM prev

slide-42
SLIDE 42

SQL:1999 — Recursion

WITH RECURSIVE prev (id, parent) AS ( )
 SELECT t.id, t.parent FROM t WHERE t.id = ? UNION ALL SELECT t.id, t.parent FROM t JOIN prev ON t.parent = prev.id SELECT * FROM prev

slide-43
SLIDE 43

SQL:1999 — Recursion

WITH RECURSIVE prev (id, parent) AS ( )
 SELECT t.id, t.parent FROM t WHERE t.id = ? UNION ALL SELECT t.id, t.parent FROM t JOIN prev ON t.parent = prev.id SELECT * FROM prev

slide-44
SLIDE 44

SQL:1999 — Recursion

1999 2001 2003 2005 2007 2009 2011 2013 2015 2017

5.1 10.2

MariaDB

8.0

MySQL

8.4

PostgreSQL

3.8.3

[0]

SQLite

7.0

DB2 LUW

11gR2

Oracle

2005

SQL Server

[0]Only for top-level SELECT statements

slide-45
SLIDE 45

1999 2003

slide-46
SLIDE 46

http://www.acm.org:80/sigmod/record/issues/0206/standard.pdf (via Wayback machine)

slide-47
SLIDE 47

SQL:2003 — Schemaless & Analytical

Schemaless

  • Introduced XML
  • Non-uniform


documents in
 a single column

slide-48
SLIDE 48

SQL:2003 — Schemaless & Analytical

Schemaless

  • Introduced XML
  • Non-uniform


documents in
 a single column

Later:

  • JSON added with SQL:2016
  • Proprietary JSON support:
  • 2012: PostgreSQL
  • 2014: Oracle
  • 2015: MySQL
  • 2016: SQL Server
slide-49
SLIDE 49

SQL:2003 — Schemaless & Analytical

Analytical

  • Introduced


window functions

  • Accessing other rows

  • f the current result

Schemaless

  • Introduced XML
  • Non-uniform


documents in
 a single column

Later:

  • JSON added with SQL:2016
  • Proprietary JSON support:
  • 2012: PostgreSQL
  • 2014: Oracle
  • 2015: MySQL
  • 2016: SQL Server
slide-50
SLIDE 50

SQL:2003 — Schemaless & Analytical

Analytical

  • Introduced


window functions

  • Accessing other rows

  • f the current result

Schemaless

  • Introduced XML
  • Non-uniform


documents in
 a single column

Later:

  • Extended in SQL:2011
  • Popular among “New SQLs”
  • 2013: BigQuery, Hive
  • 2014: Impala
  • 2015: Spark SQL
  • 2016: NuoDB, MemSQL,

Cockroach DB, VoltDB

Later:

  • JSON added with SQL:2016
  • Proprietary JSON support:
  • 2012: PostgreSQL
  • 2014: Oracle
  • 2015: MySQL
  • 2016: SQL Server
slide-51
SLIDE 51

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

SQL:2003 — Analytical

slide-52
SLIDE 52

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

slide-53
SLIDE 53

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

+10

slide-54
SLIDE 54

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

+10 +30 +20 +70 +40 +20

slide-55
SLIDE 55

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

, SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-56
SLIDE 56

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

ORDER BY id , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-57
SLIDE 57

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

ORDER BY id , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-58
SLIDE 58

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-59
SLIDE 59

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-60
SLIDE 60

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-61
SLIDE 61

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-62
SLIDE 62

SELECT id, value FROM t

id 1 2 3 4 5 6 value +10 +20

  • 10

+50

  • 30
  • 20

bal

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-63
SLIDE 63

SELECT id, value FROM t

id value bal 1 +10 2 +20 3

  • 10

4 +50 5

  • 30

6

  • 20

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-64
SLIDE 64

SELECT id, value FROM t

id value bal 1 +10 2 +20 3

  • 10

4 +50 5

  • 30

6

  • 20

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-65
SLIDE 65

SELECT id, value FROM t

id value bal 1 +10 2 +20 3

  • 10

4 +50 5

  • 30

6

  • 20

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-66
SLIDE 66

SELECT id, value FROM t

id value bal 1 +10 2 +20 3

  • 10

4 +50 5

  • 30

6

  • 20

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-67
SLIDE 67

SELECT id, value FROM t

id value bal 1 +10 2 +20 3

  • 10

4 +50 5

  • 30

6

  • 20

SQL:2003 — Analytical

ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , SUM(value) OVER ( ) bal

+10 +30 +20 +70 +40 +20

slide-68
SLIDE 68

SQL:2003 — Analytical

1999 2001 2003 2005 2007 2009 2011 2013 2015 2017

5.1 10.2

MariaDB

8.0

MySQL

8.4

PostgreSQL

3.25.0

SQLite

7.0

DB2 LUW

8i

Oracle

2005[0] 2012

SQL Server

[0]No framing

slide-69
SLIDE 69

2003 2016

slide-70
SLIDE 70

SQL:2016 — JSON

http://standards.iso.org/ittf/PubliclyAvailableStandards/c067367_ISO_IEC_TR_19075-6_2017.zip

slide-71
SLIDE 71

SQL:2016 — JSON

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

http://standards.iso.org/ittf/PubliclyAvailableStandards/c067367_ISO_IEC_TR_19075-6_2017.zip

slide-72
SLIDE 72

SQL:2016 — JSON

id a1 42 foo 43 bar [ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

http://standards.iso.org/ittf/PubliclyAvailableStandards/c067367_ISO_IEC_TR_19075-6_2017.zip

slide-73
SLIDE 73

SELECT * FROM JSON_TABLE ( ? , '$[*]' COLUMNS ( id INT PATH '$.id' , a1 VARCHAR(…) PATH '$.a1' ) ) r

SQL:2016 — JSON_TABLE

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

id a1 42 foo 43 bar

slide-74
SLIDE 74

SELECT * FROM JSON_TABLE ( ? , '$[*]' COLUMNS ( id INT PATH '$.id' , a1 VARCHAR(…) PATH '$.a1' ) ) r

SQL:2016 — JSON_TABLE

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

id a1 42 foo 43 bar

Bind Parameter

slide-75
SLIDE 75

SELECT * FROM JSON_TABLE ( ? , '$[*]' COLUMNS ( id INT PATH '$.id' , a1 VARCHAR(…) PATH '$.a1' ) ) r

SQL:2016 — JSON_TABLE

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

id a1 42 foo 43 bar

SQL/JSON Path

  • Query language to

select elements from a JSON document

  • Defined in the


SQL standard

Bind Parameter

slide-76
SLIDE 76

SELECT * FROM JSON_TABLE ( ? , '$[*]' COLUMNS ( id INT PATH '$.id' , a1 VARCHAR(…) PATH '$.a1' ) ) r

SQL:2016 — JSON_TABLE

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

id a1 42 foo 43 bar

SQL/JSON Path

  • Query language to

select elements from a JSON document

  • Defined in the


SQL standard

Bind Parameter

slide-77
SLIDE 77

SELECT * FROM JSON_TABLE ( ? , '$[*]' COLUMNS ( id INT PATH '$.id' , a1 VARCHAR(…) PATH '$.a1' ) ) r

SQL:2016 — JSON_TABLE

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

id a1 42 foo 43 bar

SQL/JSON Path

  • Query language to

select elements from a JSON document

  • Defined in the


SQL standard

Bind Parameter

slide-78
SLIDE 78

SQL:2016 — JSON_TABLE — Use Case

SELECT * FROM JSON_TABLE ( ? , '$[*]' COLUMNS ( id INT PATH '$.id' , a1 VARCHAR(…) PATH '$.a1' ) ) r

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

id a1 42 foo 43 bar

slide-79
SLIDE 79

SQL:2016 — JSON_TABLE — Use Case

SELECT * FROM JSON_TABLE ( ? , '$[*]' COLUMNS ( id INT PATH '$.id' , a1 VARCHAR(…) PATH '$.a1' ) ) r INSERT INTO target_table

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

id a1 42 foo 43 bar

slide-80
SLIDE 80

SQL:2016 — JSON_TABLE — Use Case

SELECT * FROM JSON_TABLE ( ? , '$[*]' COLUMNS ( id INT PATH '$.id' , a1 VARCHAR(…) PATH '$.a1' ) ) r INSERT INTO target_table

[ { "id": 42, "a1": "foo" }, { "id": 43, "a1": "bar" } ]

id a1 42 foo 43 bar

Session tip: How Well Do Relational Database Engines Support JSON? Today 15:30!

slide-81
SLIDE 81

SQL:2016 — JSON_TABLE

1999 2001 2003 2005 2007 2009 2011 2013 2015 2017

MariaDB

8.0

MySQL PostgreSQL SQLite DB2 LUW

12cR1

Oracle

2016[0]

SQL Server

[0]OPENJSON provides similar functionality

slide-82
SLIDE 82
slide-83
SLIDE 83

MATCH_RECOGNIZE

(Row Pattern Recognition)

slide-84
SLIDE 84

Time 30 minutes

Example: Logfile

SQL:2016 — Pattern Matching

slide-85
SLIDE 85

Example: Logfile

Time 30 minutes

Session 1 Session 2 Session 3 Session 4

SQL:2016 — Pattern Matching

slide-86
SLIDE 86

Time 30 minutes

SQL:2016 — Pattern Matching

slide-87
SLIDE 87

SELECT count(*) sessions, avg(duration) avg_duration FROM (SELECT MAX(ts) - MIN(ts) duration FROM (SELECT ts, SUM(grp_start) OVER(ORDER BY ts) session_no FROM (SELECT ts, CASE WHEN ts >= LAG( ts, 1, DATE'1900-01-01' ) OVER( ORDER BY ts ) + INTERVAL '30' minute THEN 1 END grp_start FROM log ) tagged ) numbered GROUP BY session_no ) grouped

Time 30 minutes

SQL:2016 — Pattern Matching

slide-88
SLIDE 88

SELECT count(*) sessions, avg(duration) avg_duration FROM (SELECT MAX(ts) - MIN(ts) duration FROM (SELECT ts, SUM(grp_start) OVER(ORDER BY ts) session_no FROM (SELECT ts, CASE WHEN ts >= LAG( ts, 1, DATE'1900-01-01' ) OVER( ORDER BY ts ) + INTERVAL '30' minute THEN 1 END grp_start FROM log ) tagged ) numbered GROUP BY session_no ) grouped

Time 30 minutes

Start-of-group tags

SQL:2016 — Pattern Matching

slide-89
SLIDE 89

SELECT count(*) sessions, avg(duration) avg_duration FROM (SELECT MAX(ts) - MIN(ts) duration FROM (SELECT ts, SUM(grp_start) OVER(ORDER BY ts) session_no FROM (SELECT ts, CASE WHEN ts >= LAG( ts, 1, DATE'1900-01-01' ) OVER( ORDER BY ts ) + INTERVAL '30' minute THEN 1 END grp_start FROM log ) tagged ) numbered GROUP BY session_no ) grouped

Time 30 minutes

Start-of-group tags

SQL:2016 — Pattern Matching

slide-90
SLIDE 90

SELECT count(*) sessions, avg(duration) avg_duration FROM (SELECT MAX(ts) - MIN(ts) duration FROM (SELECT ts, SUM(grp_start) OVER(ORDER BY ts) session_no FROM (SELECT ts, CASE WHEN ts >= LAG( ts, 1, DATE'1900-01-01' ) OVER( ORDER BY ts ) + INTERVAL '30' minute THEN 1 END grp_start FROM log ) tagged ) numbered GROUP BY session_no ) grouped

Time 30 minutes

number sessions

SQL:2016 — Pattern Matching

slide-91
SLIDE 91

SELECT count(*) sessions, avg(duration) avg_duration FROM (SELECT MAX(ts) - MIN(ts) duration FROM (SELECT ts, SUM(grp_start) OVER(ORDER BY ts) session_no FROM (SELECT ts, CASE WHEN ts >= LAG( ts, 1, DATE'1900-01-01' ) OVER( ORDER BY ts ) + INTERVAL '30' minute THEN 1 END grp_start FROM log ) tagged ) numbered GROUP BY session_no ) grouped

Time 30 minutes

number sessions 2222 2 33 3 44 4 2 3 4 1

SQL:2016 — Pattern Matching

slide-92
SLIDE 92

.\S*

SQL:2016 — Pattern Matching

slide-93
SLIDE 93

.\S*

Regular Expression

SQL:2016 — Pattern Matching

slide-94
SLIDE 94

any character non-white space

.\S*

{

Rail track diagram by regexper.com

SQL:2016 — Pattern Matching

slide-95
SLIDE 95

any character non-white space

.\S*

{

{

Rail track diagram by regexper.com

SQL:2016 — Pattern Matching

slide-96
SLIDE 96

any character non-white space any character non-white space

.\S*

{

{

{

Rail track diagram by regexper.com

SQL:2016 — Pattern Matching

slide-97
SLIDE 97

SELECT COUNT(*) sessions, AVG(duration) avg_duration FROM log MATCH_RECOGNIZE( ORDER BY ts MEASURES LAST(ts) - FIRST(ts) AS duration ONE ROW PER MATCH PATTERN ( any cont* ) DEFINE cont AS ts < PREV(ts) + INTERVAL '30' MINUTE ) t

Time 30 minutes

Oracle doesn’t support avg on intervals — query doesn’t work as shown

SQL:2016 — Pattern Matching

slide-98
SLIDE 98

SELECT COUNT(*) sessions, AVG(duration) avg_duration FROM log MATCH_RECOGNIZE( ORDER BY ts MEASURES LAST(ts) - FIRST(ts) AS duration ONE ROW PER MATCH PATTERN ( any cont* ) DEFINE cont AS ts < PREV(ts) + INTERVAL '30' MINUTE ) t

Time 30 minutes

define
 continued

Oracle doesn’t support avg on intervals — query doesn’t work as shown

SQL:2016 — Pattern Matching

slide-99
SLIDE 99

SELECT COUNT(*) sessions, AVG(duration) avg_duration FROM log MATCH_RECOGNIZE( ORDER BY ts MEASURES LAST(ts) - FIRST(ts) AS duration ONE ROW PER MATCH PATTERN ( any cont* ) DEFINE cont AS ts < PREV(ts) + INTERVAL '30' MINUTE ) t

Time 30 minutes

any number


  • f “cont”


rows

Oracle doesn’t support avg on intervals — query doesn’t work as shown

SQL:2016 — Pattern Matching

slide-100
SLIDE 100

SELECT COUNT(*) sessions, AVG(duration) avg_duration FROM log MATCH_RECOGNIZE( ORDER BY ts MEASURES LAST(ts) - FIRST(ts) AS duration ONE ROW PER MATCH PATTERN ( any cont* ) DEFINE cont AS ts < PREV(ts) + INTERVAL '30' MINUTE ) t

Time 30 minutes

Very much
 like GROUP BY

Oracle doesn’t support avg on intervals — query doesn’t work as shown

SQL:2016 — Pattern Matching

slide-101
SLIDE 101

SELECT COUNT(*) sessions, AVG(duration) avg_duration FROM log MATCH_RECOGNIZE( ORDER BY ts MEASURES LAST(ts) - FIRST(ts) AS duration ONE ROW PER MATCH PATTERN ( any cont* ) DEFINE cont AS ts < PREV(ts) + INTERVAL '30' MINUTE ) t

Time 30 minutes

Very much
 like SELECT

Oracle doesn’t support avg on intervals — query doesn’t work as shown

SQL:2016 — Pattern Matching

slide-102
SLIDE 102

SQL:2016 — Pattern Matching

1999 2001 2003 2005 2007 2009 2011 2013 2015 2017

MariaDB MySQL PostgreSQL SQLite DB2 LUW

12cR1

Oracle SQL Server

slide-103
SLIDE 103

2011 2016

slide-104
SLIDE 104

SQL:2011 — Time Travelling

http://cs.ulb.ac.be/public/_media/teaching/infoh415/tempfeaturessql2011.pdf

slide-105
SLIDE 105

SQL:2011 — Time Travelling

Application Versioning

  • Dedicated syntax added
  • When did something


happen in the
 real world?

http://cs.ulb.ac.be/public/_media/teaching/infoh415/tempfeaturessql2011.pdf

slide-106
SLIDE 106

SQL:2011 — Time Travelling

Application Versioning

  • Dedicated syntax added
  • When did something


happen in the
 real world?

New syntax (excerpt)

  • FOR PORTION OF in


UPDATE and DELETE

  • WITHOUT OVERLAPS in UNIQUE

constraints & PRIMARY KEYS

  • [IMMEDIATELY] PRECEDES,

OVERLAPS in WHERE,HAVING,…

slide-107
SLIDE 107

SQL:2011 — Time Travelling

System Versioning

  • Fully automatic and


(almost) transparent

  • When did we learn


about something Application Versioning

  • Dedicated syntax added
  • When did something


happen in the
 real world?

New syntax (excerpt)

  • FOR PORTION OF in


UPDATE and DELETE

  • WITHOUT OVERLAPS in UNIQUE

constraints & PRIMARY KEYS

  • [IMMEDIATELY] PRECEDES,

OVERLAPS in WHERE,HAVING,…

slide-108
SLIDE 108

SQL:2011 — Time Travelling

System Versioning

  • Fully automatic and


(almost) transparent

  • When did we learn


about something Application Versioning

  • Dedicated syntax added
  • When did something


happen in the
 real world?

New syntax (excerpt)

  • FOR PORTION OF in


UPDATE and DELETE

  • WITHOUT OVERLAPS in UNIQUE

constraints & PRIMARY KEYS

  • [IMMEDIATELY] PRECEDES,

OVERLAPS in WHERE,HAVING,… Transparent changes, new syntax for queries

  • INSERT, UPDATE & DELETE


use the system time automatically

  • SELECT can use FOR

SYSTEM_TIME AS OF

slide-109
SLIDE 109

SQL:2011 — System Versioning

slide-110
SLIDE 110

SQL:2011 — System Versioning

CREATE TABLE t ( ... , from TIMESTAMP(9) GENERATED ALWAYS
 AS ROW START , till TIMESTAMP(9) GENERATED ALWAYS
 AS ROW END
 , PERIOD FOR SYSTEM_TIME (from, till) ) WITH SYSTEM VERSIONING

slide-111
SLIDE 111

SQL:2011 — System Versioning

slide-112
SLIDE 112

SQL:2011 — System Versioning

INSERT INTO t (id, data)
 VALUES (1 , 'X' )

id data from till 1 X 10:00

slide-113
SLIDE 113

SQL:2011 — System Versioning

INSERT INTO t (id, data)
 VALUES (1 , 'X' )

id data from till 1 X 10:00

UPDATE t SET data = 'Y' WHERE id = 1

id data from till 1 X 10:00 11:00 1 Y 11:00

slide-114
SLIDE 114

SQL:2011 — System Versioning

INSERT INTO t (id, data)
 VALUES (1 , 'X' )

id data from till 1 X 10:00

UPDATE t SET data = 'Y' WHERE id = 1

id data from till 1 X 10:00 11:00 1 Y 11:00

DELETE FROM t WHERE id = 1

id data from till 1 X 10:00 11:00 1 Y 11:00 12:00

slide-115
SLIDE 115

SQL:2011 — System Versioning

id data from till 1 X 10:00 11:00 1 Y 11:00 12:00

slide-116
SLIDE 116

SQL:2011 — System Versioning

id data from till 1 X 10:00 11:00 1 Y 11:00 12:00

SELECT * FROM t

id data from till

slide-117
SLIDE 117

SQL:2011 — System Versioning

id data from till 1 X 10:00 11:00 1 Y 11:00 12:00

SELECT * FROM t

id data from till

SELECT * FROM t FOR SYSTEM_TIME AS OF
 TIMESTAMP'…10:30:00'

id data from till 1 X 10:00 11:00

slide-118
SLIDE 118

SQL:2011 — System Versioning

1999 2001 2003 2005 2007 2009 2011 2013 2015 2017

5.1 10.3

MariaDB MySQL PostgreSQL SQLite

10.1

DB2 LUW

10gR1[0] 11gR1[1]

Oracle

2016

SQL Server

[0]Short term using Flashback. [1]Flashback Archive. Proprietery syntax.

slide-119
SLIDE 119

https://webstore.iec.ch/publication/59685

slide-120
SLIDE 120

A lot has
 happened since SQL-92

slide-121
SLIDE 121

SQL has evolved
 beyond the relational idea A lot has
 happened since SQL-92

slide-122
SLIDE 122

SQL has evolved
 beyond the relational idea If you use SQL for CRUD operations only, you are doing it wrong A lot has
 happened since SQL-92

slide-123
SLIDE 123

SQL has evolved
 beyond the relational idea If you use SQL for CRUD operations only, you are doing it wrong A lot has
 happened since SQL-92

https://modern-sql.com


@ModernSQL by @MarkusWinand

Training:
 https://winand.at/