Dzielenie relacyjne (Relational division) Bazy i hurtownie danych, TWO1, 2010 https://ophelia.cs.put.poznan.pl/webdav/dbdw/students/ dbdw-winter_2010-11/

Slides:



Advertisements
Similar presentations
Relational data objects 1 Lecture 6. Relational data objects 2 Answer to last lectures activity.
Advertisements

Database Languages Chapter 7. The Relational Algebra.
Copyright © 2004 Pearson Education, Inc.. Chapter 15 Algorithms for Query Processing and Optimization.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Discrete Mathematics Lecture 5 Alexander Bukharovich New York University.
Concepts of Database Management Sixth Edition
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Query Processing Presented by Aung S. Win.
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Databases Illuminated
Relational Algebra.  Introduction  Relational Algebra Operations  Projection and Selection  Set Operations  Joins  Division  Tuple Relational Calculus.
Database Management 9. course. Execution of queries.
The Relational Model: Relational Calculus
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Chapter 8 Relational Calculus. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-2 Topics in this Chapter Tuple Calculus Calculus vs. Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Concepts of Database Management Seventh Edition
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
RELATIONAL ALGEBRA CHAPTER 6 1. LECTURE OUTLINE  Unary Relational Operations: SELECT and PROJECT  Relational Algebra Operations from Set Theory  Binary.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
Dividing Mixed Numbers © Math As A Second Language All Rights Reserved next #7 Taking the Fear out of Math
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
IST 210 The Relational Language Todd S. Bacastow January 2004.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
5.3 D IVIDING P OLYNOMIALS Algebra II w/ trig. I. Divide a Polynomial by a monomial: A. this expression means that each term in the numerator shares a.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational tuple calculus.
PREDICATES AND QUANTIFIERS COSC-1321 Discrete Structures 1.
Chapter 13: Query Processing
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
CPSC-310 Database Systems
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Multiplying and Dividing Rational Expressions
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 15 QUERY EXECUTION.
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Database Dr. Roueida Mohammed.
Unit Relational Algebra 1
Presentation transcript:

Dzielenie relacyjne (Relational division) Bazy i hurtownie danych, TWO1, dbdw-winter_ /

References 1.V.M. Matos, R. Grasser, Assessing performance of the relational division operator. Data Base Management 2001, , V.M. Matos, R. Grasser, A Simpler (and Better) SQL Approach to Relational Division, Journal of Information System Education 2002, 13 (2), /11/2010Bazy i hurtownie danych2

Relational Division  The basic operators of the relational algebra: – Union (UNION) – Difference (MINUS/EXCEPT) – Cartesian product – Projection & selection (SELECT... FROM...)  Additional operators added to the relational algebra: – Join  most popular in practice – Rename (AS) – Intersection (INTERSECT) – Division 21/11/2010Bazy i hurtownie danych3

Relational Division  The division operator is less common than select-project-join queries, however, it is applicable to many common queries: – Find suppliers who supply all the engine parts – Find students who have taken all the core courses – Find customers who have ordered all items from a given line of products  The division operator can be also employed in data mining algorithms (e.g., generation of association rules) 21/11/2010Bazy i hurtownie danych4

Informal Definition  The division operator allows verifying whether or not a candidate subject is related to each of the values held in the base set.  The base set is called the divisor (or denominator T2[B]), and the table holding the subject’s data is called the dividend (or nominator T1[A, B]).  The expression T1[A, B]/T2[B] selects the A values from the dividend table T1[A, B], whose B values are a superset of those B values held in the divisor table T2[B]. 21/11/2010Bazy i hurtownie danych5

Informal Definition 21/11/2010Bazy i hurtownie danych T3[A] = T1[A,B] / T2[B] 6

More Practical Example #1 Given the relations ORDERS[SID, PID, QTY] and PRODUCTS[PID, PRICE] find all the stores that ordered at least 10 of all products priced over 15 $ 21/11/2010Bazy i hurtownie danych SIDPIDQTY s2p225 s2p320 s2p420 s1p125 s1p44 s1p312 s1p230 s3p412 s3p214 ORDERS PIDPRICE p110 p225 p318 p420 PRODUCTS ORDERS[SID,PID] / PRODUCTS[PID] = { s2 } 7

More Practical Example #2 Given the relations ORDERS[SID, PID, PMNT] and PRODUCTS[PID, PRICE] we focus on ordered products priced over 15$ and paid either with cash or a credit card. What would be the result of ORDERS[SID, PID, PMNT] / PRODUCTS[PID]? 21/11/2010Bazy i hurtownie danych SIDPIDPMNT s1p1cash s1p2bank s1p2credit s1p3cash s1p4bank s1p3credit s2p2cash s2p2bank s2p3credit ORDERS PIDPRICE p110 p225 p318 p414 PRODUCTS ORDERS[SID,PID,PMNT] / PRODUCTS[PID] = { (s1, credit) } 8

