SlideShare una empresa de Scribd logo
1 de 28
Ranges, Ranges Everywhere!
Stew Ashton (stewashton.wordpress.com)
UKOUG Tech 2016
Can you read the following line? If not, please move closer.
It's much better when you can read the code ;)
Agenda
• Defining ranges
• Relating ranges: gaps, overlaps
• Range DDL: sensible data
• Ranges in one table
• Ranges in two tables
2
Who am I?
• 36 years in IT
– Developer, Technical Sales Engineer, Technical Architect
– Aeronautics, IBM, Finance
– Mainframe, client-server, Web apps
• 12 years using Oracle database
– SQL performance analysis
– Replace Java with SQL
• 4 years as in-house “Oracle Development Expert”
• Conference speaker since 2014
• Currently independent
3
Questions
4
What is a range?
• Two values that can be compared
– Always use the same datatype 
– Comparable datatypes:
• integer, date (without time)
• number, datetime, interval, (n)(var)char
• rowid
• Range design questions:
– Is the "end" value part of the range?
– Are NULLs allowed?
5
Allen’s
Interval
Algebra
6
1 2 3 4
A precedes B 1 2
B preceded by A 3 4
A meets B 1 2
B met by A 2 3
A overlaps B 1 3
B overlapped by A 2 4
A finished by B 1 3
B finishes A 2 3
A contains B 1 4
B during A 2 3
A starts B 1 2
B started by A 1 3
A and B 1 2
are equal 1 2
Meet
Gap
"Overlap"
1 2 3 41 2 3 4
A precedes B 1 2
B preceded by A 3 4
1 2 3 4
A precedes B 1 2
B preceded by A 3 4
A meets B 1 2
B met by A 2 3
End value: Inclusive or Exclusive
• Design must allow ranges to "meet"
• Discrete quantities can be inclusive
– [1-3] meets [4-6] : no intermediate integer
– [Jan. 1-31] meets [Feb. 1-28] : no intermediate date
• Continuous quantities require exclusive
– Most ranges are continuous (including dates, really)
7
Votes for Exclusive end values
• SQL:2013 and Oracle 12c Temporal Validity
– "Period": date/time range
• [Closed-Open): includes start time but not end time
• WIDTH_BUCKET() function
– Puts values in equiwidth histogram
– Buckets must touch
– [Closed-open): upper boundary value goes in higher bucket
• Me!
– Exclusive end values work for every kind of range
– Except: ROWID ranges must be inclusive
8
DDL: make sure data is sensible
• Start_range < End_range
• If date without time, CHECK( dte = trunc(dte))
• If integer, say so
• Is NULL allowed?
– If so, what does it mean?
– Ex. Temporal Validity :
NULL end value means "until the end of time"
• Are overlaps allowed?
9
Overlaps avoided by unique constraints
10
Unique(start,end) Unique(start) Unique(end) 1 2 3 4
No constraint works
A overlaps B 1 3
B overlapped by A 2 4
Y
A finished by B 1 3
B finishes A 2 3
No constraint works
A contains B 1 4
B during A 2 3
Y
A starts B 1 2
B started by A 1 3
Y Y Y
A and B 1 2
are equal 1 2
Avoiding Overlaps: 3 solutions
1. Triggers
– Hard to do right, not very scalable
2. "Refresh on commit" materialized views
– Not scalable?
3. Virtual ranges
11
Virtual range: no gaps, no overlaps
• One column: start value
• End value is calculated:
= next row's start
– Putting identical value in 2
rows is denormalization
• Last row has unlimited
end
• Maybe OK for audit trails?
START_VALUE END_VALUE
16-11-15 08:30 16-11-15 09:30
16-11-15 09:30 16-11-15 18:30
16-11-15 18:30 (null)
12
START_VALUE
16-11-15 08:30
16-11-15 09:30
16-11-15 18:30
Physical (table)
Virtual (view)
Semi-Virtual range: no overlaps
• Start column always used
• End column optional:
– If null, use next row's start
– If not null, use lesser of end
column and next row's start
– Last row can have limited end
• Or: intermediate row with
'not exists' flag
– ≅ Change Data Capture
format
13
START_VALUE END_VALUE
16-11-15 08:30 16-11-15 09:30
16-11-15 18:30 (null)
START_VALUE D
16-11-15 08:30
16-11-15 09:30 D
16-11-15 18:30
Range-related SQL
• Why hard?
– Can't use BETWEEN
– Inequality joins impact performance
– With overlaps, 1 value point can be in any number of rows
– Joining 2 tables with overlaps -> row explosion
– NULLs have special meanings
• Common problems
– Find gaps
– Intersect: find overlaps
– Union: packing ranges between gaps
– Joins
• Today, ends are exclusive, everything is NOT NULL (unless specified)
14
15
FROM_TM TO_TM
07:00 08:00
09:00 10:50
10:00 10:45
12:00 12:45
18:00 23:00
select * from (
select
max (to_tm) over(order by from_tm)
as gap_from,
lead(from_tm) over(order by from_tm)
as gap_to
from t
) where gap_from < gap_to;
select
to_tm
as gap_from,
lead(from_tm) over(order by from_tm)
as gap_to
from t
FROM_TM GAP_FROM GAP_TO
07:00 08:00 09:00
09:00 10:50 10:00
10:00 10:45 12:00
12:00 12:45 18:00
18:00 23:00
GAP_FROM GAP_TO
08:00 09:00
10:50 12:00
12:45 18:00
Gaps, ex. Free time in calendar
16
FROM_TM GAP_FROM GAP_TO
07:00 08:00 09:00
09:00 10:50 10:00
10:00 10:50 12:00
12:00 12:45 18:00
18:00 23:00
Intersect: finding Overlaps
17
Test case Start End
01:precedes 1 2
01:precedes 3 4
02:meets 1 2
02:meets 2 3
03:overlaps 1 3
03:overlaps 2 4
04:finished by 1 3
04:finished by 2 3
05:contains 1 4
05:contains 2 3
06:starts 1 2
06:starts 1 3
07:equals 1 2
07:equals 1 2
select test_case, dte, col
from t
unpivot (dte for col in (
start_date as 1, end_date as -1))
A overlaps B 1 3
B overlapped by A 2 4
1 2
2 3
3 4
select test_case, dte, col
from t
unpivot (dte for col in (
start_date as 1, end_date as -1))
select test_case, dte "Start",
lead(dte,1,dte) over(
partition by test_case
order by dte, col desc
) "End",
sum(col) over(
partition by test_case
order by dte, col desc
) "Rows"
from t
unpivot (dte for col in (
start_date as 1, end_date as -1))
Intersect: finding Overlaps
18
Test case Dte Col
01:precedes 1 1
01:precedes 2 -1
01:precedes 3 1
01:precedes 4 -1
02:meets 1 1
02:meets 2 -1
02:meets 2 1
02:meets 3 -1
03:overlaps 1 1
03:overlaps 3 -1
03:overlaps 2 1
03:overlaps 4 -1
select test_case, dte "Start",
lead(dte,1,dte) over(
partition by test_case
order by dte, col desc
) "End",
sum(col) over(
partition by test_case
order by dte, col desc
) "Rows"
from t
unpivot (dte for col in (
start_date as 1, end_date as -1))
select * from (
select test_case, dte "Start",
lead(dte,1,dte) over(
partition by test_case
order by dte, col desc
) "End",
sum(col) over(
partition by test_case
order by dte, col desc
) "Rows"
from t
unpivot (dte for col in (
start_date as 1, end_date as -1))
) where
"Start" < "End";
Intersect: finding Overlaps
19
Test case Start End Rows
01:precedes 1 2 1
01:precedes 2 3 0
01:precedes 3 4 1
01:precedes 4 4 0
02:meets 1 2 1
02:meets 2 2 2
02:meets 2 3 1
02:meets 3 3 0
03:overlaps 1 2 1
03:overlaps 2 3 2
03:overlaps 3 4 1
03:overlaps 4 4 0
✖
✖
✖
✖
select * from (
select test_case, dte "Start",
lead(dte,1,dte) over(
partition by test_case
order by dte, col desc
) "End",
sum(col) over(
partition by test_case
order by dte, col desc
) "Rows"
from t
unpivot (dte for col in (
start_date as 1, end_date as -1))
) where
"Start" < "End";
select * from (
select test_case, dte "Start",
lead(dte,1,dte) over(
partition by test_case
order by dte, col desc
) "End",
sum(col) over(
partition by test_case
order by dte, col desc
) "Rows"
from t
unpivot (dte for col in (
start_date as 1, end_date as -1))
) where "Rows" > 1
and "Start" < "End";
Intersect: finding Overlaps
20
Test case Start End Rows
01:precedes 1 2 1
01:precedes 2 3 0
01:precedes 3 4 1
02:meets 1 2 1
02:meets 2 3 1
03:overlaps 1 2 1
03:overlaps 2 3 2
03:overlaps 3 4 1
Test case Start End Rows
03:overlaps 2 3 2
04:finished by 2 3 2
05:contains 2 3 2
06:starts 1 2 2
07:equals 1 2 2
Test case Start End
01:precedes 1 2
01:precedes 3 4
02:meets 1 2
02:meets 2 3
03:overlaps 1 3
03:overlaps 2 4
04:finished by 1 3
04:finished by 2 3
05:contains 1 4
05:contains 2 3
06:starts 1 2
06:starts 1 3
07:equals 1 2
07:equals 1 2
Packing Ranges
21
Test case Start End
01:precedes 1 2
01:precedes 3 4
02:meets 1 3
03:overlaps 1 4
04:finished by 1 3
05:contains 1 4
06:starts 1 3
07:equals 1 2
Test case Start End
01:precedes 1 2
01:precedes 3
02:meets 1
03:overlaps 1
04:finished by 1
05:contains 1
06:starts 1
07:equals 1
select * from t
match_recognize(
partition by test_case
order by end_date, start_date
measures min(start_date) start_date,
last(end_date) end_date
pattern(a* b)
define a as end_date >= next(start_date)
);
select * from t
match_recognize(
partition by test_case
order by end_date, start_date
measures min(start_date) start_date,
last(end_date) end_date
pattern(a* b)
define a as end_date >= next(start_date)
or end_date is null
);
JOIN: range to range
22
> create table A(start_n, end_n) as
select level, level+1 from dual
connect by level <= 10000;
> create table B as
select start_n+9995 start_n,
end_n+9996 end_n
from A;
> select * from A
join B
on (A.start_n <= B.start_n
and B.start_n < A.end_n)
or (B.start_n <= A.start_n
and A.start_n < B.end_n);
Elapsed: 00:00:13.332
Exadata?
All data in buffer cache
Elapsed: 00:00:13.332
InMemory?
Elapsed: 00:00:09.842
JOIN: range to range
23
------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 1 |00:00:17.82 | 90 |
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:17.82 | 90 |
| 2 | CONCATENATION | | 1 | | 10 |00:00:00.01 | 90 |
| 3 | MERGE JOIN | | 1 | 55 | 10 |00:00:00.01 | 45 |
| 4 | SORT JOIN | | 1 | 10000 | 10000 |00:00:00.01 | 24 |
| 5 | TABLE ACCESS FULL | T_NEW | 1 | 10000 | 10000 |00:00:00.01 | 24 |
|* 6 | FILTER | | 10000 | | 10 |00:00:00.01 | 21 |
|* 7 | SORT JOIN | | 10000 | 10000 | 55 |00:00:00.01 | 21 |
| 8 | TABLE ACCESS FULL| T_OLD | 1 | 10000 | 10000 |00:00:00.02 | 21 |
| 9 | MERGE JOIN | | 1 | 55 | 0 |00:00:17.80 | 45 |
| 10 | SORT JOIN | | 1 | 10000 | 10000 |00:00:00.02 | 24 |
| 11 | TABLE ACCESS FULL | T_NEW | 1 | 10000 | 10000 |00:00:00.01 | 24 |
|* 12 | FILTER | | 10000 | | 0 |00:00:17.78 | 21 |
|* 13 | SORT JOIN | | 10000 | 10000 | 99M|00:01:21.50 | 21 |
| 14 | TABLE ACCESS FULL| T_OLD | 1 | 10000 | 10000 |00:00:00.01 | 21 |
------------------------------------------------------------------------------------------
Join, or Sort and Match?
24
A 1 4
B is equal 1 4
B started by A 1 5
B during A 2 3
B finishes A 3 4
B overlapped by A 3 4 5
B met by A 4 5
B preceded by A 5 6
another A 5 7
✔
✖
?
✔
✔
✔
✔
Join, or Sort and Match?
25
A 1 4
B is equal 1 4
B started by A 1 5
B during A 2 3
B finishes A 3 4
B overlapped by A 3 4 5
B met by A 4 5
B preceded by A 5 6
another A 5 7
✖
?
3
3
3
3
26
select A_start_n, A_end_n, B_start_n, B_end_n from (
select 'A' ttype, A.* from A
union all
select 'B' ttype, B.* from B
) match_recognize (
order by start_n, end_n
measures decode(f.ttype,'A',f.start_n, o.start_n) A_start_n,
decode(f.ttype,'A',f.end_n, o.end_n) A_end_n,
decode(f.ttype,'B',f.start_n, o.start_n) B_start_n,
decode(f.ttype,'B',f.end_n, o.end_n) B_end_n
all rows per match
after match skip to next row
pattern ( {-f-} (o|{-x-})+ )
define o as ttype != f.ttype and start_n < f.end_n,
x as start_n < f.end_n
);
Elapsed: 00:00:00.063
{- exclusion -}
( grouping )
+ at least one
Alternation A | B
✔
✔
27
Child'
s play
More!
• Overlapping ranges with priority
• Data warehouses with date ranges:
– Trickle feed
• Impact on foreign keys
• OLTP
• Take advantage of MATCH_RECOGNIZE ,
28

