sql design patterns advanced sql programming idioms
TRANSCRIPT
![Page 1: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/1.jpg)
SQL Design Patterns
Advanced SQL programming idioms
![Page 2: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/2.jpg)
Genesis
C++ worldAdvanced C++ Programming Styles and
Idioms, by James O. Coplien Design Patterns: Elements of Reusable Object-
Oriented Software by Erich Gamma et al
SQLSQL for Smarties by Joe CelkoSQL Cookbook by Anthony Molinaro The Art of SQL
by Stephane Faroult, Peter Robson
![Page 3: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/3.jpg)
What is a SQL Pattern?
A common design vocabulary A documentation and learning aid An adjunct to existing design methods A target for refactoring Large range of granularity -- from very general design principles to language-specific idioms
![Page 4: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/4.jpg)
List of Patterns
CountingConditional summationInteger generatorString/Collection decompositionList AggregateEnumerating pairsEnumerating setsInterval coalesce
![Page 5: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/5.jpg)
Discrete interval samplingUser-defined aggregatePivotSymmetric differenceHistogramSkyline queryRelational divisionOuter unionComplex constraintNested intervalsTransitive closureHierarchical total
![Page 6: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/6.jpg)
Symmetric Difference
A = B ?
Isn’t it Equality operator ?
![Page 7: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/7.jpg)
Venn diagram
A\B
B\AA∩B
(A \ B) ∪ (B \ A)
(A ∪ B) \ (A ∩ B)
![Page 8: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/8.jpg)
SQL Query
( select * from A minus select * from B) union all ( select * from B minus select * from A)
![Page 9: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/9.jpg)
Test
create table A asselect obj# id, name from sys.obj$where rownum < 100000;
create table B asselect obj# id, name from sys.obj$where rownum < 100010;
![Page 10: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/10.jpg)
Execution Statistics
![Page 11: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/11.jpg)
Anti Join Transformation
convert_set_to_join = true:
select * from A
where (col1,col2,…) not in (select col1,col2,… from B)
union all
select * from B
where (col1,col2,…) not in (select col1,col2,… from A)
![Page 12: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/12.jpg)
Execution Statistics
![Page 13: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/13.jpg)
Optimization continued…
CREATE INDEX A_id_name ON A(id, name); CREATE INDEX B_id_name ON B(id, name);
_hash_join_enabled = false_optimizer_sortmerge_join_enabled = falseor/*+ use_nl(@"SEL$74086987" A) use_nl(@"SET$D8486D66" B)*/
![Page 14: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/14.jpg)
Symmetric Difference via Aggregation
select * from ( select id, name, sum(case when src=1 then 1 else 0 end) cnt1, sum(case when src=2 then 1 else 0 end) cnt2 from ( select id, name, 1 src from A union all select id, name, 2 src from B ) group by id, name)where cnt1 <> cnt2
![Page 15: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/15.jpg)
Execution Statistics
![Page 16: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/16.jpg)
Equality checking via Aggregation
1. Is there any difference? (Boolean).2. What are the rows that one table contains, and the other doesn't?
|| orahash 267629
|| orahash 512259|| orahash 334382|| orahash 592731
+++=1523431
![Page 17: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/17.jpg)
Relational Division
Name Steve Pete Kate
x Language
SQL
Java
=
Name Language
Steve SQL
Pete Java
Kate SQL
Steve Java
Pete SQL
Kate Java
JobApplicants
JobRequirements
ApplicantSkills
![Page 18: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/18.jpg)
Dividend, Divisor and Quotient
Name Language Steve SQL
Pete Java
Kate SQL
Kate Java
/
ApplicantSkills
Language
SQL
Java
=
JobRequirements
? Name
Kate
Remainder
![Page 19: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/19.jpg)
Is it a common Pattern?
Not a basic operator in RA or SQLInformally:
“Find job applicants who meet all job requirements”
compare with:
“Find job applicants who meet at least one job requirement”
![Page 20: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/20.jpg)
Set Union Query
Given a set of sets, e.g {{1,3,5},{3,4,5},
{5,6}}Find their union:
SELECT DISTINCT elementFROM Sets
ID ELEMENT 1 1
1 3 1 5 2 3 2 4 2 5 3 5 3 6
Sets
![Page 21: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/21.jpg)
Set Intersection
Given a set of sets, e.g {{1,3,5},{3,4,5},
{5,6}}Find their intersection?
ID ELEMENT 1 1
1 3 1 5 2 3 2 4 2 5 3 5 3 6
Sets
![Page 22: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/22.jpg)
It’s Relational Division Query!
“Find Elements which belong to all sets” compare with:
“Find Elements who belong to at least one set”
ID ELEMENT 1 1 1 3 1 5 2 3 2 4 2 5 3 5 3 6
/
ID
1
2
3
= ELEMENT
5
![Page 23: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/23.jpg)
Implementation (1)
πName(ApplicantSkills) x JobRequirements Name Language
Steve SQL
Pete Java
Kate SQL
Steve Java
Pete SQL
Kate Java
![Page 24: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/24.jpg)
Implementation (2)
Applicants who are not qualified:
πName ( πName(ApplicantSkills) x JobRequirements
- ApplicantSkills
)
![Page 25: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/25.jpg)
Implementation (3)
Final Query:
πName (ApplicantSkills) -
πName ( ApplicantSkills -
πName(ApplicantSkills) x JobRequirements )
![Page 26: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/26.jpg)
Implementation in SQL (1)select distinct Name from ApplicantSkillsminusselect Name from ( select Name, Language from ( select Name from ApplicantSkills ), ( select Language from JobRequirements ) minus select Name, Language from ApplicantSkills)
![Page 27: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/27.jpg)
Implementation in SQL (2)
select distinct Name from ApplicantSkills i
where not exists (
select * from JobRequirements ii
where not exists (
select * from ApplicantSkills iii
where iii.Language = ii.Language
and iii.Name = i.Name
)
)
![Page 28: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/28.jpg)
Implementation in SQL (3)
“Name the applicants such that for all job requirements there exists a corresponding entry in the applicant skills”
“Name the applicants such that there is no job requirement such that there doesn’t exists a corresponding entry in the applicant skills”
“Name the applicants for which the set of all job skills is a subset of their skills”
![Page 29: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/29.jpg)
Implementation in SQL (4)
select distinct Name from ApplicantSkills i
where
(select Language from JobRequirements ii
where ii.Name = i.Name)
in
(select Language from ApplicantSkills)
![Page 30: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/30.jpg)
Implementation in SQL (5)
A ⊆ B A \ B = ∅
select distinct Name from ApplicantSkills iwhere not exists ( select Language from ApplicantSkills minus select Language from JobRequirements ii where ii.Name = i.Name)
![Page 31: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/31.jpg)
Implementation in SQL (6)
select Name from ApplicantSkills s, JobRequirements r
where s.Language = r.Language
group by Name
having count(*) = (select count(*) from
JobRequirements)
![Page 32: SQL Design Patterns Advanced SQL programming idioms](https://reader035.vdocuments.mx/reader035/viewer/2022062300/56649d015503460f949d4b04/html5/thumbnails/32.jpg)
Book