SlideShare a Scribd company logo
Date Ranges:
Time for Oracle 12c SQL
"Ask not what Oracle can do for you,
ask what you can do with Oracle."
Stew Ashton 2.0 UKOUG Tech 15 Stew ASHTON
UKOUG Tech 14
Agenda
• Who am I?
• Date/time data types
• Date range concepts
• 12c Temporal Validity
• Date range DDL
• Date range queries
2
Who am I?
• 35 years as Developer / Technical Architect
– Aeronautics, IBM, Finance
– Mainframe, client-server, Web apps
• 27 years as an American in Paris
• 10 years using Oracle database
– Performance analysis
– Replace Java with SQL
• 3 years as internal "Oracle Development Expert"
• "Career 2.0" started last week
3
Review of date/time datatypes
• There is no "date" type: always a time element
– Closest thing to a date: dte DATE CHECK(dte = TRUNC(dte))
• There are no "formats"
• Values from 4712-01-01 00:00:00 BC through 9999-12-31 23:59:59 AD
• Any of the above can be used in ranges
4
Datatype Year Month Day Hour Minute Second Fraction Time zone
Date Y Y Y Y Y Y
Timestamp Y Y Y Y Y Y Y
Timestamp with
local time zone
Y Y Y Y Y Y Y implicit
Timestamp with
time zone
Y Y Y Y Y Y Y explicit
Date/time Ranges
• Use same data_type for start and end (duh!)
• To NULL or not to NULL?
– SQL:2011 and 12c allow
– Makes NULL mean something
– Accounting for NULLs complicates queries and index design
– Alternative: extreme values such as DATE '9999-21-31'
• "Start" is included in the range, "end" is not
– Works for date/time ranges, not just dates
– See SQL:2011 standard, 12c Temporal Validity
– Allows ranges to "meet"
– SQL can't use BETWEEN "start" and "end"
5
Allen’s
Time Interval
Arithmetic
6
Day of month 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 equals B 1 2
B equals A 1 2
Meet
Gap
"Overlap"
Day of month 1 2 3 4Day of month 1 2 3 4
A precedes B 1 2
B preceded by A 3 4
Day of month 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
Temporal Validity: SQL:2011, 12c
• Two temporal concepts
– "Transaction time": when data committed (flashback)
• "Oracle Flashback" Tues. 11:20 (Connor McDonald)
– "Valid time":
• start / end times determined by users
• Past, present or future
• "Period": date/time range
– Closed-Open: includes start time but not end time
– Constraint: start time < end time
– NULL start time = valid any time before end
– NULL end time = valid from start time on
7
What Oracle can do for you
8
create table valid_emp(
ename varchar2(64),
PERIOD FOR PRESENCE
);
9
select column_name, data_type, nullable,
hidden_column, virtual_column
from user_tab_cols where table_name = 'VALID_EMP'
order by internal_column_id;
Name Data Type Nullable Hidden Virtual
PRESENCE_START TIMESTAMP(6) WITH TIME ZONE Y YES NO
PRESENCE_END TIMESTAMP(6) WITH TIME ZONE Y YES NO
PRESENCE NUMBER Y YES YES
ENAME VARCHAR2 Y NO NO
select search_condition from user_constraints
where table_name = 'VALID_EMP';
SEARCH_CONDITION
PRESENCE_START < PRESENCE_END
10
insert all
into valid_emp (ename, presence_start, presence_end)
values('Stew', date '1998-03-01', date '2015-12-01')
into valid_emp (ename, presence_start, presence_end)
values('Stew', date '2016-01-01', date '2050-12-01')
select null from dual;
select * from valid_emp;
ENAME
Stew
Stew
Oops! Hidden columns…
11
select ename, presence_start, presence_end
from valid_emp;
ENAME PRESENCE_START PRESENCE_END
Stew 1998-03-01 2015-12-01
Stew 2016-01-01 2050-12-01
12
select ename, presence_start, presence_end
from valid_emp
as of period for presence systimestamp;
ENAME PRESENCE_START PRESENCE_END
select * from table
(dbms_xplan.display_cursor(format=>'+PREDICATE'));
filter((
(T.PRESENCE_START IS NULL OR
SYS_EXTRACT_UTC(T.PRESENCE_START)<=SYS_EXTRACT_UTC(SYSTIMESTAMP(6)))
AND
(T.PRESENCE_END IS NULL OR
SYS_EXTRACT_UTC(T.PRESENCE_END) > SYS_EXTRACT_UTC(SYSTIMESTAMP(6)))
))
13
select ename, presence_start, presence_end
from valid_emp
as of period for presence to_timestamp_tz('2015-11-01');
exec DBMS_FLASHBACK_ARCHIVE.ENABLE_AT_VALID_TIME
('CURRENT');
select ename, presence_start, presence_end
from valid_emp;
ENAME PRESENCE_START PRESENCE_END
Stew 1998-03-01 2015-12-01
ENAME PRESENCE_START PRESENCE_END
Valid Time Range
14
select ename, presence_start, presence_end
from valid_emp
versions period for presence
between to_timestamp_tz('2015-11-01')
and to_timestamp_tz('2016-01-01');
ENAME PRESENCE_START PRESENCE_END
Stew 1998-03-01 2015-12-01
Stew 2016-01-01 2050-12-01
• So what did Oracle do for us?
– Notion of "period"
– Automatic, hidden column definitions
– New query syntax with automatic rewrite
• And what did Oracle not do for us?
– Query performance?
– Temporal constraints?
– Query for gaps or overlaps?
– Temporal DML?
• Now: what can we do with Oracle? 15
What we need to do:
• Temporal constraints
• Performance: indexing
• Temporal queries
• Temporal DML
16
Temporal Constraints
• No gaps, no overlaps
– Don't use ranges!
– "effective date"
• "start" = EFF_DATE , "end" = lead(EFF_DATE) over (EFF_DATE)
• Temporal primary keys
– Use ID + start_date (usually)
– ORA-02329: …TIMESTAMP WITH TIME ZONE cannot be unique or
primary key
• Temporal foreign keys
– Child range must be within parent range
– "On commit" materialized views (hard to scale)
– Triggers (hard to write correctly)
17
Temporal DDL & Indexing
18
create table valid_emp(
ename varchar2(64),
presence_start date,
presence_end date not null,
CHECK(presence_start < presence_end),
period for presence(presence_start, presence_end),
CONSTRAINT valid_emp_pk primary key (ename, presence_start)
USING INDEX (
CREATE INDEX valid_emp_pk
ON valid_emp (ename, presence_start, presence_end)
)
);
Temporal Queries
• Use Analytic functions
or 12c MATCH_RECOGNIZE
• Typical use cases
– find gaps: "free time" in calendars
– Merge ranges that "meet"
– Merge ranges that "meet" or "overlap"
– Join on intersecting date ranges
• For simplicity, I assume "not NULL"
19
Find Gaps
20
START_DATE END_DATE
---------- ----------
2007-01-12 2007-01-25
2007-01-20 2007-02-01
2007-02-05 2007-02-10
2007-02-05 2007-02-28
2007-02-10 2007-02-15
2007-03-01 2007-03-02
2007-03-03 2007-03-16
SELECT end_date start_gap,
LEAD(start_date) OVER (ORDER BY start_date) end_gap
FROM T;
START_GAP END_GAP
---------- ----------
2007-01-25 2007-01-20
2007-02-01 2007-02-05
2007-02-10 2007-02-05
2007-02-28 2007-02-10
2007-02-15 2007-03-01
2007-03-02 2007-03-03
2007-03-16
Find Gaps
21
START_DATE END_DATE
---------- ----------
2007-01-12 2007-01-25
2007-01-20 2007-02-01
2007-02-05 2007-02-10
2007-02-05 2007-02-28
2007-02-10 2007-02-15
2007-03-01 2007-03-02
2007-03-03 2007-03-16
SELECT * FROM (
SELECT MAX(end_date) OVER (ORDER BY start_date) start_gap,
LEAD(start_date) OVER (ORDER BY start_date) end_gap
FROM T
)
WHERE start_gap < end_gap;
START_GAP END_GAP
---------- ----------
2007-01-25 2007-01-20
2007-02-01 2007-02-05
2007-02-28 2007-02-05
2007-02-28 2007-02-10
2007-02-28 2007-03-01
2007-03-02 2007-03-03
2007-03-16
PACK: merge ranges that meet
22
ID START END
1 01-01 01-03
2 01-03 01-05
3 02-05 02-28
4 02-10 02-15
5 03-01 03-11
6 03-10 03-12
7 03-1103-21
select * from t
match_recognize(
order by start_date, end_date
measures first(id) "first", last(id) "last",
first(start_date) "start", last(end_date) "end"
pattern(A* B)
define A as end_date = next(start_date)
);
first last start end
----- ---- ----- -----
1 2 01-01 01-05
3 3 02-05 02-28
4 4 02-10 02-15
5 5 03-01 03-11
6 6 03-10 03-12
7 7 03-11 03-21
overlap
intervening
Merge ranges that meet or overlap
23
ID START END
1 01-01 01-03
2 01-03 01-05
3 02-05 02-28
4 02-10 02-15
5 03-01 03-11
6 03-10 03-12
7 03-1103-21
select * from t
match_recognize(
order by start_date, end_date
measures first(id) "first", last(id) "last",
first(start_date) "start", MAX(end_date) "end"
pattern(A* B)
define A as MAX(end_date) >= next(start_date)
);
first last start end
----- ---- ----- -----
1 2 01-01 01-05
3 4 02-05 02-28
5 7 03-01 03-21
gap
gap
Join on intersecting ranges
• Prerequisite for temporal DML
– Join input to table to find affected rows
• Use UNION ALL to gather input and affected rows
• Use UNPIVOT and DISTINCT to get range boundaries
• Use LEAD() to create base ranges
• DO LEFT JOIN from base ranges to table + input.
24
1 3 5 7
2 4
St
1
2
3
4
5
St End
1 2
2 3
3 4
4 5
St End Old New
1 2 1-3
2 3 1-3 2-4
3 4 3-5 2-4
4 5 3-5