Más contenido relacionado

La actualidad más candente

Oracle sql high performance tuning
Oracle sql high performance tuningOracle sql high performance tuning
Oracle sql high performance tuningGuy Harrison
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX PerformanceScott Wesley
 
Oracle APEX Introduction (release 18.1)
Oracle APEX Introduction (release 18.1)Oracle APEX Introduction (release 18.1)
Oracle APEX Introduction (release 18.1)Michael Hichwa
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts Bharat Kalia
 
Oracle 11g Database Administration
Oracle 11g Database Administration Oracle 11g Database Administration
Oracle 11g Database Administration Xad Kuain
 
Analyzing and Interpreting AWR
Analyzing and Interpreting AWRAnalyzing and Interpreting AWR
Analyzing and Interpreting AWRpasalapudi
 
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLTop 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLJim Mlodgenski
 
Oracle Database in-Memory Overivew
Oracle Database in-Memory OverivewOracle Database in-Memory Overivew
Oracle Database in-Memory OverivewMaria Colgan
 
How to Handle DEV&TEST&PROD for Oracle Data Integrator
How to Handle DEV&TEST&PROD for Oracle Data IntegratorHow to Handle DEV&TEST&PROD for Oracle Data Integrator
How to Handle DEV&TEST&PROD for Oracle Data IntegratorGurcan Orhan
 
