Similar documents
Carnegie Mellon Univ. Dept. of Computer Science Database Applications

CSE 444: Database Internals. Section 4: Query Optimizer

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

SQL: The Query Language Part 1. Relational Query Languages

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

SQL. Chapter 5 FROM WHERE

SQL: Queries, Constraints, Triggers

Review. Relational Query Optimization. Query Optimization Overview (cont) Query Optimization Overview. Cost-based Query Sub-System

Database Applications (15-415)

Introduction to Data Management. Lecture 14 (SQL: the Saga Continues...)

Review: Where have we been?

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

SQL: Queries, Programming, Triggers

CS 186, Fall 2002, Lecture 8 R&G, Chapter 4. Ronald Graham Elements of Ramsey Theory

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #11: Query Op/miza/on

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

Basic form of SQL Queries

CSE 444: Database Internals. Sec2on 4: Query Op2mizer

HW2 out! Administrative Notes

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

CSIT5300: Advanced Database Systems

Course No: 4411 Database Management Systems Fall 2008 Midterm exam

Keys, SQL, and Views CMPSCI 645

Database Applications (15-415)

An SQL query is parsed into a collection of query blocks optimize one block at a time. Nested blocks are usually treated as calls to a subroutine

CompSci 516 Data Intensive Computing Systems

Midterm Exam #2 (Version A) CS 122A Winter 2017

SQL - Lecture 3 (Aggregation, etc.)

CSIT5300: Advanced Database Systems

CIS 330: Applied Database Systems

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Schema for Examples. Query Optimization. Alternative Plans 1 (No Indexes) Motivating Example. Alternative Plans 2 With Indexes

Enterprise Database Systems

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

CITS3240 Databases Mid-semester 2008

Overview of Implementing Relational Operators and Query Evaluation

Experimenting with bags (tables and query answers with duplicate rows):

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of...

Database Systems. October 12, 2011 Lecture #5. Possessed Hands (U. of Tokyo)

Database Systems ( 資料庫系統 )

Database Management Systems. Chapter 5

Database Management Systems. Chapter 5

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

This lecture. Step 1

ATYPICAL RELATIONAL QUERY OPTIMIZER

Introduction to Data Management. Lecture #14 (Relational Languages IV)

CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #5: Sor-ng, Query Processing and Op-miza-on

SQL: The Query Language (Part 1)

IMPORTANT: Circle the last two letters of your class account:

Rewrite INTERSECT using IN

R & G Chapter 13. Implementation of single Relational Operations Choices depend on indexes, memory, stats, Joins Blocked nested loops:

QUERIES, PROGRAMMING, TRIGGERS

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

Introduction to Data Management. Lecture #10 (Relational Calculus, Continued)

Database Management Systems. Chapter 5

Database Systems. Announcement. December 13/14, 2006 Lecture #10. Assignment #4 is due next week.

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

The Database Language SQL (i)

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Συστήματα Διαχείρισης Βάσεων Δεδομένων

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

RELATIONAL ALGEBRA AND CALCULUS

Database Applications (15-415)

SQL SQL SQL SQL SQL SQL SQL. Eugene Wu Fall 2018

Evaluation of Relational Operations: Other Techniques. Chapter 14 Sayyed Nezhadi

UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

Examples of Physical Query Plan Alternatives. Selected Material from Chapters 12, 14 and 15

CS330. Query Processing

CompSci 516 Data Intensive Computing Systems. Lecture 11. Query Optimization. Instructor: Sudeepa Roy

Relational Query Optimization

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Overview of Query Evaluation. Overview of Query Evaluation

SQL Lab Query 5. More SQL Aggregate Queries, Null Values. SQL Lab Query 8. SQL Lab Query 5. SQL Lab Query 8. SQL Lab Query 9

Relational Query Optimization. Highlights of System R Optimizer

SQL: Queries, Programming, Triggers

Database Applications (15-415)

Goals for Today. CS 133: Databases. Relational Model. Multi-Relation Queries. Reason about the conceptual evaluation of an SQL query

Overview of Query Evaluation

Database Applications (15-415)

Overview of Query Processing

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

CompSci 516: Database Systems

