1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

14
1 Revision and Exam Briefing M. Akhtar Ali School of CEIS

Transcript of 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

Page 1: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

1

Revision and Exam Briefing

M. Akhtar Ali

School of CEIS

Page 2: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

2Advanced Databases – Revision and Exam Briefing

Agenda Query optimization Object-relational and object-oriented databases

Page 3: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

3Advanced Databases – Revision and Exam Briefing

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

Page 4: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

4Advanced Databases – Revision and Exam Briefing

Query optimization … The Relational Algebra Query Tree

viewDate = '10-JAN-2004' and city= 'Newcastle'and type = 'F' and maxRent > 595

π fName, lName, street, city, rooms, rent

propertyNo = propertyNo

PropertyForRent

σ

clientNo = clientNo

Client Viewing

Page 5: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

5Advanced Databases – Revision and Exam Briefing

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 pagesThere are 145,000 records;   Each record is of 70 bytes;

29 records occupy one page on disk; and log 2 5000 =

12

Page 6: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

6Advanced Databases – Revision and Exam Briefing

There are 5800 records in the Viewing table with viewDate = ‘10-JAN-2004’.

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: page-

oriented 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.

Query optimization: Information Assumed

Page 7: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

7Advanced Databases – Revision and Exam Briefing

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.

Query optimization: Questions and Answers

π fName, lName, street, city, rooms, rent

propertyNo = propertyNo

PropertyForRent

clientNo = clientNo

Client Viewing

σ maxRent > 595

σ city= 'Newcastle' andtype = 'F'

σviewDate = '10-JAN-2004'

viewDate = '10-JAN-2004' and city= 'Newcastle'and type = 'F' and maxRent > 595

π fName, lName, street, city, rooms, rent

propertyNo = propertyNo

PropertyForRent

σ

clientNo = clientNo

Client Viewing

Initial Query Tree Logically Optimized Query Tree

Page 8: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

8Advanced Databases – Revision and Exam Briefing

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

propertyNo = propertyNo

PropertyForRent

clientNo = clientNo

Client Viewing

σ maxRent > 595

σ city= 'Newcastle' andtype = 'F'

σviewDate = '10-JAN-2004'

(using block-nested loopto join T1 and T4)

(using block-nested loopto join T2 and T3 and

wrtie result to T4)

(using binary searchwrite result to T3)

(using B+ tree index onmaxRent write result to

T2)

(using Hash index on cityselect qualifying tuples in a

pipelining mode then chek thetype='F' condition and thenwrite selected tuples to T1 )

(on the fly)

Page 9: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

9Advanced Databases – Revision and Exam Briefing

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

Page 10: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

10Advanced Databases – Revision and Exam Briefing

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

Page 11: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

11Advanced Databases – Revision and Exam Briefing

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

Page 12: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

12Advanced Databases – Revision and Exam Briefing

Query optimization: Questions and Answers . . .

The cost of Join between T2 and T3 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 between T1 and T4 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

Page 13: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

13Advanced Databases – Revision and Exam Briefing

OO Databases Questions like:

Using ODL, provide an object-oriented representation of some classes/tables in the given UML class diagram / relational schema.

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.

Compare and contrast object-oriented representations with relational equivalent.

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.

Page 14: 1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.

14Advanced Databases – Revision and Exam Briefing

OR Databases

Questions like: Using Oracle 9i, provide an object-relational representation of

some classes in the given UML class diagram. Study past exam papers. Material covered on object-relational databases.

Compare and contrast object-relational representations with relational equivalent.

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.