0% found this document useful (0 votes)
2 views3 pages

Query processing - short form

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

UNIT – 4

QUERY PROCESSING:
i) Basic steps in query processing
1. Parsing and translation
2. Evaluation
3. Optimization

 Parsing and translation


o The parser of the query processor checks the syntax of the query, the user’s
privileges to execute the query, the table names and attribute names etc.
o Translator translate the query into its internal form. This is then translated into
relational algebra.
 Evaluation
o The query-execution engine takes a query-evaluation plan, executes that plan, and
returns the answers to the query.
 A sequence of operations that can be used to evaluate a query is a query evaluation plan.
 This following query can be translated into Query evaluation plan as
o select balancefrom accountwhere balance < 2500

 Query evaluation plan

 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

External sorting using Sort-merge


 Let M denote memory size (in pages).
1. Create sorted runs. Let i = 0 initially.
Repeatedly do the following till the end of the relation:
(a) Read M blocks of relation from memory
(b) Sort the in-memory blocks
(c) Write sorted data to run Ri; increment i.
Let the final value of i be N
2. Merge the runs (N-way merge). AssumeN<M.
o Sort and merge the blocks in each run
o Repeated passes are performed till all runs are sorted and have been merged into
one.
iv) JOIN OPERATION
 Join operation is used to join several tuples or blocks of two relation based on join
condition. (i.e) Atleast one of the attribute must be common in both relation.
 Several different algorithms to implement joins
o Nested-loop join
o Block nested-loop join
o Indexed nested-loop join
o Merge-join
o Hash-join
Nested-Loop Join: It joins each tuple of relation r with relation s if it satisfies the join condition
for each tuple tr in r do begin
for each tuple ts in s do begin
check if (tr , ts ) satisfy the join condition
if they do, add tr • ts to the result
end
end
 r is called the outerrelation and s the inner relation of the join.

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

You might also like