08.02.2014 Views

Query optimization in relational databases

Query optimization in relational databases

Query optimization in relational databases

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Query</strong> <strong>optimization</strong> <strong>in</strong> <strong>relational</strong> <strong>databases</strong><br />

Java C (speed-up factor 2-5)<br />

C Assembler (speed-up factor 2-5) , but<br />

SQL optimized SQL (speed-up by 1-100 and more) - Why?<br />

Chapter 2: Storage structures and <strong>in</strong>dices<br />

- database buffer<br />

- storage structures<br />

- <strong>in</strong>dices<br />

Chapter 3: <strong>Query</strong> <strong>optimization</strong><br />

- logical query <strong>optimization</strong><br />

- physical query <strong>optimization</strong><br />

- query execution plans and cost estimation<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 1


Selectivity of queries<br />

significant for size of <strong>in</strong>termediate result<br />

selectivity of the selection with condition B :<br />

selectivity ( S B (R) ) = | S B (R) | / | R |<br />

selectivity of the Jo<strong>in</strong> with condition B :<br />

selectivity ( R |X| B S ) = | R |X| B S | / ( | R | * | S | )<br />

estimated (e.g. based on samples, histograms)<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 2


Goal of logical query <strong>optimization</strong><br />

SQL queries<br />

select A1,...,An from R1,..., Rm where B<br />

correspond to algebra expression<br />

P(A1,...,An) S B ( R1 x ... x Rm )<br />

- very large <strong>in</strong>termediate results<br />

==> task:<br />

obta<strong>in</strong> the same result with<br />

smaller <strong>in</strong>termediate results<br />

e.g. move selection and projection <strong>in</strong>side expressions<br />

as far as possible<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 3


Logical query <strong>optimization</strong>– example<br />

select S.Vorname, S.Name<br />

from Student S, Hört H, Kurs K<br />

where K.titel = 'Datenbanken' and K.kursnr = H.kursnr and S.mnr=H.mnr ;<br />

200<br />

200<br />

500.000.000<br />

10.000<br />

P S.Vorname, S.Name<br />

|<br />

S K.titel = 'Datenbanken' and K.kursnr = H.kursnr and S.mnr=H.mnr<br />

|<br />

X 500.000.000.000<br />

/ \<br />

X K 1.000<br />

/ \<br />

S H assumptions:<br />

50.000<br />

10000 students, each tak<strong>in</strong>g 5 courses on avarage<br />

1000 courses, 2 of which are entitled ‘Datenbanken‘<br />

and taken by 100 students each<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 4


Logical query <strong>optimization</strong>– example<br />

A possible <strong>optimization</strong> :<br />

200 P S.Vorname, S.Name<br />

|<br />

200 |X|<br />

K.kursnr = H.kursnr<br />

/ \ 2<br />

50.000<br />

|X| S K.titel = 'Datenbanken'<br />

S.mnr=H.mnr \<br />

/ \ \<br />

S H K 1.000<br />

10.000<br />

50.000<br />

assumptions:<br />

10000 students, each tak<strong>in</strong>g 5 courses on avarage<br />

1000 courses, 2 of which are entitled ‘Datenbanken‘<br />

and taken by 100 students each<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 5


Logical query <strong>optimization</strong>– example<br />

A better <strong>optimization</strong> :<br />

200<br />

10.000<br />

50.000<br />

P S.Vorname, S.Name<br />

|<br />

|X| 200<br />

S.mnr=H.mnr<br />

/ \ 200<br />

S |X|<br />

K.kursnr = H.kursnr<br />

/ \<br />

H<br />

1.000<br />

2<br />

S K.titel = 'Datenbanken'<br />

|<br />

K<br />

assumptions:<br />

10000 students, each tak<strong>in</strong>g 5 courses on avarage<br />

1000 courses, 2 of which are entitled ‘Datenbanken‘<br />

and taken by 100 students each<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 6


Rules of logical query <strong>optimization</strong><br />

Union, <strong>in</strong>tersection, cartesian product and jo<strong>in</strong><br />

are commutative and associative .<br />

R1 U R2 = R2 U R1<br />

R1 ∩ R2 = R2 ∩ R1<br />

R1 X R2 = R2 X R1<br />

R1 |X| B R2 = R2 |X| B R1<br />

( R1 U R2 ) U R3 = R1 U ( R2 U R3 )<br />