Data Science 100. Databases Part 2 (The SQL) Slides by: Joseph E. Gonzalez & Joseph Hellerstein,

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Cover Page. The handle holds various files of this Leiden University dissertation

Advances in Data Management Query Processing and Query Optimisation A.Poulovassilis

!-%/ -%& "-!&"!!!*0**

Relational Query Optimization. Overview of Query Evaluation. SQL Refresher. Yanlei Diao UMass Amherst October 23 & 25, 2007

Advanced Database Management Systems

Data Science 100 Databases Part 2 (The SQL) Previously. How do you interact with a database? 2/22/18. Database Management Systems

The Relational Data Model. Data Model

Cost-based Query Sub-System. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Last Class.

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

Midterm Exam (Version B) CS 122A Spring 2017

Database Management Systems

Implementing Joins 1

Advanced Database Management Systems

Transcription:

SELECT s.name, e.cid FROM student AS s, enrolled AS e WHERE s.sid = e.sid AND e.grade = 'A' p s s.name,e.cid grade='a' s.sid=e.sid p s s.name,e.cid s.sid=e.sid grade='a' student enrolled student enrolled

SELECT s.name, e.cid FROM student AS s, enrolled AS e WHERE s.sid = e.sid AND e.grade = 'A' σ σ

σ σ σ σ (X=Y AND Y=3) X=3 AND Y=3

SELECT s.name, e.cid FROM student AS s, enrolled AS e WHERE s.sid = e.sid AND e.grade = 'A' p s s.name,e.cid s.sid=e.sid grade='a' student enrolled sid,namep p student s.name,e.cid s.sid=e.sid p sid,cid s grade='a' enrolled

SELECT * FROM table WHERE 1 = 0 SELECT * FROM table WHERE 1 = 1 SELECT A1.* FROM A AS A1 JOIN A AS A2 ON A1.id = A2.id

SELECT * FROM A AS A1 WHERE EXISTS(SELECT * FROM A AS A2 WHERE A1.id = A2.id) SELECT * FROM A WHERE val BETWEEN 1 AND 100 AND val BETWEEN 50 AND 150

R S = S R (R S) T = R (S T)

4 n

ANALYZE ANALYZE TABLE

R N R V(A,R) A

SC(A,R) A N R / V(A,R)

SELECT * FROM A WHERE id = 123 SELECT * FROM A WHERE val > 1000 SELECT * FROM A WHERE age = 30 AND status = 'Lit'

sel P

sel P

V(age, people) N R A=constant sel(a=constant) = SC(P) / V(A,R) SELECT * FROM people WHERE age = 2

V(age, people) N R A=constant sel(a=constant) = SC(P) / V(A,R) sel(age=2) = SELECT * FROM people WHERE age = 2

V(age, people) N R A=constant sel(a=constant) = SC(P) / V(A,R) sel(age=2) = SELECT * FROM people WHERE age = 2 V(age,R)=5

V(age, people) N R A=constant sel(a=constant) = SC(P) / V(A,R) sel(age=2) = 1/5 SC(age=2)=1 SELECT * FROM people WHERE age = 2 V(age,R)=5

sel(a>=a) = (A max a) / (A max A min ) sel(age>=2) SELECT * FROM people WHERE age >= 2

sel(a>=a) = (A max a) / (A max A min ) sel(age>=2) = (4 2) / (4 0) = 1/2 SELECT * FROM people WHERE age >= 2 age min = 0 age max = 4

sel(not P) = 1 sel(p) sel(age!= 2) SELECT * FROM people WHERE age!= 2

sel(not P) = 1 sel(p) sel(age!= 2) SELECT * FROM people WHERE age!= 2 SC(age=2)=1

sel(not P) = 1 sel(p) sel(age!= 2) = 1 (1/5) = 4/5 SELECT * FROM people WHERE age!= 2 SC(age!=2)=2 SC(age!=2)=2

sel(p1 P2) = sel(p1) sel(p2) sel(age=2 name LIKE 'A%') SELECT * FROM people WHERE age = 2 AND name LIKE 'A%'

sel(p1 P2) = sel(p1) + sel(p2) sel(p1 P2) = sel(p1) + sel(p2) sel(p1) sel(p2) sel(age=2 OR name LIKE 'A%') SELECT * FROM people WHERE age = 2 OR name LIKE 'A%'

