View
215
Download
0
Category
Preview:
Citation preview
October 31, 2012
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
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 bobsmith@gmail.comC1003 Doe John jdoe@tacosalad.lssu.eduC1007 Boger Tom tboger@lssu.edu
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
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
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)
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
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
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
Example:
PROJECT Invoice OVER description GIVING Temp
Symbolically:
Temp = description (Invoice)
Result:Temp
descriptionBlouse
End tableMiscellaneous
Example of multiple column projection:
PROJECT Invoice OVER salesId, custId
Symbolically:
salesId, custId (Invoice)
Result:salesId custIdS101 C1001S101 C1007S105 C1007S101 C1003
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
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]
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
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
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
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
Same SQL statement can be translated to different relational algebra statements
Performing SELECT early reduces size of intermediate nodes-See next slide
Relational algebra:
SELECT Student WHERE major=‘Math’
GIVING T1T1 JOIN Enroll GIVING T2T2 JOIN Class GIVING T3PROJECT T3 OVER schedule,
room
Recommended