Revision and Exam Briefing 2009

Revision and Exam Briefing
M. Akhtar Ali
School of CEIS
1
Agenda


Query optimization
Object-relational and object-oriented databases
Advanced Databases – Revision and Exam Briefing
2
Query optimization

SQL Query:
SELECT C.fName, C.lName, P.street, P.city, P.rooms, P.rent
FROM PropertyForRent P, Client C, Viewing V
WHERE
P.propertyNo = V.propertyNo
AND
V.clientNo = C.clientNo
AND
V.viewDate = ‘10-JAN-2004’
AND P.city = ‘Newcastle’
AND P.type = ‘F’
AND C.maxRent > 595
Advanced Databases – Revision and Exam Briefing
3
Query optimization …

The Relational Algebra Query Tree
π
σ
fName, lName, street, city, rooms, rent
viewDate = '10-JAN-2004' and city= 'Newcastle'
and type = 'F' and maxRent > 595
propertyNo = propertyNo
PropertyForRent
Client
Advanced Databases – Revision and Exam Briefing
clientNo = clientNo
Viewing
4
Query optimization: Information Assumed
* The page size for the database is 2048 bytes.
* The size of the PropertyForRent table is 5000 pages
There are 90,000 records; Each record is of 112 bytes;
18 records occupy one page on disk; and log 2 5000 = 12
* The size the Client table is 4500 pages
There are 103,500 records; Each record is of 88 bytes;
23 records occupy one page on disk; and log 2 4500 = 12
* The size the Viewing table is 5000 pages
There are 145,000 records; Each record is of 70 bytes;
29 records occupy one page on disk; and log 2 5000 = 12
Advanced Databases – Revision and Exam Briefing
5
Query optimization: Information Assumed











There are 5800 records in the Viewing table with viewDate = ‘10-JAN2004’.
There are 31,500 records in the PropertyForRent table with type = ‘F’.
There are 1800 records in the PropertyForRent table with city =
‘Newcastle’, however, only 900 of these properties are flats (i.e. with type =
‘F’).
There are 20,700 records in the Client table with maxRent > 595.
The result of the SQL query contains 2700 records and the size of each
record is 146 bytes.
The number of buffer pages available during query processing is 5 i.e. B = 5.
Hash indexes (clustered) are available on city and type attributes of
PropertyForRent table.
B+ tree index (clustered) is available on maxRent attribute of Client table.
The Viewing table is sorted in ascending order of viewDate.
The join algorithms used by the DBMS for evaluating joins are: pageoriented nested and block-nested loops. Also note that you should consider
the cost of writing to temporary tables for intermediate results.
While calculating the evaluation cost of the query, ignore the cost of writing
out the final result.
Advanced Databases – Revision and Exam Briefing
6
Query optimization: Questions and Answers

Apply logical query optimisation (i.e. transformation/re-writing rules and
algebraic equivalence) to the relational algebra query tree and draw the
logically optimised query tree thus obtained.
Initial Query Tree
π
σ
Logically Optimized Query Tree
π
fName, lName, street, city, rooms, rent
fName, lName, street, city, rooms, rent
viewDate = '10-JAN-2004' and city= 'Newcastle'
and type = 'F' and maxRent > 595
propertyNo = propertyNo
propertyNo = propertyNo
PropertyForRent
Client
clientNo = clientNo
Viewing
Advanced Databases – Revision and Exam Briefing
σ
city= 'Newcastle' and
type = 'F'
PropertyForRent
σ maxRent > 595
Client
clientNo = clientNo
σ
viewDate = '10-JAN-2004'
Viewing
7
Query optimization: Questions and Answers . . .

Re-draw the query tree from part (a) and devise a physical plan for executing
the relational operations involved in the tree.
π
fName, lName, street, city, rooms, rent
(using block-nested loop
to join T1 and T4)
(using Hash index on city
select qualifying tuples in a
pipelining mode then chek the
type='F' condition and then
write selected tuples to T1 )
σ
(on the fly)
propertyNo = propertyNo
(using block-nested loop
to join T2 and T3 and
wrtie result to T4)
city= 'Newcastle' and
type = 'F'
clientNo = clientNo
(using binary search
write result to T3)
PropertyForRent
(using B+ tree index on
maxRent write result to
T2)
σ maxRent > 595
Client
Advanced Databases – Revision and Exam Briefing
σ
viewDate = '10-JAN-2004'
Viewing
8
Query optimization: Questions and Answers . . .