Webinar slides: How to Migrate from Oracle DB to MariaDB
Webinar slides: How to Migrate from Oracle DB to MariaDBWebinar slides: How to Migrate from Oracle DB to MariaDB
Webinar slides: How to Migrate from Oracle DB to MariaDBSeveralnines
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuningYogiji Creations
 
Entity relationship diagram
Entity relationship diagramEntity relationship diagram
Entity relationship diagramHaseeb
 
Back to the Basics - 1 - Introduction to Web Development
Back to the Basics - 1 - Introduction to Web DevelopmentBack to the Basics - 1 - Introduction to Web Development
Back to the Basics - 1 - Introduction to Web DevelopmentClint LaForest
 
Oracle DB Performance Tuning Tips
Oracle DB Performance Tuning TipsOracle DB Performance Tuning Tips
Oracle DB Performance Tuning TipsAsanka Dilruk
 
MySQL Cookbook: Recipes for Your Business
MySQL Cookbook: Recipes for Your BusinessMySQL Cookbook: Recipes for Your Business
MySQL Cookbook: Recipes for Your BusinessSveta Smirnova
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxFive_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxMaria Colgan
 
SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)Logan Palanisamy
 

La actualidad más candente (20)

Oracle sql high performance tuning
Oracle sql high performance tuningOracle sql high performance tuning
Oracle sql high performance tuning
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX Performance
 
