November 9, 2015. The RDBMS steps in executing SQL query: Checks query syntax Validates...

20
November 9 , 2015

Transcript of November 9, 2015. The RDBMS steps in executing SQL query: Checks query syntax Validates...

Page 1: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

November 9 , 2015

Page 2: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

The RDBMS steps in executing SQL query: Checks query syntax Validates query-checks data dictionary;

verifies objects referred to are database objects and requested operations are valid

Translates query into relational algebra (or relational calculus)

Rearranges relational algebra operations into most efficient form

Uses its knowledge of table size, indexes, order of tuples, distribution of values, to determine how the query will be processed-estimates the "cost" of alternatives and chooses the plan with the least estimated cost-considers the number of disk accesses, amount of memory, processing time, and communication costs, if any

Execution plan is then coded and executed

Page 3: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.
Page 4: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

SalesPersonsalesId lastName firstName address phoneS101 Smith Tom 123 Oak 333-4444S102 Chin Ann 456 Elm 555-1212S105 Lee Perry 789 Main 545-3333S109 Smith Betty 123 Oak 333-4444

CustomercustId lastName firstName emailC1001 Smith Bob [email protected] Doe John [email protected] Boger Tom [email protected]

InvoicesalesId custId description date amountS101 C1001 Blouse 10/15/2012 $45.89S101 C1007 End table 10/18/2012 $205.00S105 C1007 Miscellaneous 10/18/2012 $95.99S101 C1003 Blouse 10/21/2012 $55.99

Page 5: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Theoretical language with operators that apply to one or two relations to produce another relation

Both operands and results are tablesCan assign name to resulting table

(rename)SELECT, PROJECT, JOIN allow many

data retrieval operations

Page 6: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Applied to a single table, returns rows that meet a specified predicate, copying them to new table

Returns a horizontal subset of original table

SELECT tableName WHERE condition [GIVING newTableName]

Symbolically, [newTableName = ] predicate (table-name)

Predicate is called theta-condition, as in (table-name)

Result table is horizontal subset of operand Predicate can have operators <, <=, , =, =, <>,

(AND), (OR), (NOT)

Page 7: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Example:

SELECT SalesPerson WHERE lastName=‘Smith’ GIVING Answer

Symbolic version:

Answer = lastName=‘Smith’ (salesPerson)

Result: AnswersalesId lastName firstName address phoneS101 Smith Tom 123 Oak 333-4444S109 Smith Betty 123 Oak 333-4444

Page 8: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Example with more complex predicate:

SELECT Invoice WHERE salesId=‘S101’ AND date = ‘10/18/2012’

Symbolic version:

salesId=‘S101’ date=’10/18/2012’ (Invoice)

Result:salesId custId description date amountS101 C1007 End table 10/18/2012 $205.00

Page 9: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Operates on single tableReturns unique values in a column or

combination of columnsPROJECT tableName OVER (colName,...,colName) [GIVING

newTableName]

Symbolically [newTableName =] colName,...,colName (tableName)

Can compose SELECT and PROJECT, using result of first as argument for second

Page 10: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Example:

PROJECT Invoice OVER description GIVING Temp

Symbolically:

Temp = description (Invoice)

Result:Temp

descriptionBlouse

End tableMiscellaneous

Page 11: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Example of multiple column projection:

PROJECT Invoice OVER salesId, custId

Symbolically:

salesId, custId (Invoice)

Result:salesId custIdS101 C1001S101 C1007S105 C1007S101 C1003

Page 12: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Example:

SELECT Invoice WHERE salesId=‘S101’ GIVING TempPROJECT Temp OVER date, amount GIVING Result

Symbolically:

date, amount ( salesId=‘S101’ (Invoice))

Temp: Result:Temp

salesId custId description date amountS101 C1001 Blouse 10/15/2012 $45.89S101 C1007 End table 10/18/2012 $205.00S101 C1003 Blouse 10/21/2012 $55.99

Resultdate amount

10/15/2012 $45.8910/18/2012 $205.0010/21/2012 $55.99

Page 13: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Requires two tables with common column(s) (at least with same domain)

combines the matching rows-rows of the two tables that have the same values in the common column(s)

symbolized by |x| as in [newTableName = ] Table1 |x| Table2 , or Table1 JOIN Table2 [GIVING newTableName]

Page 14: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Example:

SalesPerson JOIN Invoice GIVING Result

Symbolically:

Result = SalesPerson |x| Invoice

Result: InvoicesalesId lastName firstName address phone custId description date amountS101 Smith Tom 123 Oak 333-4444 C1001 Blouse 10/15/2012 $45.89S101 Smith Tom 123 Oak 333-4444 C1007 End table 10/18/2012 $205.00S105 Lee Perry 789 Main 545-3333 C1007 Miscellaneous 10/18/2012 $95.99S101 Smith Tom 123 Oak 333-4444 C1003 Blouse 10/21/2012 $55.99

Page 15: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.
Page 16: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Graphical representation of the operations and operands in a relational algebra expression a leaf node is created for each relation (table) in

the expression For each unary or binary operation on a relation,

an upward branch is drawn from the node Reading upward from the leaves, as each

operation is included, an internal, non-leaf node is created to represent the result of the operation

Root node represents the entire expression See next slide for an example

Page 17: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Query: “Find the schedules and rooms of all courses taken by any Math major.”

Relational algebra:

Student JOIN Enroll GIVING Temp1Temp1 JOIN Class GIVING Temp2SELECT Temp2 WHERE

major=‘Math’ GIVING Temp3PROJECT Temp3 OVER schedule,

room

Page 18: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

An internal node can be executed when its operands are available

Node is replaced by the result of the operation it represents

Process continues until root node is reached

Root node is executed last, and is replaced by the result of the entire query

Page 19: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Same SQL statement can be translated to different relational algebra statements

Performing SELECT early reduces size of intermediate nodes-See next slide

Page 20: November 9, 2015.  The RDBMS steps in executing SQL query:  Checks query syntax  Validates query-checks data dictionary; verifies objects referred.

Relational algebra:

SELECT Student WHERE major=‘Math’

GIVING T1T1 JOIN Enroll GIVING T2T2 JOIN Class GIVING T3PROJECT T3 OVER schedule,

room