Compute the cost for evaluating the physical plan in terms
of I/O operations.

The cost of Selection over PropertyForRent (using Hash Index on
city attribute)









Let H = 1, is the constant cost associated with Hash index to find out
the RIDs (record identifiers) of the qualifying tuples.
The number of qualifying tuples = 1800
The number of data pages read = 1800 / 18 = 100
The cost of this selection = H + 100 = 101 I/Os
After reading each data page the condition type = ‘F’ is tested on
each tuple. All tuples for which this condition is true are written to T1.
The number of tuples in the result of 2nd round of selection = 900
The cost of writing to T1 = 900 / 18 = 50 I/Os
Subtotal cost = 101 + 50 = 151 I/Os
The size of temp T1 = 50 pages
Advanced Databases – Revision and Exam Briefing
9
Query optimization: Questions and Answers . . .

The cost of Selection over Client (using B+ tree index on
maxRent)






Let BT = 2, the height of B+ tree index.The number of qualifying
tuples = 20700
The number of pages read = 20700 / 23 = 900
Cost of selection = BT + 900 = 902 I/Os
The cost of writing out the result to temp T2 = 900 I/Os.
Subtotal cost = 902 + 900 = 1802 I/Os
The size of temp T2 = 900 pages
Advanced Databases – Revision and Exam Briefing
10
Query optimization: Questions and Answers . . .

The cost of Selection over Client (using B+ tree index on maxRent)







Let BT = 2, the height of B+ tree index.The number of qualifying tuples =
20700
The number of pages read = 20700 / 23 = 900
Cost of selection = BT + 900 = 902 I/Os
The cost of writing out the result to temp T2 = 900 I/Os.
Subtotal cost = 902 + 900 = 1802 I/Os
The size of temp T2 = 900 pages
The cost of Selection over Viewing (using Binary search because
the table is sorted on viewDate)








Let VM = 5000 (the number of pages, the size of Viewing table)
Let BS (be the average cost of using Binary Search) = log2 5000 = log
5000 / log 2 = 12.29 » 12
The number of qualifying tuples = 5800
The number of pages read = 5800 / 29 = 200
Cost of selection = BS + 200 = 212 I/Os
The cost of writing out the result to temp T3 = 200 I/Os.
Subtotal cost = 212 + 200 = 412 I/Os
The size of temp T3 = 200 pages
Advanced Databases – Revision and Exam Briefing
11
Query optimization: Questions and Answers . . .

The cost of Join between T2 and T3









The cost of Join between T1 and T4


Given B = 5 (buffer pages)
The Cost of Join = size of T3 + size of T2 * (size of T3 / B – 2)
= 200 + 900 * (200 / 3) = 60,200 I/Os.
The size of the join result is computed as follows:
The join will result in 5800 tuples, size of each tuple will be 70 +
88 = 158 bytes
Tuples per page = 2048 / 158 = 12,
Size in pages = 484
The cost of writing to T4 = 484 I/Os
Subtotal cost = 60200 + 484 = 60,684 I/Os.
The Cost of Join = size of T1 + size of T4 * (size of T1 / B – 2)
= 50 + 484 * (50 / 3) = 50 + 484 * 17 = 8278 I/Os.
The overall cost = 151 + 1802 + 412 + 60684 + 8278 =
71,327 I/Os
Advanced Databases – Revision and Exam Briefing
12
OO Databases

Questions like:

Using ODL, provide an object-oriented representation of
some classes/tables in the given UML class diagram /
relational schema.




Compare and contrast object-oriented representations with
relational equivalent.


Questions usually need a clear understanding of how to map /
implement classes, attributes, associations and inheritance from
UML onto the ODMG ODL or how to implement relations using
OO database design and ODL.
For this you should study past exam papers.
Lecture material and seminars for weeks 10-12.
This should be specific to the question, and classes etc you
have written in ODL and their equivalent relational counterpart
given in the scenario.
Answering a query in SQL and OQL and comparison and
contrasting.
Advanced Databases – Revision and Exam Briefing
13
OR Databases

Questions like:

Using Oracle 9i, provide an object-relational representation of
some classes in the given UML class diagram.



Compare and contrast object-relational representations with
relational equivalent.


Study past exam papers.
Material covered on object-relational databases.
This should be specific to the question, and classes etc you
have written in OR SQL and their equivalent relational
counterpart given in the scenario.
Answering a query OR SQL over the OR representations.
Advanced Databases – Revision and Exam Briefing
14