Download presentation
Presentation is loading. Please wait.
1
CPSC-608 Database Systems
Fall 2017 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #10
2
What Does DBMS Do? Prepare a collection A of efficient algorithms for operations in relational algebra; For a given database program P: 1. understand the program P; 2. translate the program P into an expression E in relational algebra; 3. convert E into an algorithm using algorithms in the collection A; take care of issues in optimization, consistency, and security. √
3
What Does DBMS Do? understand the program P
An input database program P understand the program P parse tree translate the program P into an expression E in relational algebra expression tree convert E into an algorithm using algorithms in the collection C Machine executable code
4
What Does DBMS Do? understand the program P
An input database program P understand the program P parse tree translate the program P into an expression E in relational algebra expression tree logic query plan convert E into an algorithm using algorithms in the collection C Machine executable code
6
The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions;
7
The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT.
8
The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C
9
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
10
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
11
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
12
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
13
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
14
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
15
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
16
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
17
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
18
π σC × …… The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. π \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … A1 … Ak select-statement SELECT FROM select- List L table- List T WHERE conditions A1 … Ak T1 … Th C σC × …… exp. tree for T1 exp. tree for Th
19
The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. SELECT c FROM S(a,b), T(b,c) WHERE S.b = T.b AND a>4;
20
The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. SELECT c FROM S(a,b), T(b,c) WHERE S.b = T.b AND a>4; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <column-name> <tbl-name> , S(a,b) <b-term> <b-facor> and T(b,c) <b-primary> <comp-pred> <exp> <co-op> = <term> S.b T.b > a 4 <integer> c
21
The most nontrivial statement:
The most nontrivial statement: SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; If only simple relations and conditions are involved: Make a cross product of the relations from FROM; Apply the select based on the condition given in WHERE; Apply the projection given in SELECT. SELECT c FROM S(a,b), T(b,c) WHERE S.b = T.b AND a>4; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <column-name> <tbl-name> , S(a,b) <b-term> <b-facor> and T(b,c) <b-primary> <comp-pred> <exp> <co-op> = <term> S.b T.b > a 4 <integer> c S(a,b) T(b,c) c × π σ S.b=T.b AND a>4
22
DDL Statements CREATE TABLE <name> (<list of elements>); CREATE VIEW <name> AS <table construction>; CREATE ASSERTION <name> CHECK (<condition>); CREATE TRIGGER <name> CREATE INDEX <name> ON <table>(<attribute-list>); DROP TABLE <name>; ALTER TABLE <name> ADD <attribute declaration>; ALTER TABLE <name> DROP <attribute>; DML Statements SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; (subquery) UNION (subquery); (subquery) INTERSECT (subquery); (subquery) EXCEPT (subquery); R CROSS JOIN S; R NATURAL JOIN S; R JOIN S ON <condition>; INSERT INTO <relation> VALUES (<list of values>); DELETE FROM <relation> WHERE <condition>; UPDATE <relation> SET <list of attribute assignments> WHERE <condition on tuples>;
23
DDL Statements CREATE TABLE <name> (<list of elements>); CREATE VIEW <name> AS <table construction>; CREATE ASSERTION <name> CHECK (<condition>); CREATE TRIGGER <name> CREATE INDEX <name> ON <table>(<attribute-list>); DROP TABLE <name>; ALTER TABLE <name> ADD <attribute declaration>; ALTER TABLE <name> DROP <attribute>; DML Statements SELECT select-list FROM from-list WHERE conditions GROUP BY group-list HAVING conditions; (subquery) UNION (subquery); (subquery) INTERSECT (subquery); (subquery) EXCEPT (subquery); R CROSS JOIN S; R NATURAL JOIN S; R JOIN S ON <condition>; INSERT INTO <relation> VALUES (<list of values>); DELETE FROM <relation> WHERE <condition>; UPDATE <relation> SET <list of attribute assignments> WHERE <condition on tuples>;
24
syntax rules TinySQL grammar Syntax Rules
statement ::= create-table-statement | drop-table-statement | select-statement | delete-statement | insert-statement create-table-statement ::= CREATE TABLE table-name(attribute-type-list) attribute-type-list ::= attribute-name data-type | attribute-name data-type, attribute-type-list data-type ::= INT | STR20 drop-table-statement ::= DROP TABLE table-name select-statement ::= SELECT [DISTINCT] select-list FROM table-list [WHERE search-condition] [ORDER BY column-name] select-list ::= * | select-sublist select-sublist ::= column-name | column-name, select-sublist table-list ::= table-name | table-name, table-list delete-statement ::= DELETE FROM table-name [WHERE search-condition] insert-statement ::= INSERT INTO table-name(attribute-list) insert-tuples insert-tuples ::= VALUES (value-list) | select-statement attribute-list ::= attribute-name | attribute-name, attribute-list value ::= literal | integer | NULL value-list ::= value | value, value-list search-condition ::= boolean-term | boolean-term OR search-condition boolean-term ::= boolean-factor | boolean-factor AND boolean-term boolean-factor ::= [NOT] boolean-primary boolean-primary ::= comparison-predicate | "[" search-condition "]" comparison-predicate ::= expression comp-op expression expression ::= term | term + expression | term - expression term ::= column-name | literal | integer | (expression) TinySQL grammar syntax rules
25
syntax rules TinySQL grammar Syntax Rules
statement ::= create-table-statement | drop-table-statement | select-statement | delete-statement | insert-statement create-table-statement ::= CREATE TABLE table-name(attribute-type-list) attribute-type-list ::= attribute-name data-type | attribute-name data-type, attribute-type-list data-type ::= INT | STR20 drop-table-statement ::= DROP TABLE table-name select-statement ::= SELECT [DISTINCT] select-list FROM table-list [WHERE search-condition] [ORDER BY column-name] select-list ::= * | select-sublist select-sublist ::= column-name | column-name, select-sublist table-list ::= table-name | table-name, table-list delete-statement ::= DELETE FROM table-name [WHERE search-condition] insert-statement ::= INSERT INTO table-name(attribute-list) insert-tuples insert-tuples ::= VALUES (value-list) | select-statement attribute-list ::= attribute-name | attribute-name, attribute-list value ::= literal | integer | NULL value-list ::= value | value, value-list search-condition ::= boolean-term | boolean-term OR search-condition boolean-term ::= boolean-factor | boolean-factor AND boolean-term boolean-factor ::= [NOT] boolean-primary boolean-primary ::= comparison-predicate | "[" search-condition "]" comparison-predicate ::= expression comp-op expression expression ::= term | term + expression | term - expression term ::= column-name | literal | integer | (expression) TinySQL grammar syntax rules
26
syntax rules TinySQL grammar Syntax Rules
statement ::= create-table-statement | drop-table-statement | select-statement | delete-statement | insert-statement create-table-statement ::= CREATE TABLE table-name(attribute-type-list) attribute-type-list ::= attribute-name data-type | attribute-name data-type, attribute-type-list data-type ::= INT | STR20 drop-table-statement ::= DROP TABLE table-name select-statement ::= SELECT [DISTINCT] select-list FROM table-list [WHERE search-condition] [ORDER BY column-name] select-list ::= * | select-sublist select-sublist ::= column-name | column-name, select-sublist table-list ::= table-name | table-name, table-list delete-statement ::= DELETE FROM table-name [WHERE search-condition] insert-statement ::= INSERT INTO table-name(attribute-list) insert-tuples insert-tuples ::= VALUES (value-list) | select-statement attribute-list ::= attribute-name | attribute-name, attribute-list value ::= literal | integer | NULL value-list ::= value | value, value-list search-condition ::= boolean-term | boolean-term OR search-condition boolean-term ::= boolean-factor | boolean-factor AND boolean-term boolean-factor ::= [NOT] boolean-primary boolean-primary ::= comparison-predicate | "[" search-condition "]" comparison-predicate ::= expression comp-op expression expression ::= term | term + expression | term - expression term ::= column-name | literal | integer | (expression) TinySQL grammar syntax rules
27
syntax rules TinySQL grammar Syntax Rules
statement ::= create-table-statement | drop-table-statement | select-statement | delete-statement | insert-statement create-table-statement ::= CREATE TABLE table-name(attribute-type-list) attribute-type-list ::= attribute-name data-type | attribute-name data-type, attribute-type-list data-type ::= INT | STR20 drop-table-statement ::= DROP TABLE table-name select-statement ::= SELECT [DISTINCT] select-list FROM table-list [WHERE search-condition] [ORDER BY column-name] select-list ::= * | select-sublist select-sublist ::= column-name | column-name, select-sublist table-list ::= table-name | table-name, table-list delete-statement ::= DELETE FROM table-name [WHERE search-condition] insert-statement ::= INSERT INTO table-name(attribute-list) insert-tuples insert-tuples ::= VALUES (value-list) | select-statement attribute-list ::= attribute-name | attribute-name, attribute-list value ::= literal | integer | NULL value-list ::= value | value, value-list search-condition ::= boolean-term | boolean-term OR search-condition boolean-term ::= boolean-factor | boolean-factor AND boolean-term boolean-factor ::= [NOT] boolean-primary boolean-primary ::= comparison-predicate | "[" search-condition "]" comparison-predicate ::= expression comp-op expression expression ::= term | term + expression | term - expression term ::= column-name | literal | integer | (expression) TinySQL grammar syntax rules
28
Sample (real) SQL Programs
29
Sample (real) SQL Programs
SELECT sub.* FROM ( SELECT * FROM tutorial.sf_crime_incidents_2014_01 WHERE day_of_week = 'Friday' ) sub WHERE sub.resolution = 'NONE'
30
Sample (real) SQL Programs
SELECT sub.* FROM ( SELECT * FROM tutorial.sf_crime_incidents_2014_01 WHERE day_of_week = 'Friday' ) sub WHERE sub.resolution = 'NONE' Subqueries may appear in the FROM clause
31
Sample (real) SQL Programs
SELECT sub.* FROM ( SELECT * FROM tutorial.sf_crime_incidents_2014_01 WHERE day_of_week = 'Friday' ) sub WHERE sub.resolution = 'NONE' SELECT * FROM ReplA A WHERE A.ID IN (SELECT sum(B.ID2) OVER() FROM ReplB AS B WHERE A.ID2 = B.ID); Subqueries may appear in the FROM clause
32
Sample (real) SQL Programs
SELECT sub.* FROM ( SELECT * FROM tutorial.sf_crime_incidents_2014_01 WHERE day_of_week = 'Friday' ) sub WHERE sub.resolution = 'NONE' SELECT * FROM ReplA A WHERE A.ID IN (SELECT sum(B.ID2) OVER() FROM ReplB AS B WHERE A.ID2 = B.ID); Subqueries may appear in the FROM clause Subqueries may appear in the WHERE clause
33
Sample (real) SQL Programs
SELECT sub.* FROM ( SELECT * FROM tutorial.sf_crime_incidents_2014_01 WHERE day_of_week = 'Friday' ) sub WHERE sub.resolution = 'NONE' SELECT * FROM ReplA A WHERE A.ID IN (SELECT sum(B.ID2) OVER() FROM ReplB AS B WHERE A.ID2 = B.ID); SELECT DISTINCT LastName, FirstName, e.EmployeeKey FROM DimEmployee e JOIN FactResellerSales s ON e.EmployeeKey = s.EmployeeKey WHERE ProductKey IN (SELECT ProductKey FROM DimProduct WHERE ProductSubcategoryKey IN (SELECT ProductSubcategoryKey FROM DimProductSubcategory WHERE EnglishProductSubcategoryName LIKE '%Bikes')) ORDER BY LastName Subqueries may appear in the FROM clause Subqueries may appear in the WHERE clause
34
Sample (real) SQL Programs
SELECT sub.* FROM ( SELECT * FROM tutorial.sf_crime_incidents_2014_01 WHERE day_of_week = 'Friday' ) sub WHERE sub.resolution = 'NONE' SELECT * FROM ReplA A WHERE A.ID IN (SELECT sum(B.ID2) OVER() FROM ReplB AS B WHERE A.ID2 = B.ID); SELECT DISTINCT LastName, FirstName, e.EmployeeKey FROM DimEmployee e JOIN FactResellerSales s ON e.EmployeeKey = s.EmployeeKey WHERE ProductKey IN (SELECT ProductKey FROM DimProduct WHERE ProductSubcategoryKey IN (SELECT ProductSubcategoryKey FROM DimProductSubcategory WHERE EnglishProductSubcategoryName LIKE '%Bikes')) ORDER BY LastName Subqueries may appear in the FROM clause Subqueries may appear in the WHERE clause Subqueries may appear in both FROM/WHERE clauses, recursively
35
SQL-92 Grammar <from clause> ::= FROM <table list>
<table list> ::= <table reference> | <table reference>, <table list> <table reference> ::= <table name> | <derived table> | <joined table> <derived table> ::= <table subquery> <where clause> ::= WHERE <search condition> <search condition> ::= <boolean term> | …… <boolean term> ::= <boolean factor> | …… <boolean factor> ::= [ NOT ] <boolean test> <boolean test> ::= <boolean primary> [ IS [ NOT ] <truth value> ] <boolean primary> ::= <predicate> | …… <predicate> ::= <comparison predicate> | <in predicate> | <exists predicate> | …… <in predicate> ::= <row value constructor> [ NOT ] IN <in predicate value> <in predicate value> ::= <table subquery> | …… <exists predicate> ::= EXISTS <table subquery>
36
SQL-92 Grammar <from clause> ::= FROM <table list>
<table list> ::= <table reference> | <table reference>, <table list> <table reference> ::= <table name> | <derived table> | <joined table> <derived table> ::= <table subquery> <where clause> ::= WHERE <search condition> <search condition> ::= <boolean term> | …… <boolean term> ::= <boolean factor> | …… <boolean factor> ::= [ NOT ] <boolean test> <boolean test> ::= <boolean primary> [ IS [ NOT ] <truth value> ] <boolean primary> ::= <predicate> | …… <predicate> ::= <comparison predicate> | <in predicate> | <exists predicate> | …… <in predicate> ::= <row value constructor> [ NOT ] IN <in predicate value> <in predicate value> ::= <table subquery> | …… <exists predicate> ::= EXISTS <table subquery> FROM clause allows subqueries.
37
SQL-92 Grammar <from clause> ::= FROM <table list>
<table list> ::= <table reference> | <table reference>, <table list> <table reference> ::= <table name> | <derived table> | <joined table> <derived table> ::= <table subquery> <where clause> ::= WHERE <search condition> <search condition> ::= <boolean term> | …… <boolean term> ::= <boolean factor> | …… <boolean factor> ::= [ NOT ] <boolean test> <boolean test> ::= <boolean primary> [ IS [ NOT ] <truth value> ] <boolean primary> ::= <predicate> | …… <predicate> ::= <comparison predicate> | <in predicate> | …… <in predicate> ::= <row value constructor> [ NOT ] IN <in predicate value> <in predicate value> ::= <table subquery> | …… FROM clause allows subqueries. WHERE clause allows subqueries.
38
The most nontrivial statement: select-from-where
The most nontrivial statement: select-from-where If only simple relations and conditions are involved. How do we handle these complicated situations ? \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … SELECT c FROM S(a,b), T(b,c) WHERE S.b = T.b AND a>4; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <column-name> <tbl-name> , S(a,b) <b-term> <b-facor> and T(b,c) <b-primary> <comp-pred> <exp> <co-op> = <term> S.b T.b > a 4 <integer> c S(a,b) T(b,c) c × π σ S.b=T.b AND a>4
39
subqueries in the FROM clause
subqueries in the FROM clause SELECT c FROM S(a,b), (SELECT * FROM T(b,c) WHERE c = 10) t WHERE S.b = t.b AND a>4;
40
subqueries in the FROM clause
subqueries in the FROM clause SELECT c FROM S(a,b), (SELECT * FROM T(b,c) WHERE c = 10) t WHERE S.b = t.b AND a>4; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> , <tbl-list> <b-term> <column-name> S(a,b) <tbl-name> <b-facor> <b-term> and c <b-primary> <b-facor> <comp-pred> <b-primary> <exp> <co-op> <exp> <comp-pred> <term> <term> = <exp> <co-op> <exp> <column-name> <column-name> <term> <term> > S.b T.b <column-name> <integer> a 4 parse tree for (SELECT * FROM T(b,c) WHERE c = 10) t
41
subqueries in the FROM clause
subqueries in the FROM clause SELECT c FROM S(a,b), (SELECT * FROM T(b,c) WHERE c = 10) t WHERE S.b = t.b AND a>4; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <column-name> c <tbl-name> , S(a,b) <b-term> <b-facor> and <b-primary> <comp-pred> <exp> <co-op> = <term> S.b T.b > a 4 <integer> t × π σ S.b=t.b AND a>4 parse tree for (SELECT * FROM T(b,c) WHERE c = 10) t logic plan for
42
subqueries in the FROM clause
subqueries in the FROM clause \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … SELECT c FROM S(a,b), (SELECT * FROM T(b,c) WHERE c = 10) t WHERE S.b = t.b AND a>4; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> , <tbl-list> <b-term> <column-name> S(a,b) <tbl-name> <b-facor> <b-term> and c <b-primary> <b-facor> S(a,b) t c × π σ S.b=t.b AND a>4 <comp-pred> <b-primary> <exp> <co-op> <exp> <comp-pred> <term> <term> = <exp> <co-op> <exp> <column-name> <column-name> <term> <term> > S.b T.b <column-name> <integer> a 4 parse tree for (SELECT * FROM T(b,c) WHERE c = 10) t logic plan for (SELECT * FROM T(b,c) WHERE c = 10) t
43
subqueries in the WHERE clause
44
subqueries in the WHERE clause
subqueries in the WHERE clause SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ;
45
subqueries in the WHERE clause
subqueries in the WHERE clause SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ;
46
subqueries in the WHERE clause
subqueries in the WHERE clause SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … Can we make a logic query plan like this? S(a,b) b π σ S.a > ANY (SELECT c FROM T(c,d) WHERE d= 10)
47
subqueries in the WHERE clause
subqueries in the WHERE clause SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … Can we make a logic query plan like this? S(a,b) b π σ S.a > ANY (SELECT c FROM T(c,d) WHERE d= 10) NO! Conditions in σ involved in subqueries can cause significant inefficiency and/or even logic inconsistency
48
Then what will we do? subqueries in the WHERE clause
subqueries in the WHERE clause SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; \\ Parse tree to Exp tree: for SFW 1. create a π node; 2. collect the attributes in child-2 and attach them to the π node; 3. IF child-5 ≠ NULL THEN create a σ node and make it the child of the π node; get the conditions in child-6 and attach it to the σ node; 4. create a × node and make it the child of the σ node; 5. make the tables in child-4 the children of the × node; 6. IF child-7 ≠ NULL THEN … Can we make a logic query plan like this? S(a,b) b π σ S.a > ANY (SELECT c FROM T(c,d) WHERE d= 10) Then what will we do?
49
subqueries in the WHERE clause
subqueries in the WHERE clause EXIST R s IN R s > ALL R s > ANY R SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ;
50
subqueries in the WHERE clause
subqueries in the WHERE clause EXIST R s IN R s > ALL R s > ANY R SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; Each operation has a different conversion procedure. In general, Convert the subquery into a logic plan; Collect the attributes for the condition; Make a cross product with the “main” relations; Apply the conditions in the cross product.
51
subqueries in the WHERE clause
subqueries in the WHERE clause 1. Convert the subquery into a logic plan; 2. Collect the attributes for the condition; 3. Make a cross product with the “main” relations; 4. Apply the conditions in the cross product. SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ;
52
subqueries in the WHERE clause
subqueries in the WHERE clause 1. Convert the subquery into a logic plan; 2. Collect the attributes for the condition; 3. Make a cross product with the “main” relations; 4. Apply the conditions in the cross product. SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> <b-term> <column-name> <b-facor> S(a,b) b <b-primary> <comp-pred> <exp> <co-op> <exp> <term> > any <column-name> S.a Parse tree for SELECT c FROM T(c,d) WHERE d = 10
53
subqueries in the WHERE clause
subqueries in the WHERE clause 1. Convert the subquery into a logic plan; 2. Collect the attributes for the condition; 3. Make a cross product with the “main” relations; 4. Apply the conditions in the cross product. SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> <b-term> <column-name> <b-facor> S(a,b) b <b-primary> <comp-pred> <exp> <co-op> <exp> <term> > any <column-name> S.a Parse tree for SELECT c FROM T(c,d) WHERE d = 10 logic plan for SELECT c FROM T(c,d) WHERE d = 10
54
subqueries in the WHERE clause
subqueries in the WHERE clause 1. Convert the subquery into a logic plan; 2. Collect the attributes for the condition; 3. Make a cross product with the “main” relations; 4. Apply the conditions in the cross product. SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> <b-term> <column-name> <b-facor> S(a,b) b <b-primary> <comp-pred> σ d = 10 c π T(c,d) <exp> <co-op> <exp> <term> > any <column-name> S.a Parse tree for SELECT c FROM T(c,d) WHERE d = 10 logic plan for SELECT c FROM T(c,d) WHERE d = 10
55
subqueries in the WHERE clause
subqueries in the WHERE clause 1. Convert the subquery into a logic plan; 2. Collect the attributes for the condition; 3. Make a cross product with the “main” relations; 4. Apply the conditions in the cross product. SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; <statement> <select-statement> select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> <b-term> <column-name> <b-facor> S(a,b) b <b-primary> × <comp-pred> σ d = 10 c π T(c,d) S(a,b) <exp> <co-op> <exp> <term> > any <column-name> S.a Parse tree for SELECT c FROM T(c,d) WHERE d = 10 logic plan for SELECT c FROM T(c,d) WHERE d = 10
56
subqueries in the WHERE clause
subqueries in the WHERE clause 1. Convert the subquery into a logic plan; 2. Collect the attributes for the condition; 3. Make a cross product with the “main” relations; 4. Apply the conditions in the cross product. SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; <statement> <select-statement> b π select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> <b-term> σ S.a > U.c <column-name> <b-facor> S(a,b) b <b-primary> × <comp-pred> σ d = 10 c π T(c,d) S(a,b) <exp> <co-op> <exp> <term> > any <column-name> S.a Parse tree for SELECT c FROM T(c,d) WHERE d = 10 logic plan for SELECT c FROM T(c,d) WHERE d = 10
57
subqueries in the WHERE clause
subqueries in the WHERE clause 1. Convert the subquery into a logic plan; 2. Collect the attributes for the condition; 3. Make a cross product with the “main” relations; 4. Apply the conditions in the cross product. SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; <statement> <select-statement> b π select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> <b-term> σ S.a > U.c <column-name> <b-facor> S(a,b) b <b-primary> × <comp-pred> σ d = 10 c π T(c,d) S(a,b) <exp> <co-op> <exp> <term> > any <column-name> S.a Parse tree for SELECT c FROM T(c,d) WHERE d = 10
58
subqueries in the WHERE clause
subqueries in the WHERE clause 1. Convert the subquery into a logic plan; 2. Collect the attributes for the condition; 3. Make a cross product with the “main” relations; 4. Apply the conditions in the cross product. SELECT b FROM S(a,b) WHERE S.a > ANY (SELECT c FROM T(c,d) WHERE d = 10) ; <statement> <select-statement> b π select <select-list> from <tbl-list> where <search-condition> <select-sublist> <tbl-name> <b-term> σ S.a > U.c <column-name> <b-facor> S(a,b) b <b-primary> × <comp-pred> S(a,b) δ <exp> <co-op> <exp> σ d = 10 c π T(c,d) <term> > any <column-name> S.a Parse tree for SELECT c FROM T(c,d) WHERE d = 10
59
What Does DBMS Do? √ understand the program P
An input database program P understand the program P parse tree translate the program P into an expression E in relational algebra √ logic query plan convert E into an algorithm using algorithms in the collection C Machine executable code
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.