( R1 ∩ R2 ) ∩ R3 = R1 ∩ ( R2 ∩ R3 )<br />

( R1 X R2 ) X R3 = R1 X ( R2 X R3 )<br />

( R1 |X| B R2 ) |X| B R3 = R1 |X| B ( R2 |X| B R3 )<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 7


Rules of logical query <strong>optimization</strong><br />

selections can be splitted and their order can be switched :<br />

S B1 and B2 (R) = S B1 (S B2 (R)) = S B2 (S B1 (R))<br />

push selections <strong>in</strong>side union, difference and <strong>in</strong>tersection:<br />

S B ( R1 U R2 ) = S B ( R1 ) U S B ( R2 )<br />

S B ( R1 - R2 ) = S B ( R1 ) - S B ( R2 )<br />

S B ( R1 ∩ R2 ) = S B ( R1 ) ∩ S B ( R2 )<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 8


Rules of logical query <strong>optimization</strong><br />

push selection <strong>in</strong>side a jo<strong>in</strong>, i.e. to a jo<strong>in</strong> argument,<br />

S B ( R1 |X| B2 R2 ) = S B ( R1 ) |X| B2 R2<br />

if B only uses attributes of R1<br />

push selection <strong>in</strong>side an argument of a cartesian product<br />

S B ( R1 X R2 ) = S B ( R1 ) X R2<br />

if B only uses attributes of R1<br />

if this is impossible for both R1 and R2,<br />

i.e., B uses attributes of R1 and of R2 :<br />

substitute selection applied to cartesian product with jo<strong>in</strong><br />

S B ( R1 X R2 ) = R1 |X| B R2<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 9


Rules of logical query <strong>optimization</strong><br />

order of projection and selection can be switched,<br />

if the projection yields all attributes needed for the selection condition :<br />

S B ( P A1,...,Am ( R1 ) ) = P A1,...,Am ( S B ( R1 ) )<br />

if B only uses attributes of A1,...,Am.<br />

push projection <strong>in</strong>side union<br />

P A1,...,Am ( R1 U R2 ) = P A1,...,Am ( R1 ) U P A1,...,Am ( R2 )<br />

push projection <strong>in</strong>to the jo<strong>in</strong>, i.e. apply it a jo<strong>in</strong> argument,<br />

if the jo<strong>in</strong> attributes are conta<strong>in</strong>ed <strong>in</strong> the projection<br />

P A1,...,Am<br />

( R1 |X| B R2 ) = P A1,...,Am<br />

( ( P A1,...,Am,AB1,...,ABn<br />

( R1 ) ) |X| B<br />

R2 )<br />

where AB1,...,ABn are the attributes of R1 needed to check the jo<strong>in</strong> condition B.<br />

projections can be comb<strong>in</strong>ed and <strong>in</strong>serted additionally<br />

P A1,...,Am ( R1 ) = P A1,...,Am ( P A1,...,Am,AB1,...,ABn ( R1 ) )<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 10


Logical query <strong>optimization</strong> - steps<br />

SQL query represented as a logical query tree<br />

apply the follow<strong>in</strong>g <strong>optimization</strong>s to this query tree<br />

• split and push down selections<br />

• comb<strong>in</strong>e selections and cartesian products to jo<strong>in</strong>s.<br />

• determ<strong>in</strong>e jo<strong>in</strong> sequence with smallest <strong>in</strong>termediate result.<br />

• where possible push down and <strong>in</strong>sert projections<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 11


F<strong>in</strong>d<strong>in</strong>g common sub-expressions<br />

SQL query represented as a logical query tree<br />

S1 = S2 ?<br />

op<br />

op<br />

S1 ⊆ S2 ?<br />

reuse !<br />

subtree<br />

S1<br />

subtree<br />

S2<br />

recompute<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 12


Physical query <strong>optimization</strong><br />

goal : m<strong>in</strong>imize number of pages loaded from disk<br />

because : 1 disc access costs about as much as 100.000 to 1000.000<br />

ma<strong>in</strong> storage operations<br />

translate query tree <strong>in</strong>to equivalent iterator tree :<br />

iterator P S.Vorname, S.Name<br />

|<br />

iterator |X|<br />

S.mnr=H.mnr<br />

/ \<br />

iterator-Rel(S)<br />