More Related Content

Viewers also liked

Eff Plsql
Eff PlsqlEff Plsql
Eff Plsql
afa reg
 
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
 
The Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result CacheThe Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result Cache
Steven Feuerstein
 
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
stewashton
 
Impact Analysis with PL/Scope
Impact Analysis with PL/ScopeImpact Analysis with PL/Scope
Impact Analysis with PL/Scope
Steven Feuerstein
 
All About PL/SQL Collections
All About PL/SQL CollectionsAll About PL/SQL Collections
All About PL/SQL Collections
Steven Feuerstein
 
Managing SQL Performance
Managing SQL PerformanceManaging SQL Performance
Managing SQL Performance
Karen Morton
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, How
Karen Morton
 

Viewers also liked (8)

Eff Plsql
Eff PlsqlEff Plsql
Eff Plsql
 
SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)
 
The Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result CacheThe Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result Cache
 
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
 
Impact Analysis with PL/Scope
Impact Analysis with PL/ScopeImpact Analysis with PL/Scope
Impact Analysis with PL/Scope
 
All About PL/SQL Collections
All About PL/SQL CollectionsAll About PL/SQL Collections
All About PL/SQL Collections
 
Managing SQL Performance
Managing SQL PerformanceManaging SQL Performance
Managing SQL Performance
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, How
 