Oracle APEX Introduction (release 18.1)
Oracle APEX Introduction (release 18.1)Oracle APEX Introduction (release 18.1)
Oracle APEX Introduction (release 18.1)
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Oracle 11g Database Administration
Oracle 11g Database Administration Oracle 11g Database Administration
Oracle 11g Database Administration
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Analyzing and Interpreting AWR
Analyzing and Interpreting AWRAnalyzing and Interpreting AWR
Analyzing and Interpreting AWR
 
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLTop 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
 
Oracle Database in-Memory Overivew
Oracle Database in-Memory OverivewOracle Database in-Memory Overivew
Oracle Database in-Memory Overivew
 
How to Handle DEV&TEST&PROD for Oracle Data Integrator
How to Handle DEV&TEST&PROD for Oracle Data IntegratorHow to Handle DEV&TEST&PROD for Oracle Data Integrator
How to Handle DEV&TEST&PROD for Oracle Data Integrator
 
Webinar slides: How to Migrate from Oracle DB to MariaDB
Webinar slides: How to Migrate from Oracle DB to MariaDBWebinar slides: How to Migrate from Oracle DB to MariaDB
Webinar slides: How to Migrate from Oracle DB to MariaDB
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuning
 
MS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTUREMS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTURE
 
Entity relationship diagram
Entity relationship diagramEntity relationship diagram
Entity relationship diagram
 
Back to the Basics - 1 - Introduction to Web Development
Back to the Basics - 1 - Introduction to Web DevelopmentBack to the Basics - 1 - Introduction to Web Development
Back to the Basics - 1 - Introduction to Web Development
 