iterator |X|<br />

K.kursnr = H.kursnr<br />

1 iterator per algebra operator and<br />

1 iterator per relation<br />

/ \<br />

iterator-Rel(H) iterator S K.titel = 'Datenbanken'<br />

|<br />

iterator-Rel(K)<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 13


Iterator concept<br />

abstract data type iterator:<br />

Open( )<br />

Next( )<br />

Close( )<br />

Size( )<br />

Cost( )<br />

<strong>in</strong>itialize the iterator<br />

return / hand over next tuple<br />

close iterator, release ressources<br />

estimates the size of the result<br />

on the basis of the estimation for the <strong>in</strong>put variables<br />

estimates the costs of the result<br />

on the basis of the estimation for the <strong>in</strong>put variables<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 14


Classes of iterators<br />

We try to load R and S as rarely as possible<br />

for all operations op(R) and ( R op S ) :<br />

• 1-pass iterators: to load R (and S) only once is sufficient<br />

• nested-loop iterators: read R once and S several times<br />

• multi-pass iterators:<br />

read R (and often also S) several times<br />

• sort-based iterators: based on sort<strong>in</strong>g <strong>in</strong>put<br />

• <strong>in</strong>dex-based iterators: construct an <strong>in</strong>dex<br />

• hash-based iterators: construct a hash table<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 15


1-pass iterators for unary operators<br />

for operations op( R )<br />

with op ∈ { load, store, P, S, removeDup, group, sort, hash }<br />

we try to get along with one pass,<br />

i.e. to load the whole relation only once <strong>in</strong>to ma<strong>in</strong> memory<br />

for load, store, P, and S ,<br />

this always works<br />

for other operations only,<br />

if R fits <strong>in</strong>to ma<strong>in</strong> memory<br />

This is somewhat simplified<br />

because if necessary there<br />

still must be space for a<br />

search structure and space<br />

for collect<strong>in</strong>g the output.<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 16


Iterator for load<strong>in</strong>g data base relation<br />

implementation of operations<br />

Open( )<br />

Next( )<br />

Close( )<br />

Size( )<br />

Cost( )<br />

open relation, load first page <strong>in</strong>to ma<strong>in</strong> memory<br />

return / hand over next tuple<br />

close relation, release ma<strong>in</strong> memory ressources<br />

return size of relation<br />

return number of blocks,<br />

which have to be loaded from disk to ma<strong>in</strong> memory<br />

Same implementation for<br />

<strong>in</strong>termediate results displaced to disk<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 17


1-pass iterator for projection (P)<br />

implementation of the operations for P(R)<br />

concatenated with previous iterator (which produced R)<br />

Open( ) call R.Open( )<br />

result of the <strong>in</strong>put Iterator (R) is <strong>in</strong> ma<strong>in</strong> memory<br />

and will be used further<br />

Next( )<br />

return / hand over projection of result tuple of R.Next()<br />

Close( ) call R.Close( ).<br />

Size( )<br />

return R.Size( ) * space reduction factor of projection<br />

Cost( ) 0 ,<br />

because everyth<strong>in</strong>g is done <strong>in</strong> ma<strong>in</strong> memory<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 18


1-pass iterator for standard selection<br />

implementation of the selection operations S B (R)<br />

concatenated with previous iterator (that generates R)<br />

Open( ) call R.Open( )<br />

result of the <strong>in</strong>put iterator (R) is <strong>in</strong> ma<strong>in</strong> memory<br />

and will be used further<br />

Next( ) tuple = R.Next() ;<br />

while ( tuple≠null and not B(tuple) ) { tuple = R.Next(); }<br />

return tuple ;<br />

Close( ) call R.Close( )<br />

Size( )<br />

Cost( )<br />

return R.Size( ) * selectivity of S B<br />

0 for standard selection<br />

however R must be <strong>in</strong> ma<strong>in</strong> memory already<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 19


1-pass iterator for bag-union<br />

computation of R ∪bag S :<br />

1. transfer each tuple of R to Output<br />

R<br />

Output<br />

2. transfer each tuple of S to Output<br />

S<br />

Output<br />

cost: 0 , if R and S are already <strong>in</strong> ma<strong>in</strong> memory<br />

size: return R.size( ) + S.size( )<br />

space requirements : 2 pages, 1 for (R and S) and 1 for Output<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 20