Similar to Date rangestech15

Oracle 12c SQL: Date Ranges
Oracle 12c SQL: Date RangesOracle 12c SQL: Date Ranges
Oracle 12c SQL: Date Ranges
Stew Ashton
 
11thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp0111thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp01
Karam Abuataya
 
11 Things About11g
11 Things About11g11 Things About11g
11 Things About11g
fcamachob
 
Oracle12c For Developers
Oracle12c For DevelopersOracle12c For Developers
Oracle12c For Developers
Alex Nuijten
 
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Oracle12 for Developers - Oracle OpenWorld Preview AMISOracle12 for Developers - Oracle OpenWorld Preview AMIS
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Getting value from IoT, Integration and Data Analytics
 
Do You Have the Time
Do You Have the TimeDo You Have the Time
Do You Have the Time
Michael Antonovich
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
Federico Razzoli
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application Development
Saurabh K. Gupta
 
Data Time Travel by Delta Time Machine
Data Time Travel by Delta Time MachineData Time Travel by Delta Time Machine
Data Time Travel by Delta Time Machine
Databricks
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
Alex Zaballa
 
Oracle 12c Automatic Data Optimization (ADO) - ILM
Oracle 12c Automatic Data Optimization (ADO) - ILMOracle 12c Automatic Data Optimization (ADO) - ILM
Oracle 12c Automatic Data Optimization (ADO) - ILM
Monowar Mukul
 