Formal Definition: Relational Algebra Let’s assume that the numerator table T1 always consists of two columns A and B, and the denominator has only one B attribute. Then, the expression T1[A, B]/T2[B] is semantically equivalent to: T1[A, B]/T2[B] = T1[A] – ((T1[A] × T2[B]) – T1[A, B])[A] 21/11/2010Bazy i hurtownie danych9

Formal Definition: Relational Algebra 21/11/2010Bazy i hurtownie danych10

Formal Definition: Tuple-Calculus Using relational tuple-calculus language, the division operator can be rephrased as follows: T1[A, B]/T2[B] = { t1[A] / t1  T1 and for-all t2 (t2  T2  exists t3 (t3  T1 and (t1[A] = t3[A]) and (t2[B] = t3[B]))) } 21/11/2010Bazy i hurtownie danych11

Formal Definition: NFNF Databases (Non First-Normal Form)  Assumption of a NFNF format (fields with sets of atomic values) results in a much simplified definition of the division operator in tuple-calculus (attribute *B in T1 and T2 is defined as a set of atomic values): T1[A, B]/T2[B] = { t1[A] / t1  T1 and t2  T2 and t2[*B]  t1[*B] } 21/11/2010Bazy i hurtownie danych12

Formal Definition: NFNF Databases 21/11/2010Bazy i hurtownie danych13

SQL Implementation: Q0 SELECT A FROM T1 WHERE B IN (SELECT B FROM T2) GROUP BY A HAVING COUNT(*) = (SELECT COUNT(*) FROM T2) 21/11/2010Bazy i hurtownie danych14

SQL Implementation: Q1 (Byzantine Method )  Based on the formal predicate calculus definition modified to fit SQL: – The universal quantifier for-all x (f(x)) replaced by not exists x (not f(x)) – The implication X  Y replaced by (not(X) or Y) T1[A, B]/T2[B] = { t1[A] / t1  T1 and not exists t2 (not(not( t2  T2) or (exists t3 (t3  T1 and (t1[A] = t3[A]) and (t2[B] = t3[B]))))) } 21/11/2010Bazy i hurtownie danych15

SQL Implementation: Q1 Previous definition is equivalent (following De Morgan’s law  not (P or Q) = not P and not Q ) to: T1[A, B]/T2[B] = { t1[A] / t1  T1 and not exists t2 (( t2  T2) and (not exists t3 (t3  T1 and (t1[A] = t3[A]) and (t2[B] = t3[B])))) } 21/11/2010Bazy i hurtownie danych16

SQL Implementation: Q1 SELECT DISTINCT x.A FROM T1 AS x WHERE NOT EXISTS (SELECT * FROM T2 y WHERE NOT EXISTS (SELECT * FROM T1 AS z WHERE (z.A=x.A) AND (z.B=y.B))) 21/11/2010Bazy i hurtownie danych17

SQL Implementation: Q2 Based on the algebraic definition of the division operator and broken into two steps: SELECT DISTINCT y.A, z.B INTO T3 FROM T1 AS y, T2 AS z WHERE NOT EXISTS (SELECT * FROM T1 WHERE (T1.A = y.A) AND (T1.B=z.B)) SELECT DISTINCT A FROM T1 WHERE NOT EXISTS (SELECT * FROM T3 WHERE (T3.A=T1.A)) 21/11/2010Bazy i hurtownie danych T1[A, B]/T2[B] = T1[A] – ((T1[A] × T2[B]) – T1[A, B])[A] 18

SQL Implementation: Q3 Based on the definition for the NFNF and tuple-calculus: SELECT DISTINCT x.A FROM T1 AS x WHERE (SELECT COUNT(*) FROM T2) = (SELECT COUNT(*) FROM T1, T2 WHERE (T1.A=x.A) AND (T1.B=T2.B)) 21/11/2010Bazy i hurtownie danych19

Zero Division  The divide operator is defined in such a way that T1[A,B]/T2[B] produces exactly all A values in T1 each time that T2[B] is empty.  An empty set would be a more appropriate answer  this is how Q0 works. 21/11/2010Bazy i hurtownie danych20

Experimental Evaluation of Q0…Q3  Assume basic structure of tables (T1[A, B], T2[B], integer or char)  Conduct an experiment with the following settings: – number of A-values in T1 = , – number of B-values in T1 = 100, – number of B-values in T2 = 0, 20, 40, 60, 80, 100.  Use provided scripts to generate sample tables and to run specific queries (Q0…Q3).  Observer performance of specific queries (execution time – CPU time).  Collect the observations in a tabular and graphical form. 21/11/2010Bazy i hurtownie danych dbdw-winter_ / 21

Checking Execution Time 21/11/2010Bazy i hurtownie danych Turn SET STATISTICS TIME on (Tools  Options) 22