1-pass iterators for b<strong>in</strong>ary operators<br />

for operations ( R op S )<br />

with op ∈ { ∪set, ∩set, -set, ∩bag, -bag, X, |X| }<br />

and S fits <strong>in</strong>to ma<strong>in</strong> memory<br />

construct search structure for S<br />

read R (and S) only once<br />

R<br />

Output<br />

search<br />

tree<br />

S<br />

which tuples shall be transferred<br />

from where to Output ?<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 21


1-pass iterators for b<strong>in</strong>ary operators<br />

for operations ( R op S )<br />

with op ∈ { ∪set, ∩set, -set, ∩bag, -bag, X, |X| }<br />

and S fits <strong>in</strong>to ma<strong>in</strong> memory<br />

construct a search structure for S<br />

read R (and S) only once<br />

R<br />

Output<br />

search<br />

tree<br />

S<br />

∩set : transfer tuple t from R to Output,<br />

if t is found <strong>in</strong> S .<br />

∪set : 1. transfer tupel t from R to Output,<br />

if t is not found <strong>in</strong> S .<br />

2. transfer tuples of S to Output<br />

<strong>in</strong> case of ∩bag, count number of identical tuples <strong>in</strong> S<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 22


Exercise: 1-pass iterators for<br />

b<strong>in</strong>ary operators R-S and S-R<br />

assumption: S fits <strong>in</strong>to ma<strong>in</strong> memory , but R does not fit<br />

How can we implement<br />

1. R – set S ,<br />

2. S – set R ,<br />

3. R – bag S and<br />

4. S – bag R<br />

as 1-pass iterators, i.e., <strong>in</strong> such a way that<br />

a search structur for S <strong>in</strong> ma<strong>in</strong> memory<br />

+ 1 <strong>in</strong>put page for R<br />

+ 1 output page for Output<br />

is sufficient?<br />

R<br />

Output<br />

search<br />

tree<br />

S<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 23


1-pass iterators for b<strong>in</strong>ary operators<br />

for operations ( R op S )<br />

with op ∈ { ∪set, ∩set, -set, ∪bag, ∩bag, -bag, X, |X| }<br />

and S fits <strong>in</strong>to ma<strong>in</strong> memory<br />

construct a search structure for S<br />

and read R (and S) only once<br />

search<br />

tree<br />

S<br />

R<br />

Output<br />

R- set S : transfer tuple t of R to Output<br />

if t is not found <strong>in</strong> S.<br />

S- set R : 1. delete tuple t of R from S.<br />

2. transfer rema<strong>in</strong><strong>in</strong>g tuples of S to Output<br />

<strong>in</strong> case of -bag, count number of identical tuples <strong>in</strong> tree S<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 24


Iterator for 1-pass "nested-loop jo<strong>in</strong>",<br />

if one relation fits <strong>in</strong>to ma<strong>in</strong> memory<br />

implementation of the jo<strong>in</strong> operation R |X| B S with a nested-loop jo<strong>in</strong><br />

if the smaller relation (say S) fits <strong>in</strong>to ma<strong>in</strong> memory , and<br />

one ma<strong>in</strong> memory page is left for load<strong>in</strong>g blocks of R , and<br />

one ma<strong>in</strong> memory page is left for collect<strong>in</strong>g the results <strong>in</strong> Output :<br />

load S <strong>in</strong>to ma<strong>in</strong> memory<br />

while R has blocks not yet loaded <strong>in</strong>to ma<strong>in</strong> memory<br />

{ load the next page Ri of R <strong>in</strong>to ma<strong>in</strong> memory<br />

}<br />

real implementation is more complex<br />

as Next() returns s<strong>in</strong>gle tuples<br />

for each tuple r <strong>in</strong> Ri<br />

for each tuple s <strong>in</strong> S<br />

if ( r |X| B s ) collect <strong>in</strong> output page(r,s)<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 25


Nested-loop iterators<br />

implementation of operations<br />

R op S by nested-loop iterators<br />

read one (usually the larger) relation R once<br />

and the other (usually the smaller) relation S multiple times,<br />

sometimes back and forth (<strong>in</strong> zig-zag mode)<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 26


Naive iterator for nested-loop jo<strong>in</strong><br />

if no relation fits <strong>in</strong>to ma<strong>in</strong> memory<br />

