SQL Design Patterns Advanced SQL programming idioms.

Slides:



Advertisements
Similar presentations
The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL.
Advertisements

A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Sets of Real Numbers The language of set notation.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
DB Review Session. ER Diagrams 1 Where does the salary attribute belong? Actor id name address birthday Acted In Movie title type year salary.
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Hierarchies & Trees in SQL by Joe Celko copyright 2008.
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Query Execution Optimizing Performance. Resolving an SQL query Since our SQL queries are very high level, the query processor must do a lot of additional.
COMP 451/651 Optimizing Performance
Operations on Sets Union Intersection: Two sets are disjoint if their intersection is the null set. Difference A - B. Complement of a set A.
Discrete Mathematics Lecture 4 Harper Langston New York University.
Discrete Structures Chapter 3 Set Theory Nurul Amelina Nasharuddin Multimedia Department.
CS 4432query processing1 CS4432: Database Systems II.
Lecture 3 Operations on Sets CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Relational Algebra.
Midterm 1 Concepts Relational Algebra (DB4) SQL Querying and updating (DB5) Constraints and Triggers (DB11) Unified Modeling Language (DB9) Relational.
Chapter 3 Section 3.4 Relational Database Operators
Copyright © Curt Hill The Relational Algebra What operations can be done?
April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
2.3 – Set Operations and Cartesian Products Intersection of Sets: The intersection of sets A and B is the set of elements common to both A and B. A  B.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Discrete Structure Sets. 2 Set Theory Set: Collection of objects (“elements”) a  A “a is an element of A” “a is a member of A” a  A “a is not an element.
CompSci 102 Discrete Math for Computer Science
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
Advanced Relational Algebra & SQL (Part1 )
Set Operations Chapter 2 Sec 3. Union What does the word mean to you? What does it mean in mathematics?
Mathematical Preliminaries
CSNB143 – Discrete Structure Topic 1 - Set. Topic 1 - Sets Learning Outcomes – Student should be able to identify sets and its important components. –
Chapter 2 With Question/Answer Animations. Section 2.1.
DISCRETE COMPUTATIONAL STRUCTURES
CSNB143 – Discrete Structure Topic 3 – Matrices. Learning Outcomes Students should understand all matrices operations. Students should be able to differentiate.
Basic Definitions of Set Theory Lecture 23 Section 5.1 Wed, Mar 8, 2006.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Basic Definitions of Set Theory Lecture 24 Section 5.1 Fri, Mar 2, 2007.
CS422 Principles of Database Systems Introduction to Query Processing Chengyu Sun California State University, Los Angeles.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Basic Definitions of Set Theory Lecture 23 Section 5.1 Mon, Feb 21, 2005.
Set Operations Section 2.2.
Discrete Mathematics Lecture # 10 Venn Diagram. Union  Let A and B be subsets of a universal set U. The union of sets A and B is the set of all elements.
Section 1.2 – 1.3 Outline Intersection  Disjoint Sets (A  B=  ) AND Union  OR Universe The set of items that are possible for membership Venn Diagrams.
Implementation of Vector Space Model March 27, 2006.
Lecture 5 Set Theory. Plan of lecture Why set theory? Sets and their properties Membership and definition of sets “Famous” sets Types of variables and.
Chapter 2 1. Chapter Summary Sets (This Slide) The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Chapter 5 Relations and Operations
CHAPTER 3 SETS, BOOLEAN ALGEBRA & LOGIC CIRCUITS
COMP3017 Advanced Databases
Unit-III Algebraic Structures
COP Introduction to Database Structures
Relations and Digraphs
Prepared by : Ankit Patel (226)
CS1022 Computer Programming & Principles
Lecture 2 The Relational Model
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Session – 2 SETS & Operations of SETS
The Relational Algebra
Chapter Sets &Venn Diagrams.
The Relational Algebra
Implementation of Relational Operations
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
2.1 – Symbols and Terminology
Instructor: Aaron Roth
Sets, Unions, Intersections, and Complements
SQL: The Query Language (Part III)
Presentation transcript:

SQL Design Patterns Advanced SQL programming idioms

Genesis C++ world Advanced C++ Programming Styles and Idioms, by James O. Coplien Design Patterns: Elements of Reusable Object- Oriented Software by Erich Gamma et al SQL SQL for Smarties by Joe Celko SQL Cookbook by Anthony Molinaro The Art of SQL by Stephane Faroult, Peter Robson

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

List of Patterns Counting Conditional summation Integer generator String/Collection decomposition List Aggregate Enumerating pairs Enumerating sets Interval coalesce

Discrete interval sampling User-defined aggregate Pivot Symmetric difference Histogram Skyline query Relational division Outer union Complex constraint Nested intervals Transitive closure Hierarchical total

Symmetric Difference A = B ? Isn ’ t it Equality operator ?

Venn diagram A\B B\A A∩BA∩B (A \ B) ∪ (B \ A) (A ∪ B) \ (A ∩ B)

SQL Query ( select * from A minus select * from B ) union all ( select * from B minus select * from A )

Test create table A as select obj# id, name from sys.obj$ where rownum < ; create table B as select obj# id, name from sys.obj$ where rownum < ;

Execution Statistics

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)

Execution Statistics

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 = false or /*+ A) B)*/

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

Execution Statistics

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 ||orahash ||orahash334382||orahash =

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

Dividend, Divisor and Quotient Name Language Steve SQL Pete Java Kate SQL Kate Java / ApplicantSkills Language SQL Java = JobRequirements ? Name Kate Remainder

Is it a common Pattern? Not a basic operator in RA or SQL Informally: “ Find job applicants who meet all job requirements ” compare with: “ Find job applicants who meet at least one job requirement ”

Set Union Query Given a set of sets, e.g {{1,3,5},{3,4,5},{5,6}} Find their union: SELECT DISTINCT element FROM Sets ID ELEMENT Sets

Set Intersection Given a set of sets, e.g {{1,3,5},{3,4,5},{5,6}} Find their intersection? ID ELEMENT Sets

It ’ s Relational Division Query! “ Find Element s which belong to all sets ” compare with: “ Find Element s who belong to at least one set ” ID ELEMENT / ID = ELEMENT 5

Implementation (1) π Name (ApplicantSkills) x JobRequirements Name Language Steve SQL Pete Java Kate SQL Steve Java Pete SQL Kate Java

Implementation (2) Applicants who are not qualified: π Name ( π Name (ApplicantSkills) x JobRequirements - ApplicantSkills )

Implementation (3) Final Query: π Name (ApplicantSkills) - π Name ( ApplicantSkills - π Name (ApplicantSkills) x JobRequirements )

Implementation in SQL (1) select distinct Name from ApplicantSkills minus select Name from ( select Name, Language from ( select Name from ApplicantSkills ), ( select Language from JobRequirements ) minus select Name, Language from ApplicantSkills )

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 )

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 ”

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)

Implementation in SQL (5) A ⊆ B  A \ B = ∅ select distinct Name from ApplicantSkills i where not exists ( select Language from ApplicantSkills minus select Language from JobRequirements ii where ii.Name = i.Name )

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)

Book