Oracle DB Performance Tuning Tips
Oracle DB Performance Tuning TipsOracle DB Performance Tuning Tips
Oracle DB Performance Tuning Tips
 
MySQL Cookbook: Recipes for Your Business
MySQL Cookbook: Recipes for Your BusinessMySQL Cookbook: Recipes for Your Business
MySQL Cookbook: Recipes for Your Business
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxFive_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
 
SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)
 

Similar a Ranges, ranges everywhere (Oracle SQL)

Row Pattern Matching in Oracle Database 12c
Row Pattern Matching in Oracle Database 12cRow Pattern Matching in Oracle Database 12c
Row Pattern Matching in Oracle Database 12cStew Ashton
 
Bcolz Groupby Discussion Document
Bcolz Groupby Discussion DocumentBcolz Groupby Discussion Document
Bcolz Groupby Discussion DocumentCarst Vaartjes
 
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14stewashton
 
Special webinar on tips for perfect score in sat math
Special webinar on tips for perfect score in sat mathSpecial webinar on tips for perfect score in sat math
Special webinar on tips for perfect score in sat mathCareerGOD
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c eraMauro Pagano
 
Verilog-Behavioral Modeling .pdf
Verilog-Behavioral Modeling .pdfVerilog-Behavioral Modeling .pdf
Verilog-Behavioral Modeling .pdfUsssshaaaMehta
 
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxssuser2f67c91
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptgourav kottawar
 
OracleSQLraining.pptx
OracleSQLraining.pptxOracleSQLraining.pptx
OracleSQLraining.pptxRajendra Jain
 
Shift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David LaulusaShift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David LaulusaQA or the Highway
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Bounds for overlapping interval join on MapReduce
Bounds for overlapping interval join on MapReduceBounds for overlapping interval join on MapReduce
Bounds for overlapping interval join on MapReduceShantanu Sharma
 
Time Series With OrientDB - Fosdem 2015
Time Series With OrientDB - Fosdem 2015Time Series With OrientDB - Fosdem 2015
Time Series With OrientDB - Fosdem 2015wolf4ood
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_iElsayed Hemayed
 
Standard cells library design
Standard cells library designStandard cells library design
Standard cells library designBharat Biyani
 

Similar a Ranges, ranges everywhere (Oracle SQL) (20)

Row Pattern Matching in Oracle Database 12c
Row Pattern Matching in Oracle Database 12cRow Pattern Matching in Oracle Database 12c
Row Pattern Matching in Oracle Database 12c
 
Bcolz Groupby Discussion Document
Bcolz Groupby Discussion DocumentBcolz Groupby Discussion Document
Bcolz Groupby Discussion Document
 
LectureSlides3.pdf
LectureSlides3.pdfLectureSlides3.pdf
LectureSlides3.pdf
 
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
 
Special webinar on tips for perfect score in sat math
Special webinar on tips for perfect score in sat mathSpecial webinar on tips for perfect score in sat math
Special webinar on tips for perfect score in sat math
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c era
 
Verilog-Behavioral Modeling .pdf
Verilog-Behavioral Modeling .pdfVerilog-Behavioral Modeling .pdf
Verilog-Behavioral Modeling .pdf
 
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptx
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
 
2017 biological databasespart2
2017 biological databasespart22017 biological databasespart2
2017 biological databasespart2
 
2016 02 23_biological_databases_part2
2016 02 23_biological_databases_part22016 02 23_biological_databases_part2
2016 02 23_biological_databases_part2
 
OracleSQLraining.pptx
OracleSQLraining.pptxOracleSQLraining.pptx
OracleSQLraining.pptx
 
Shift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David LaulusaShift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David Laulusa
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Bounds for overlapping interval join on MapReduce
Bounds for overlapping interval join on MapReduceBounds for overlapping interval join on MapReduce
Bounds for overlapping interval join on MapReduce
 
Time Series With OrientDB - Fosdem 2015
Time Series With OrientDB - Fosdem 2015Time Series With OrientDB - Fosdem 2015
Time Series With OrientDB - Fosdem 2015
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_i
 
Standard cells library design
Standard cells library designStandard cells library design
Standard cells library design
 
ictir2016
ictir2016ictir2016
ictir2016
 
Self healing data
Self healing dataSelf healing data
Self healing data
 