Rmoug ashmaster
Rmoug ashmasterRmoug ashmaster
Rmoug ashmaster
Kyle Hailey
 
Everything That Is Really Useful in Oracle Database 12c for Application Devel...
Everything That Is Really Useful in Oracle Database 12c for Application Devel...Everything That Is Really Useful in Oracle Database 12c for Application Devel...
Everything That Is Really Useful in Oracle Database 12c for Application Devel...
Lucas Jellema
 
Using SQL Plan Management (SPM) to balance Plan Flexibility and Plan Stability
Using SQL Plan Management (SPM) to balance Plan Flexibility and Plan StabilityUsing SQL Plan Management (SPM) to balance Plan Flexibility and Plan Stability
Using SQL Plan Management (SPM) to balance Plan Flexibility and Plan Stability
Carlos Sierra
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
Federico Razzoli
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
Alex Zaballa
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
Alex Zaballa
 
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdfHailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
cookie1969
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
Alex Zaballa
 
5 Cool Things About SQL
5 Cool Things About SQL5 Cool Things About SQL
5 Cool Things About SQL
Connor McDonald
 

Similar to Date rangestech15 (20)

Oracle 12c SQL: Date Ranges
Oracle 12c SQL: Date RangesOracle 12c SQL: Date Ranges
Oracle 12c SQL: Date Ranges
 
11thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp0111thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp01
 
11 Things About11g
11 Things About11g11 Things About11g
11 Things About11g
 
Oracle12c For Developers
Oracle12c For DevelopersOracle12c For Developers
Oracle12c For Developers
 
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Oracle12 for Developers - Oracle OpenWorld Preview AMISOracle12 for Developers - Oracle OpenWorld Preview AMIS
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
 
Do You Have the Time
Do You Have the TimeDo You Have the Time
Do You Have the Time
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application Development
 
Data Time Travel by Delta Time Machine
Data Time Travel by Delta Time MachineData Time Travel by Delta Time Machine
Data Time Travel by Delta Time Machine
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
Oracle 12c Automatic Data Optimization (ADO) - ILM
Oracle 12c Automatic Data Optimization (ADO) - ILMOracle 12c Automatic Data Optimization (ADO) - ILM
Oracle 12c Automatic Data Optimization (ADO) - ILM
 
Rmoug ashmaster
Rmoug ashmasterRmoug ashmaster
Rmoug ashmaster
 
Everything That Is Really Useful in Oracle Database 12c for Application Devel...
Everything That Is Really Useful in Oracle Database 12c for Application Devel...Everything That Is Really Useful in Oracle Database 12c for Application Devel...
Everything That Is Really Useful in Oracle Database 12c for Application Devel...
 
Using SQL Plan Management (SPM) to balance Plan Flexibility and Plan Stability
Using SQL Plan Management (SPM) to balance Plan Flexibility and Plan StabilityUsing SQL Plan Management (SPM) to balance Plan Flexibility and Plan Stability
Using SQL Plan Management (SPM) to balance Plan Flexibility and Plan Stability
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
 
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdfHailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
5 Cool Things About SQL
5 Cool Things About SQL5 Cool Things About SQL
5 Cool Things About SQL
 

More from stewashton

JSON in Oracle 18c and 19c
JSON in Oracle 18c and 19cJSON in Oracle 18c and 19c
JSON in Oracle 18c and 19c
stewashton
 
