1 Query Optimization in Relational Database Systems
It is safer to accept any chance
CS5208: Query Optimization 1
It is safer to accept any chance that offers itself, and extemporize a procedure to fit it, than to get a good plan matured, and wait for a chance of using it. Thomas Hardy (1874) in Far from the Madding Crowd
Review: Case where index is useful
CS5208: Query Optimization 2
Query Optimization
- Since each relational op returns a relation, ops can be
composed!
- Queries that require multiple ops to be composed may
be composed in different ways - thus optimization is necessary for good performance e g A B C D can
CS5208: Query Optimization 3
necessary for good performance, e.g. A B C D can be evaluated as follows:
- (((A B) C) D)
- ((A B) (C D))
- ((B A) (D C))
- …
Query Optimization
- Each strategy can be represented as a query
evaluation plan (QEP) - Tree of R.A. ops, with choice
- f algo for each op.
D NL SM HJ
CS5208: Query Optimization 4
- Goal of optimization: To find the “best” plan that
compute the same answer (to avoid “bad” plans)
A B C D A B C D NL NL HJ INL
More on Motivating Examples
Sailors (sid: integer, sname: string, rating: integer, age: real) Reserves (sid: integer, bid: integer, day: dates, rname: string)
CS5208: Query Optimization 5
- Reserves:
- Each tuple is 40 bytes long, 100 tuples per page, 1000 pages.
- Sailors:
- Each tuple is 50 bytes long, 80 tuples per page, 500 pages.
Example
SELECT S.sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid=100 AND S.rating>5
sname sname
sname ti > 5
CS5208: Query Optimization
Sailors Reserves
sid=sid bid=100 rating > 5
Reserves Sailors
sid=sid bid=100 rating > 5
Reserves Sailors sid=sid bid=100 rating > 5