implementation of the operation<br />

the naive implementation<br />

R |X| B S<br />

reads R only once<br />

for each tuple r <strong>in</strong> R<br />

for each tuple s <strong>in</strong> S<br />

if ( r |X| B s ) collect <strong>in</strong> output page (r,s)<br />

real implementation is more complex<br />

as Next() returns s<strong>in</strong>gle tuples<br />

produces too many page faults<br />

because the whole relation S is read once per tuple of R<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 27


Iterator for nested-loop jo<strong>in</strong><br />

if no relation fits <strong>in</strong>to ma<strong>in</strong> memory<br />

implementation of the operation<br />

R |X| B S<br />

divide ma<strong>in</strong> memory <strong>in</strong>to k pages for S and m-k pages for R<br />

K = page sequence conta<strong>in</strong><strong>in</strong>g the next k pages of S<br />

M = page sequence conta<strong>in</strong><strong>in</strong>g the next m-k pages of R<br />

for each M <strong>in</strong> R<br />

reads R only once<br />

for each K <strong>in</strong> S<br />

for each tuple r <strong>in</strong> M<br />

for each tuple s <strong>in</strong> K<br />

if ( r |X| B s ) collect <strong>in</strong> output page(r,s)<br />

real implementation is more complex<br />

as Next() returns s<strong>in</strong>gle tuples<br />

produces fewer page faults<br />

because the whole relation S is read only once per sequence M of R<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 28


Zig-zag iterator for nested-loop jo<strong>in</strong><br />

if no relation fits <strong>in</strong>to ma<strong>in</strong> memory<br />

1.<br />

3.<br />

5.<br />

blocks of S<br />

2.<br />

4.<br />

for each page Ri <strong>in</strong> R<br />

while not all of S has been read<br />

read a sequence K of k pages of S <strong>in</strong> zig-zag mode<br />

for each tuple r <strong>in</strong> Ri<br />

for each tuple s <strong>in</strong> K<br />

if ( r |X| B s ) collect <strong>in</strong> output (r,s)<br />

k<br />

k pages of S<br />

produces even fewer page faults because<br />

pages "at both ends of S" are loaded less frequently<br />

Ri<br />

Output<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 29


Merge-sort<br />

Example: 3-way merge-sort with 4 ma<strong>in</strong> memory pages.<br />

for every sequence F of 4 blocks of R do<br />

{ load F <strong>in</strong>to the database buffer <strong>in</strong> ma<strong>in</strong> memory ;<br />

Quicksort ( database buffer ) ;<br />

write sorted database buffer back to disk ;<br />

}<br />

// disk conta<strong>in</strong>s sorted sequences of the length 4 blocks<br />

while the disk conta<strong>in</strong>s several sorted sequences<br />

{ merge (up to) 3 sorted sequences<br />

<strong>in</strong>to a s<strong>in</strong>gle longer sorted sequence<br />

}<br />

Ordered output sequence<br />

1 output page<br />

3 <strong>in</strong>put<br />

pages<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 30


Sort-based unary iterators<br />

1. sort R ( use e.g n-way merge-sort )<br />

2. perform <strong>in</strong>tended operation,<br />

e.g. duplicate elim<strong>in</strong>ation<br />

e.g. group<strong>in</strong>g<br />

(this is possible with a s<strong>in</strong>gle scan of the sorted relation)<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 31


Other sort-based iterators<br />

sort R or S or (R and S)<br />

sort-merge jo<strong>in</strong><br />

sort R and S accord<strong>in</strong>g to jo<strong>in</strong> attributes<br />

merge run:<br />

choose all pairs of tuples with identical jo<strong>in</strong> attribute values<br />

( white board )<br />

sort-based operations for ∪ set , ∩ , -<br />

sort R and S ,<br />

count number of identical tuples <strong>in</strong> merge run<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 32


Index-based iterators<br />

construct an <strong>in</strong>dex for R or for S or for both R and S<br />

<strong>in</strong> case of a primary <strong>in</strong>dex:<br />

1. sort R accord<strong>in</strong>g to <strong>in</strong>dex<br />

2. construct B + -tree<br />

<strong>in</strong> case of a secondary <strong>in</strong>dex:<br />

1. extract secondary key values from primary data<br />

2. collect pairs ( secondary key , reference to data block )<br />