Make your data dance: PIVOT, UNPIVOT & GROUP BY extensions
Make your data dance: PIVOT, UNPIVOT & GROUP BY extensionsMake your data dance: PIVOT, UNPIVOT & GROUP BY extensions
Make your data dance: PIVOT, UNPIVOT & GROUP BY extensions
stewashton
 
JSON in 18c and 19c
JSON in 18c and 19cJSON in 18c and 19c
JSON in 18c and 19c
stewashton
 
Make your data dance
Make your data danceMake your data dance
Make your data dance
stewashton
 
Json in 18c and 19c
Json in 18c and 19cJson in 18c and 19c
Json in 18c and 19c
stewashton
 
Make your data dance: PIVOT and GROUP BY in Oracle SQL
Make your data dance: PIVOT and GROUP BY in Oracle SQLMake your data dance: PIVOT and GROUP BY in Oracle SQL
Make your data dance: PIVOT and GROUP BY in Oracle SQL
stewashton
 
Row patternmatching12ctech14
Row patternmatching12ctech14Row patternmatching12ctech14
Row patternmatching12ctech14
stewashton
 

More from stewashton (7)

JSON in Oracle 18c and 19c
JSON in Oracle 18c and 19cJSON in Oracle 18c and 19c
JSON in Oracle 18c and 19c
 
Make your data dance: PIVOT, UNPIVOT & GROUP BY extensions
Make your data dance: PIVOT, UNPIVOT & GROUP BY extensionsMake your data dance: PIVOT, UNPIVOT & GROUP BY extensions
Make your data dance: PIVOT, UNPIVOT & GROUP BY extensions
 
JSON in 18c and 19c
JSON in 18c and 19cJSON in 18c and 19c
JSON in 18c and 19c
 
Make your data dance
Make your data danceMake your data dance
Make your data dance
 
Json in 18c and 19c
Json in 18c and 19cJson in 18c and 19c
Json in 18c and 19c
 
Make your data dance: PIVOT and GROUP BY in Oracle SQL
Make your data dance: PIVOT and GROUP BY in Oracle SQLMake your data dance: PIVOT and GROUP BY in Oracle SQL
Make your data dance: PIVOT and GROUP BY in Oracle SQL
 
Row patternmatching12ctech14
Row patternmatching12ctech14Row patternmatching12ctech14
Row patternmatching12ctech14
 

Recently uploaded

GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 

Recently uploaded (20)

GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 