Último

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Ranges, ranges everywhere (Oracle SQL)

  • 1. Ranges, Ranges Everywhere! Stew Ashton (stewashton.wordpress.com) UKOUG Tech 2016 Can you read the following line? If not, please move closer. It's much better when you can read the code ;)
  • 2. Agenda • Defining ranges • Relating ranges: gaps, overlaps • Range DDL: sensible data • Ranges in one table • Ranges in two tables 2
  • 3. Who am I? • 36 years in IT – Developer, Technical Sales Engineer, Technical Architect – Aeronautics, IBM, Finance – Mainframe, client-server, Web apps • 12 years using Oracle database – SQL performance analysis – Replace Java with SQL • 4 years as in-house “Oracle Development Expert” • Conference speaker since 2014 • Currently independent 3
  • 5. What is a range? • Two values that can be compared – Always use the same datatype  – Comparable datatypes: • integer, date (without time) • number, datetime, interval, (n)(var)char • rowid • Range design questions: – Is the "end" value part of the range? – Are NULLs allowed? 5
  • 6. Allen’s Interval Algebra 6 1 2 3 4 A precedes B 1 2 B preceded by A 3 4 A meets B 1 2 B met by A 2 3 A overlaps B 1 3 B overlapped by A 2 4 A finished by B 1 3 B finishes A 2 3 A contains B 1 4 B during A 2 3 A starts B 1 2 B started by A 1 3 A and B 1 2 are equal 1 2 Meet Gap "Overlap" 1 2 3 41 2 3 4 A precedes B 1 2 B preceded by A 3 4 1 2 3 4 A precedes B 1 2 B preceded by A 3 4 A meets B 1 2 B met by A 2 3
  • 7. End value: Inclusive or Exclusive • Design must allow ranges to "meet" • Discrete quantities can be inclusive – [1-3] meets [4-6] : no intermediate integer – [Jan. 1-31] meets [Feb. 1-28] : no intermediate date • Continuous quantities require exclusive – Most ranges are continuous (including dates, really) 7
  • 8. Votes for Exclusive end values • SQL:2013 and Oracle 12c Temporal Validity – "Period": date/time range • [Closed-Open): includes start time but not end time • WIDTH_BUCKET() function – Puts values in equiwidth histogram – Buckets must touch – [Closed-open): upper boundary value goes in higher bucket • Me! – Exclusive end values work for every kind of range – Except: ROWID ranges must be inclusive 8
  • 9. DDL: make sure data is sensible • Start_range < End_range • If date without time, CHECK( dte = trunc(dte)) • If integer, say so • Is NULL allowed? – If so, what does it mean? – Ex. Temporal Validity : NULL end value means "until the end of time" • Are overlaps allowed? 9
  • 10. Overlaps avoided by unique constraints 10 Unique(start,end) Unique(start) Unique(end) 1 2 3 4 No constraint works A overlaps B 1 3 B overlapped by A 2 4 Y A finished by B 1 3 B finishes A 2 3 No constraint works A contains B 1 4 B during A 2 3 Y A starts B 1 2 B started by A 1 3 Y Y Y A and B 1 2 are equal 1 2
  • 11. Avoiding Overlaps: 3 solutions 1. Triggers – Hard to do right, not very scalable 2. "Refresh on commit" materialized views – Not scalable? 3. Virtual ranges 11
  • 12. Virtual range: no gaps, no overlaps • One column: start value • End value is calculated: = next row's start – Putting identical value in 2 rows is denormalization • Last row has unlimited end • Maybe OK for audit trails? START_VALUE END_VALUE 16-11-15 08:30 16-11-15 09:30 16-11-15 09:30 16-11-15 18:30 16-11-15 18:30 (null) 12 START_VALUE 16-11-15 08:30 16-11-15 09:30 16-11-15 18:30 Physical (table) Virtual (view)
  • 13. Semi-Virtual range: no overlaps • Start column always used • End column optional: – If null, use next row's start – If not null, use lesser of end column and next row's start – Last row can have limited end • Or: intermediate row with 'not exists' flag – ≅ Change Data Capture format 13 START_VALUE END_VALUE 16-11-15 08:30 16-11-15 09:30 16-11-15 18:30 (null) START_VALUE D 16-11-15 08:30 16-11-15 09:30 D 16-11-15 18:30
  • 14. Range-related SQL • Why hard? – Can't use BETWEEN – Inequality joins impact performance – With overlaps, 1 value point can be in any number of rows – Joining 2 tables with overlaps -> row explosion – NULLs have special meanings • Common problems – Find gaps – Intersect: find overlaps – Union: packing ranges between gaps – Joins • Today, ends are exclusive, everything is NOT NULL (unless specified) 14
  • 15. 15
  • 16. FROM_TM TO_TM 07:00 08:00 09:00 10:50 10:00 10:45 12:00 12:45 18:00 23:00 select * from ( select max (to_tm) over(order by from_tm) as gap_from, lead(from_tm) over(order by from_tm) as gap_to from t ) where gap_from < gap_to; select to_tm as gap_from, lead(from_tm) over(order by from_tm) as gap_to from t FROM_TM GAP_FROM GAP_TO 07:00 08:00 09:00 09:00 10:50 10:00 10:00 10:45 12:00 12:00 12:45 18:00 18:00 23:00 GAP_FROM GAP_TO 08:00 09:00 10:50 12:00 12:45 18:00 Gaps, ex. Free time in calendar 16 FROM_TM GAP_FROM GAP_TO 07:00 08:00 09:00 09:00 10:50 10:00 10:00 10:50 12:00 12:00 12:45 18:00 18:00 23:00
  • 17. Intersect: finding Overlaps 17 Test case Start End 01:precedes 1 2 01:precedes 3 4 02:meets 1 2 02:meets 2 3 03:overlaps 1 3 03:overlaps 2 4 04:finished by 1 3 04:finished by 2 3 05:contains 1 4 05:contains 2 3 06:starts 1 2 06:starts 1 3 07:equals 1 2 07:equals 1 2 select test_case, dte, col from t unpivot (dte for col in ( start_date as 1, end_date as -1)) A overlaps B 1 3 B overlapped by A 2 4 1 2 2 3 3 4
  • 18. select test_case, dte, col from t unpivot (dte for col in ( start_date as 1, end_date as -1)) select test_case, dte "Start", lead(dte,1,dte) over( partition by test_case order by dte, col desc ) "End", sum(col) over( partition by test_case order by dte, col desc ) "Rows" from t unpivot (dte for col in ( start_date as 1, end_date as -1)) Intersect: finding Overlaps 18 Test case Dte Col 01:precedes 1 1 01:precedes 2 -1 01:precedes 3 1 01:precedes 4 -1 02:meets 1 1 02:meets 2 -1 02:meets 2 1 02:meets 3 -1 03:overlaps 1 1 03:overlaps 3 -1 03:overlaps 2 1 03:overlaps 4 -1
  • 19. select test_case, dte "Start", lead(dte,1,dte) over( partition by test_case order by dte, col desc ) "End", sum(col) over( partition by test_case order by dte, col desc ) "Rows" from t unpivot (dte for col in ( start_date as 1, end_date as -1)) select * from ( select test_case, dte "Start", lead(dte,1,dte) over( partition by test_case order by dte, col desc ) "End", sum(col) over( partition by test_case order by dte, col desc ) "Rows" from t unpivot (dte for col in ( start_date as 1, end_date as -1)) ) where "Start" < "End"; Intersect: finding Overlaps 19 Test case Start End Rows 01:precedes 1 2 1 01:precedes 2 3 0 01:precedes 3 4 1 01:precedes 4 4 0 02:meets 1 2 1 02:meets 2 2 2 02:meets 2 3 1 02:meets 3 3 0 03:overlaps 1 2 1 03:overlaps 2 3 2 03:overlaps 3 4 1 03:overlaps 4 4 0 ✖ ✖ ✖ ✖
  • 20. select * from ( select test_case, dte "Start", lead(dte,1,dte) over( partition by test_case order by dte, col desc ) "End", sum(col) over( partition by test_case order by dte, col desc ) "Rows" from t unpivot (dte for col in ( start_date as 1, end_date as -1)) ) where "Start" < "End"; select * from ( select test_case, dte "Start", lead(dte,1,dte) over( partition by test_case order by dte, col desc ) "End", sum(col) over( partition by test_case order by dte, col desc ) "Rows" from t unpivot (dte for col in ( start_date as 1, end_date as -1)) ) where "Rows" > 1 and "Start" < "End"; Intersect: finding Overlaps 20 Test case Start End Rows 01:precedes 1 2 1 01:precedes 2 3 0 01:precedes 3 4 1 02:meets 1 2 1 02:meets 2 3 1 03:overlaps 1 2 1 03:overlaps 2 3 2 03:overlaps 3 4 1 Test case Start End Rows 03:overlaps 2 3 2 04:finished by 2 3 2 05:contains 2 3 2 06:starts 1 2 2 07:equals 1 2 2
  • 21. Test case Start End 01:precedes 1 2 01:precedes 3 4 02:meets 1 2 02:meets 2 3 03:overlaps 1 3 03:overlaps 2 4 04:finished by 1 3 04:finished by 2 3 05:contains 1 4 05:contains 2 3 06:starts 1 2 06:starts 1 3 07:equals 1 2 07:equals 1 2 Packing Ranges 21 Test case Start End 01:precedes 1 2 01:precedes 3 4 02:meets 1 3 03:overlaps 1 4 04:finished by 1 3 05:contains 1 4 06:starts 1 3 07:equals 1 2 Test case Start End 01:precedes 1 2 01:precedes 3 02:meets 1 03:overlaps 1 04:finished by 1 05:contains 1 06:starts 1 07:equals 1 select * from t match_recognize( partition by test_case order by end_date, start_date measures min(start_date) start_date, last(end_date) end_date pattern(a* b) define a as end_date >= next(start_date) ); select * from t match_recognize( partition by test_case order by end_date, start_date measures min(start_date) start_date, last(end_date) end_date pattern(a* b) define a as end_date >= next(start_date) or end_date is null );
  • 22. JOIN: range to range 22 > create table A(start_n, end_n) as select level, level+1 from dual connect by level <= 10000; > create table B as select start_n+9995 start_n, end_n+9996 end_n from A; > select * from A join B on (A.start_n <= B.start_n and B.start_n < A.end_n) or (B.start_n <= A.start_n and A.start_n < B.end_n); Elapsed: 00:00:13.332 Exadata? All data in buffer cache Elapsed: 00:00:13.332 InMemory? Elapsed: 00:00:09.842
  • 23. JOIN: range to range 23 ------------------------------------------------------------------------------------------ | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | | 1 |00:00:17.82 | 90 | | 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:17.82 | 90 | | 2 | CONCATENATION | | 1 | | 10 |00:00:00.01 | 90 | | 3 | MERGE JOIN | | 1 | 55 | 10 |00:00:00.01 | 45 | | 4 | SORT JOIN | | 1 | 10000 | 10000 |00:00:00.01 | 24 | | 5 | TABLE ACCESS FULL | T_NEW | 1 | 10000 | 10000 |00:00:00.01 | 24 | |* 6 | FILTER | | 10000 | | 10 |00:00:00.01 | 21 | |* 7 | SORT JOIN | | 10000 | 10000 | 55 |00:00:00.01 | 21 | | 8 | TABLE ACCESS FULL| T_OLD | 1 | 10000 | 10000 |00:00:00.02 | 21 | | 9 | MERGE JOIN | | 1 | 55 | 0 |00:00:17.80 | 45 | | 10 | SORT JOIN | | 1 | 10000 | 10000 |00:00:00.02 | 24 | | 11 | TABLE ACCESS FULL | T_NEW | 1 | 10000 | 10000 |00:00:00.01 | 24 | |* 12 | FILTER | | 10000 | | 0 |00:00:17.78 | 21 | |* 13 | SORT JOIN | | 10000 | 10000 | 99M|00:01:21.50 | 21 | | 14 | TABLE ACCESS FULL| T_OLD | 1 | 10000 | 10000 |00:00:00.01 | 21 | ------------------------------------------------------------------------------------------
  • 24. Join, or Sort and Match? 24 A 1 4 B is equal 1 4 B started by A 1 5 B during A 2 3 B finishes A 3 4 B overlapped by A 3 4 5 B met by A 4 5 B preceded by A 5 6 another A 5 7 ✔ ✖ ? ✔ ✔ ✔ ✔
  • 25. Join, or Sort and Match? 25 A 1 4 B is equal 1 4 B started by A 1 5 B during A 2 3 B finishes A 3 4 B overlapped by A 3 4 5 B met by A 4 5 B preceded by A 5 6 another A 5 7 ✖ ? 3 3 3 3
  • 26. 26 select A_start_n, A_end_n, B_start_n, B_end_n from ( select 'A' ttype, A.* from A union all select 'B' ttype, B.* from B ) match_recognize ( order by start_n, end_n measures decode(f.ttype,'A',f.start_n, o.start_n) A_start_n, decode(f.ttype,'A',f.end_n, o.end_n) A_end_n, decode(f.ttype,'B',f.start_n, o.start_n) B_start_n, decode(f.ttype,'B',f.end_n, o.end_n) B_end_n all rows per match after match skip to next row pattern ( {-f-} (o|{-x-})+ ) define o as ttype != f.ttype and start_n < f.end_n, x as start_n < f.end_n ); Elapsed: 00:00:00.063 {- exclusion -} ( grouping ) + at least one Alternation A | B ✔ ✔
  • 28. More! • Overlapping ranges with priority • Data warehouses with date ranges: – Trickle feed • Impact on foreign keys • OLTP • Take advantage of MATCH_RECOGNIZE , 28