<strong>in</strong> a B + -tree<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 33


Iterator for <strong>in</strong>dex-based selection<br />

implementation of operations for S B (R) , e.g. for B=(key>66)<br />

Open( )<br />

Next( )<br />

Close( )<br />

Size( )<br />

Cost( )<br />

step down the <strong>in</strong>dex (e.g. B + -tree) to the first page<br />

conta<strong>in</strong><strong>in</strong>g a record that fulfills the selection condition<br />

use <strong>in</strong>dex to access next tuple<br />

(may be on a different page)<br />

stop it when <strong>in</strong>dex key values become too high<br />

call R.Close( ) and close the <strong>in</strong>dex<br />

return R.Size( ) * selectivity of S B<br />

limited by<br />

number of result tuples<br />

+ number of <strong>in</strong>dex leaf nodes * selectivity of S B<br />

+ <strong>in</strong>dex depth<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 34


Two iterators for <strong>in</strong>dex jo<strong>in</strong><br />

1. given an <strong>in</strong>dex on R us<strong>in</strong>g the jo<strong>in</strong> attributes of R,<br />

we sequentially scan S<br />

for each tuple s of S<br />

use <strong>in</strong>dex of R to search jo<strong>in</strong> partner r of R<br />

if jo<strong>in</strong> (r,s) collect <strong>in</strong> output (r,s)<br />

2. given an <strong>in</strong>dex on R us<strong>in</strong>g the jo<strong>in</strong> attributes of R<br />

and an <strong>in</strong>dex on S us<strong>in</strong>g the jo<strong>in</strong> attributes of S :<br />

use the <strong>in</strong>dex on R and the <strong>in</strong>dex on S for a merge-run<br />

(as with<strong>in</strong> sort-merge jo<strong>in</strong>)<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 35


Hash-based iterators<br />

build hash-table for R or for S or both for R and S<br />

Goal:<br />

partition large relations us<strong>in</strong>g hash-functions,<br />

until certa<strong>in</strong> partitions fit <strong>in</strong>to ma<strong>in</strong> memory<br />

After partition<strong>in</strong>g:<br />

unary operators (e.g. duplicate elim<strong>in</strong>ation)<br />

are executed on ma<strong>in</strong> memory partitions<br />

b<strong>in</strong>ary operators are applied to partitions that correspond to each other<br />

thereby:<br />

use other iterators<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 36


Hash-jo<strong>in</strong> R |X| a=b S<br />

build (evtl. repeatedly) hash-table for (partitions of) R and S<br />

Split( R, S )<br />

{ choose new hash function h for R.a , and use h for S.b too<br />

use h to split R and S <strong>in</strong>to different buckets R1,…,Rn and S1,…,Sn<br />

}<br />

for each pair of correspond<strong>in</strong>g buckets (Ri,Si):<br />

if (at least one bucket (Ri or Si) fits <strong>in</strong>to ma<strong>in</strong> memory)<br />

nested-loop-jo<strong>in</strong>( Ri, Si ) ;<br />

else Split( Ri, Si )<br />

Example on whiteboard: Men |X| age=age Women<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 37


Hash-jo<strong>in</strong> R |X| a=b S<br />

Why do we partition repeatedly with (different) hash functions ?<br />

• partition<strong>in</strong>g everyth<strong>in</strong>g at once<br />

could require too many partitions<br />

(nearly empty buckets need to much space)<br />

• degree of partition<strong>in</strong>g (fan-out)<br />

is limited by number of available ma<strong>in</strong> memory pages<br />

that can be reserved for this operation<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 38


Summary: iterator classes<br />

for each operation (op R) and (R op S) try to load R and S<br />

as rarely as possible.<br />

• for s<strong>in</strong>gle-pass iterators,<br />

a s<strong>in</strong>gle load of R (and S) is sufficient<br />

• nested-loop iterators read R once and S multiple times<br />

• multi-pass iterators read R (and often also S) multiple<br />

times<br />

• sort-based iterators use sort<strong>in</strong>g<br />

• <strong>in</strong>dex-based iterators construct an <strong>in</strong>dex<br />

• hash-based iterators construct a hash-table<br />

Databases and Information Systems 1 - WS 2005 / 06 - Prof. Dr. Böttcher - <strong>Query</strong> <strong>optimization</strong> <strong>in</strong> RDBMS / 39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!