Date rangestech15

  • 1. Date Ranges: Time for Oracle 12c SQL "Ask not what Oracle can do for you, ask what you can do with Oracle." Stew Ashton 2.0 UKOUG Tech 15 Stew ASHTON UKOUG Tech 14
  • 2. Agenda • Who am I? • Date/time data types • Date range concepts • 12c Temporal Validity • Date range DDL • Date range queries 2
  • 3. Who am I? • 35 years as Developer / Technical Architect – Aeronautics, IBM, Finance – Mainframe, client-server, Web apps • 27 years as an American in Paris • 10 years using Oracle database – Performance analysis – Replace Java with SQL • 3 years as internal "Oracle Development Expert" • "Career 2.0" started last week 3
  • 4. Review of date/time datatypes • There is no "date" type: always a time element – Closest thing to a date: dte DATE CHECK(dte = TRUNC(dte)) • There are no "formats" • Values from 4712-01-01 00:00:00 BC through 9999-12-31 23:59:59 AD • Any of the above can be used in ranges 4 Datatype Year Month Day Hour Minute Second Fraction Time zone Date Y Y Y Y Y Y Timestamp Y Y Y Y Y Y Y Timestamp with local time zone Y Y Y Y Y Y Y implicit Timestamp with time zone Y Y Y Y Y Y Y explicit
  • 5. Date/time Ranges • Use same data_type for start and end (duh!) • To NULL or not to NULL? – SQL:2011 and 12c allow – Makes NULL mean something – Accounting for NULLs complicates queries and index design – Alternative: extreme values such as DATE '9999-21-31' • "Start" is included in the range, "end" is not – Works for date/time ranges, not just dates – See SQL:2011 standard, 12c Temporal Validity – Allows ranges to "meet" – SQL can't use BETWEEN "start" and "end" 5
  • 6. Allen’s Time Interval Arithmetic 6 Day of month 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 equals B 1 2 B equals A 1 2 Meet Gap "Overlap" Day of month 1 2 3 4Day of month 1 2 3 4 A precedes B 1 2 B preceded by A 3 4 Day of month 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. Temporal Validity: SQL:2011, 12c • Two temporal concepts – "Transaction time": when data committed (flashback) • "Oracle Flashback" Tues. 11:20 (Connor McDonald) – "Valid time": • start / end times determined by users • Past, present or future • "Period": date/time range – Closed-Open: includes start time but not end time – Constraint: start time < end time – NULL start time = valid any time before end – NULL end time = valid from start time on 7
  • 8. What Oracle can do for you 8 create table valid_emp( ename varchar2(64), PERIOD FOR PRESENCE );
  • 9. 9 select column_name, data_type, nullable, hidden_column, virtual_column from user_tab_cols where table_name = 'VALID_EMP' order by internal_column_id; Name Data Type Nullable Hidden Virtual PRESENCE_START TIMESTAMP(6) WITH TIME ZONE Y YES NO PRESENCE_END TIMESTAMP(6) WITH TIME ZONE Y YES NO PRESENCE NUMBER Y YES YES ENAME VARCHAR2 Y NO NO select search_condition from user_constraints where table_name = 'VALID_EMP'; SEARCH_CONDITION PRESENCE_START < PRESENCE_END
  • 10. 10 insert all into valid_emp (ename, presence_start, presence_end) values('Stew', date '1998-03-01', date '2015-12-01') into valid_emp (ename, presence_start, presence_end) values('Stew', date '2016-01-01', date '2050-12-01') select null from dual; select * from valid_emp; ENAME Stew Stew Oops! Hidden columns…
  • 11. 11 select ename, presence_start, presence_end from valid_emp; ENAME PRESENCE_START PRESENCE_END Stew 1998-03-01 2015-12-01 Stew 2016-01-01 2050-12-01
  • 12. 12 select ename, presence_start, presence_end from valid_emp as of period for presence systimestamp; ENAME PRESENCE_START PRESENCE_END select * from table (dbms_xplan.display_cursor(format=>'+PREDICATE')); filter(( (T.PRESENCE_START IS NULL OR SYS_EXTRACT_UTC(T.PRESENCE_START)<=SYS_EXTRACT_UTC(SYSTIMESTAMP(6))) AND (T.PRESENCE_END IS NULL OR SYS_EXTRACT_UTC(T.PRESENCE_END) > SYS_EXTRACT_UTC(SYSTIMESTAMP(6))) ))
  • 13. 13 select ename, presence_start, presence_end from valid_emp as of period for presence to_timestamp_tz('2015-11-01'); exec DBMS_FLASHBACK_ARCHIVE.ENABLE_AT_VALID_TIME ('CURRENT'); select ename, presence_start, presence_end from valid_emp; ENAME PRESENCE_START PRESENCE_END Stew 1998-03-01 2015-12-01 ENAME PRESENCE_START PRESENCE_END
  • 14. Valid Time Range 14 select ename, presence_start, presence_end from valid_emp versions period for presence between to_timestamp_tz('2015-11-01') and to_timestamp_tz('2016-01-01'); ENAME PRESENCE_START PRESENCE_END Stew 1998-03-01 2015-12-01 Stew 2016-01-01 2050-12-01
  • 15. • So what did Oracle do for us? – Notion of "period" – Automatic, hidden column definitions – New query syntax with automatic rewrite • And what did Oracle not do for us? – Query performance? – Temporal constraints? – Query for gaps or overlaps? – Temporal DML? • Now: what can we do with Oracle? 15
  • 16. What we need to do: • Temporal constraints • Performance: indexing • Temporal queries • Temporal DML 16
  • 17. Temporal Constraints • No gaps, no overlaps – Don't use ranges! – "effective date" • "start" = EFF_DATE , "end" = lead(EFF_DATE) over (EFF_DATE) • Temporal primary keys – Use ID + start_date (usually) – ORA-02329: …TIMESTAMP WITH TIME ZONE cannot be unique or primary key • Temporal foreign keys – Child range must be within parent range – "On commit" materialized views (hard to scale) – Triggers (hard to write correctly) 17
  • 18. Temporal DDL & Indexing 18 create table valid_emp( ename varchar2(64), presence_start date, presence_end date not null, CHECK(presence_start < presence_end), period for presence(presence_start, presence_end), CONSTRAINT valid_emp_pk primary key (ename, presence_start) USING INDEX ( CREATE INDEX valid_emp_pk ON valid_emp (ename, presence_start, presence_end) ) );
  • 19. Temporal Queries • Use Analytic functions or 12c MATCH_RECOGNIZE • Typical use cases – find gaps: "free time" in calendars – Merge ranges that "meet" – Merge ranges that "meet" or "overlap" – Join on intersecting date ranges • For simplicity, I assume "not NULL" 19
  • 20. Find Gaps 20 START_DATE END_DATE ---------- ---------- 2007-01-12 2007-01-25 2007-01-20 2007-02-01 2007-02-05 2007-02-10 2007-02-05 2007-02-28 2007-02-10 2007-02-15 2007-03-01 2007-03-02 2007-03-03 2007-03-16 SELECT end_date start_gap, LEAD(start_date) OVER (ORDER BY start_date) end_gap FROM T; START_GAP END_GAP ---------- ---------- 2007-01-25 2007-01-20 2007-02-01 2007-02-05 2007-02-10 2007-02-05 2007-02-28 2007-02-10 2007-02-15 2007-03-01 2007-03-02 2007-03-03 2007-03-16
  • 21. Find Gaps 21 START_DATE END_DATE ---------- ---------- 2007-01-12 2007-01-25 2007-01-20 2007-02-01 2007-02-05 2007-02-10 2007-02-05 2007-02-28 2007-02-10 2007-02-15 2007-03-01 2007-03-02 2007-03-03 2007-03-16 SELECT * FROM ( SELECT MAX(end_date) OVER (ORDER BY start_date) start_gap, LEAD(start_date) OVER (ORDER BY start_date) end_gap FROM T ) WHERE start_gap < end_gap; START_GAP END_GAP ---------- ---------- 2007-01-25 2007-01-20 2007-02-01 2007-02-05 2007-02-28 2007-02-05 2007-02-28 2007-02-10 2007-02-28 2007-03-01 2007-03-02 2007-03-03 2007-03-16
  • 22. PACK: merge ranges that meet 22 ID START END 1 01-01 01-03 2 01-03 01-05 3 02-05 02-28 4 02-10 02-15 5 03-01 03-11 6 03-10 03-12 7 03-1103-21 select * from t match_recognize( order by start_date, end_date measures first(id) "first", last(id) "last", first(start_date) "start", last(end_date) "end" pattern(A* B) define A as end_date = next(start_date) ); first last start end ----- ---- ----- ----- 1 2 01-01 01-05 3 3 02-05 02-28 4 4 02-10 02-15 5 5 03-01 03-11 6 6 03-10 03-12 7 7 03-11 03-21 overlap intervening
  • 23. Merge ranges that meet or overlap 23 ID START END 1 01-01 01-03 2 01-03 01-05 3 02-05 02-28 4 02-10 02-15 5 03-01 03-11 6 03-10 03-12 7 03-1103-21 select * from t match_recognize( order by start_date, end_date measures first(id) "first", last(id) "last", first(start_date) "start", MAX(end_date) "end" pattern(A* B) define A as MAX(end_date) >= next(start_date) ); first last start end ----- ---- ----- ----- 1 2 01-01 01-05 3 4 02-05 02-28 5 7 03-01 03-21 gap gap
  • 24. Join on intersecting ranges • Prerequisite for temporal DML – Join input to table to find affected rows • Use UNION ALL to gather input and affected rows • Use UNPIVOT and DISTINCT to get range boundaries • Use LEAD() to create base ranges • DO LEFT JOIN from base ranges to table + input. 24 1 3 5 7 2 4 St 1 2 3 4 5 St End 1 2 2 3 3 4 4 5 St End Old New 1 2 1-3 2 3 1-3 2-4 3 4 3-5 2-4 4 5 3-5