Query processing - short form
Query processing - short form
Query processing - short form
QUERY PROCESSING:
i) Basic steps in query processing
1. Parsing and translation
2. Evaluation
3. Optimization
Query Optimization: Amongst all equivalent evaluation plans choose the one with lowest
cost.
Cost is generally measured as total elapsed time for answering query
1
ii) SORTING:
SQL queries can specify that output need to be sorted.
For relations that fit in memory, quick sort can be used. For relations that don’t fit in
memory, external sort-merge is a good choice to use.
a 19 a 19
g 24 d 31 a 14
b 14
a 19 g 24 a 19
c 33
d 31 b 14
b 14 d 31
c 33 c 33
c 33 e 16
b 14 d 7
e 16 g 24
e 16 d 21
r 16 d 21 d 31
a 14
d 21 m 3 e 16
d 7
m 3 r 16 g 24
d 21
p 2 m 3
m 3
d 7 a 14 p 2
p 2
a 14 d 7 r 16
r 16
p 2
initial sorted
relation runs runs output
create merge merge
runs pass–1 pass–2
2
Block nested-loop join:
Variant of nested-loop join in which every block of inner relation is paired with every block
of outer relation.
for each block Br of r do begin
for each block Bs of s do begin
for each tuple tr in Br do begin
for each tuple ts in Bs do begin
Check if ( tr , ts ) satisfy the join condition
if they do, add tr• ts to the result
end
end
end
end
Indexed Nested-Loop Join:
In this, nested loop join is performed by using an index.
For each tuple tr in the outer relation r, use the index to look up tuples in s that satisfy the
join condition with tuple tr
Merge-Join:
1. Sort both relations on their join attribute (if not already sorted on the join attributes).
2. Merge the sorted relations to join them
Join step is similar to the merge stage of the sort-merge algorithm. Main difference is
handling of duplicate values in join attribute — every pair with same value on join
attribute must be matched
Hash-Join:
Applicable for equi-joins and natural joins.
A hash function h is used to partition tuples of both relations r and s. Then, Join operation is
performed on the partitions of r and s based on join condition
Hash join