sel(p1 P2) = sel(p1) + sel(p2) sel(p1 P2) = sel(p1) + sel(p2) sel(p1) sel(p2) sel(age=2 OR name LIKE 'A%') SELECT * FROM people WHERE age = 2 OR name LIKE 'A%'

R S S R

R cols S cols ={A} A R S estsize N R N / V(A,S) S estsize N R N S / V(A,R) estsize N R N S / max({v(a,s), V(A,R)})

# of occurrences Distinct values of attribute

SELECT AVG(age) FROM people WHERE age > 50 id name age status 1001 Obama 56 Rested 1002 Kanye 40 Weird 1003 Tupac 25 Dead 1004 Bieber 23 Crunk 1005 Andy 36 Lit

sel(age>50) = 1001 Obama 56 Rested 1003 Tupac 25 Dead 1005 Andy 36 Lit = 1/3 SELECT AVG(age) FROM people WHERE age > 50 id name age status 1001 Obama 56 Rested 1002 Kanye 40 Weird 1003 Tupac 25 Dead 1004 Bieber 23 Crunk 1005 Andy 36 Lit

A B C D C A B D C D A B

A B C D X A C B D C X D A B

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b R S T R S T R S T T S R

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b Hash Join R.a=S.a R S T R S T SortMerge Join R.a=S.a SortMerge Join T.b=S.b R S T Hash Join T.b=S.b T S R

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b Hash Join R.a=S.a R S T R S T R S T Hash Join T.b=S.b T S R

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b Hash Join R.a=S.a R S T Hash Join S.b=T.b R S T SortMerge Join S.b=T.b SortMerge Join S.a=R.a R S T Hash Join T.b=S.b T S R Hash Join S.a=R.a

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b Hash Join R.a=S.a R S T Hash Join S.b=T.b R S T SortMerge Join S.a=R.a R S T Hash Join T.b=S.b T S R

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b R S T R S T SortMerge Join S.a=R.a R S T Hash Join T.b=S.b T S R

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b R T S R T S S S R T T S T R R S R T Prune plans with cross-products immediately!

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b R T S R T S S S R T T S T X Prune X R R S R T plans with cross-products immediately!

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b R T S R T S S S R T T S T X Prune X R R S R T plans with cross-products immediately!

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b R S T R S T R S T Do this for the other plans. T T R S R S

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b R S T R S T R S T Do this for the other plans. T T R S R S

SELECT * FROM R, S, T WHERE R.a = S.a AND S.b = T.b R S T Do this for the other plans.

SELECT name FROM sailors AS S WHERE EXISTS ( SELECT * FROM reserves AS R WHERE S.sid = R.sid AND R.day = '2017-10-11' ) SELECT name FROM sailors AS S, reserves AS R WHERE S.sid = R.sid AND R.day = '2017-10-11'

SELECT S.sid, MIN(R.day) FROM sailors S, reserves R, boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red' AND S.rating = (SELECT MAX(S2.rating) FROM sailors S2) GROUP BY S.sid HAVING COUNT(*) > 1

SELECT S.sid, MIN(R.day) FROM sailors S, reserves R, boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red' AND S.rating = (SELECT MAX(S2.rating) FROM sailors S2) GROUP BY S.sid HAVING COUNT(*) > 1

SELECT MAX(rating) FROM sailors SELECT S.sid, MIN(R.day) FROM sailors S, reserves R, boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red' AND S.rating = (SELECT MAX(S2.rating) FROM sailors S2) GROUP BY S.sid HAVING COUNT(*) > 1

SELECT MAX(rating) FROM sailors SELECT S.sid, MIN(R.day) FROM sailors S, reserves R, boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red' AND S.rating = (SELECT MAX(S2.rating) GROUP BY S.sid HAVING COUNT(*) > 1 ### FROM sailors S2)

SELECT MAX(rating) FROM sailors SELECT S.sid, MIN(R.day) FROM sailors S, reserves R, boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red' AND S.rating = (SELECT MAX(S2.rating) GROUP BY S.sid HAVING COUNT(*) > 1 ### FROM sailors S2)