query processing chapter 21 in textbook. overview what is query processing? what is query...
Embed Size (px)
TRANSCRIPT

Query Processing
Chapter 21 in Textbook

Overview What is Query Processing? What is Query Optimization?
Example. Phases of Query Processing.
1. Decomposition.a. Analysis.b. Normalization.c. Semantic Analysis.d. Simplification.e. Restructuring.
2. Optimization.a. Heuristics.b. Comparing costs.
3. Code Generation.4. Execution.2

Query Processing
3
Activities involved in retrieving data from the database.
Aims of QP: transform query from high-level language
(SQL) into correct and efficient execution strategy in low-level language (Relational Algebra - RA);
execute strategy to retrieve required data.
Advantage of Declarative Languages (SQL): System performs optimization NOT user.

Query Optimization
4
Activity of choosing an efficient execution strategy for processing query.
As there are many equivalent transformations of same high-level query, choose one that minimizes resource usage.
Generally, reduce total execution time of query.
Disk access tends to be dominant cost in
query processing for centralized DBMS.

Example 21.1 - Different Strategies
5
Find all Managers who work at a London branch.
SELECT *
FROM Staff s, Branch b
WHERE s.branchNo = b.branchNo AND
(s.position = ‘Manager’ AND b.city = ‘London’);

Example 21.1 - Different Strategies
6
Three equivalent RA queries are:(1) (position='Manager') (city='London')
(Staff.branchNo=Branch.branchNo) (Staff X Branch)
(2) (position='Manager') (city='London')(Staff Staff.branchNo=Branch.branchNo Branch)
(3) (position='Manager'(Staff)) Staff.branchNo=Branch.branchNo
(city='London' (Branch))

Example 21.1 - Different Strategies
7
Assume: 1000 tuples in Staff; 50 tuples in Branch; 50 Managers; 5 London branches; no indexes or sort keys; results of any intermediate operations
stored on disk; cost of the final write is ignored; tuples are accessed one at a time.

Example 21.1 - Cost Comparison
8
Cost (in disk accesses) are:
(1) (1000 + 50) + 2*(1000 * 50) = 101 050
(2) 2*1000 + (1000 + 50) = 3 050 (3) 1000 + 2*50 + 5 + (50 + 5) = 1 160
Cartesian product and join operations much more expensive than selection, and third option significantly reduces size of relations being joined together.

Phases of Query Processing
9
QP has four main phases:
decomposition (consisting of parsing and validation);
optimization; code generation; execution.

Phases of Query Processing
10

1. Query Decomposition
11
Aims are: transform high-level query into RA query. check that query is syntactically and
semantically correct.
Typical stages are:a.analysis, b.normalization, c. semantic analysis, d.simplification, e.query restructuring.

1.a. Analysis
12
Analyze query lexically and syntactically using compiler techniques.
Verify relations and attributes exist. Verify operations are appropriate for
object type.

1.a. Analysis - Example
13
SELECT staff_no
FROM Staff
WHERE position > 10;
This query would be rejected on two grounds: staff_no is not defined for Staff relation
(should be staffNo). Comparison ‘>10’ is incompatible with type
position, which is variable character string.

1.a. Analysis
14
Finally, query transformed into a query tree constructed as follows: Leaf node for each base relation. Non-leaf node for each intermediate
relation produced by RA operation. Root of tree represents query result. Sequence is directed from leaves to root.

Example 21.1 – Relational Algebra Tree (R.A.T.)
15

1.b. Normalization
16
Converts query into a normalized form for easier manipulation.
Predicate can be converted into one of two forms:
Conjunctive normal form:(position = 'Manager' salary > 20000)
(branchNo = 'B003')
Disjunctive normal form:(position = 'Manager' branchNo = 'B003' )
(salary > 20000 branchNo = 'B003')

1.c. Semantic Analysis
17
Rejects normalized queries that are incorrectly formulated or contradictory.
Query is incorrectly formulated if components do not contribute to generation of result.
Query is contradictory if its predicate cannot be satisfied by any tuple.
Algorithms to determine correctness exist only for queries that do not contain disjunction and negation.

1.c. Semantic Analysis
18
For these queries (no disjunction and no negation) could construct:1. A relation connection graph. 2. Normalized attribute connection graph.
1. Relation connection graph a. Create node for each relation and node for result.b. Create edges between two nodes that represent a
join.c. Create edges between nodes that represent
projection. If not connected, query is incorrectly
formulated.

Example 21.2 - Checking Semantic Correctness
19
SELECT p.propertyNo, p.streetFROM Client c, Viewing v, PropertyForRent
pWHERE c.clientNo = v.clientNo AND
c.maxRent >= 500 AND
c.prefType = ‘Flat’ AND p.ownerNo = ‘CO93’;

Example 21.2 - Checking Semantic Correctness
20
Relation Connection graph
Relation connection graph not fully connected, so query is not correctly formulated.
Have omitted the join condition (v.propertyNo = p.propertyNo) .

1.d. Simplification
21
Aims:1. Detects redundant qualifications, 2. eliminates common sub-expressions, 3. transforms query to semantically
equivalent but more easily and efficiently computed form.
Apply well-known transformation rules of Boolean algebra.

Transformation Rules for RA Operations
22
1. Conjunctive Selection operations can cascade into individual Selection operations (and vice versa).
pqr(R) = p(q(r(R)))
Sometimes referred to as cascade of Selection. branchNo='B003' salary>15000(Staff) =
branchNo='B003'(salary>15000(Staff))

Transformation Rules for RA Operations
23
2. Commutativity of Selection.
p(q(R)) = q(p(R))
For example:
branchNo='B003'(salary>15000(Staff)) = salary>15000(branchNo='B003'(Staff))

Transformation Rules for RA Operations
24
3. In a sequence of Projection operations, only the last in the sequence is required.
LM … N(R) = L (R)
For example:
lNamebranchNo, lName(Staff) = lName (Staff)

Transformation Rules for RA Operations
25
4. Commutativity of Selection and Projection.
If predicate p involves only attributes in projection list, Selection and Projection operations commute:
Ai, …, Am(p(R)) = p(Ai, …, Am(R)) where p {A1, A2, …, Am}
For example:fName, lName(lName='Beech'(Staff)) =
lName='Beech'(fName,lName(Staff))

Transformation Rules for RA Operations
26
5. Commutativity of Theta join (and Cartesian product).
R p S = S p R
R X S = S X R
Rule also applies to Equijoin and Natural join. For example:
Staff staff.branchNo=branch.branchNo Branch =
Branch staff.branchNo=branch.branchNo Staff

Transformation Rules for RA Operations
27
6. Commutativity of Selection and Theta join (or Cartesian product).
If selection predicate involves only attributes of one of join relations, Selection and Join (or Cartesian product) operations commute:
p(R r S) = (p(R)) r S
p(R X S) = (p(R)) X S
where p {A1, A2, …, An}

Transformation Rules for RA Operations
28
If selection predicate is conjunctive predicate having form (p q), where p only involves attributes of R, and q only attributes of S, Selection and Theta join operations commute as:
p q(R r S) = (p(R)) r (q(S))
p q(R X S) = (p(R)) X (q(S))

Transformation Rules for RA Operations
29
For example:
position='Manager' city='London'(Staff
Staff.branchNo=Branch.branchNo Branch) = (position='Manager'(Staff)) Staff.branchNo=Branch.branchNo
(city='London' (Branch))

Transformation Rules for RA Operations
30
7. Commutativity of Projection and Theta join (or Cartesian product).
If projection list is of form L = L1 L2, where L1 only has attributes of R, and L2 only has attributes of S, provided join condition only contains attributes of L, Projection and Theta join commute:
L1L2(R r S) = (L1(R)) r (L2(S))

Transformation Rules for RA Operations
31
If join condition contains additional attributes not in L (M = M1 M2 where M1 only has attributes of R, and M2 only has attributes of S), a final projection operation is required:
L1L2(R r S) = L1L2( (L1M1(R)) r (L2M2(S)))

Transformation Rules for RA Operations
32
For example:
position,city,branchNo(Staff Staff.branchNo=Branch.branchNo Branch) =
(position, branchNo(Staff)) Staff.branchNo=Branch.branchNo (
city, branchNo (Branch))
and using the latter rule:
position, city(Staff Staff.branchNo=Branch.branchNo Branch) =
position, city ((position, branchNo(Staff))
Staff.branchNo=Branch.branchNo ( city, branchNo (Branch)))

Transformation Rules for RA Operations
33
8. Commutativity of Union and Intersection (but not set difference).
R S = S R
R S = S R

Transformation Rules for RA Operations
34
9. Commutativity of Selection and set operations (Union, Intersection, and Set difference).
p(R S) = p(S) p(R)
p(R S) = p(S) p(R)
p(R - S) = p(S) - p(R)

Transformation Rules for RA Operations
35
10.Commutativity of Projection and Union.
L(R S) = L(S) L(R)
11. Associativity of Union and Intersection (but not Set difference).
(R S) T = S (R T)
(R S) T = S (R T)

Transformation Rules for RA Operations
36
12. Associativity of Theta join (and Cartesian product).
Cartesian product and Natural join are always associative:
(R S) T = R (S T)
(R X S) X T = R X (S X T)
If join condition q involves attributes only from S and T, then Theta join is associative:
(R p S) q r T = R p r (S q T)

Transformation Rules for RA Operations
37
For example:
(Staff Staff.staffNo=PropertyForRent.staffNo
PropertyForRent) ownerNo=Owner.ownerNo staff.lName=Owner.lName Owner =
Staff staff.staffNo=PropertyForRent.staffNo staff.lName=lName
(PropertyForRent ownerNo Owner)

Example 21.3 Use of Transformation Rules
38
For prospective renters of flats, find properties that match requirements and owned by CO93.
SELECT p.propertyNo, p.streetFROM Client c, Viewing v, PropertyForRent pWHERE c.prefType = ‘Flat’ AND
c.clientNo = v.clientNo AND v.propertyNo = p.propertyNo ANDc.maxRent >= p.rent AND c.prefType = p.type AND p.ownerNo = ‘CO93’;

Example 21.3 Use of Transformation Rules
39

Example 21.3 Use of Transformation Rules
40

Example 21.3 Use of Transformation Rules
41

Overview Phases of Query Processing.
1. Decomposition.a. Analysis.b. Normalization.c. Semantic Analysis.d. Simplification.e. Restructuring.
2. Optimization.a. Heuristics.b. Comparing costs.
3. Code Generation.4. Execution.
42

2. Query Optimization 2 kinds:
a. Using heuristics on deciding the best plan.b. Comparing costs of different plans.
43

2.a. Heuristical Processing Strategies
44
Perform Selection operations as early as possible. Keep predicates on same relation together.
Combine Cartesian product with subsequent Selection whose predicate represents join condition into a Join operation.
Use associativity of binary operations to rearrange leaf nodes so leaf nodes with most restrictive Selection operations executed first.

Heuristical Processing Strategies
45
Perform Projection as early as possible. Keep projection attributes on same relation
together.
Compute common expressions once. If common expression appears more than
once, and result not too large, store result and reuse it when required.
Useful when querying views, as same expression is used to construct view each time.

2.b. Cost Estimation for RA Operations
46
Many different ways of implementing RA operations.
Aim of QO is to choose most efficient one. Use formulae that estimate costs for a
number of options, and select one with lowest cost.
Consider only cost of disk access, which is usually dominant cost in QP.
Many estimates are based on cardinality of the relation, so need to be able to estimate this.

Database Statistics
47
Success of estimation depends on amount and currency of statistical information DBMS holds.
Keeping statistics current can be problematic.
If statistics updated every time tuple is changed, this would impact performance.
DBMS could update statistics on a periodic basis, for example nightly, or whenever the system is idle.

Selection Operation
48
Predicate may be simple or composite. Number of different implementations,
depending on file structure, and whether attribute(s) involved are indexed/hashed.
Main strategies are: Linear Search (Unordered file, no index). Binary Search (Ordered file, no index). Equality on hash key. Equality condition on primary key.

Composite Predicates - Conjunction without Disjunction
49
May consider following approaches:- If one attribute has index or is ordered, can use one of above selection strategies. Can then check each retrieved record.- For equality on two or more attributes, with composite index (or hash key) on combined attributes, can search index directly.- With secondary indexes on one or more attributes (involved only in equality conditions in predicate), could use record pointers if exist.

Composite Predicates - Selections with Disjunction
50
If one term contains an (OR), and term requires linear search, entire selection requires linear search.
Only if index or sort order exists on every term can selection be optimized by retrieving records that satisfy each condition and applying union operator.
Again, record pointers can be used if they exist.

Join Operation
51
Main strategies for implementing join:
Block Nested Loop Join. Indexed Nested Loop Join. Sort-Merge Join. Hash Join.

Block Nested Loop Join
52
Simplest join algorithm is nested loop that joins two relations together a tuple at a time.
Outer loop iterates over each tuple in R, and inner loop iterates over each tuple in S.
As basic unit of reading/writing is a disk block, better to have two extra loops that process blocks.

Indexed Nested Loop Join
53
If have index (or hash function) on join attributes of inner relation, can use index lookup.
For each tuple in R, use index to retrieve matching tuples of S.

Sort-Merge Join
54
For Equijoins, most efficient join is when both relations are sorted on join attributes.
Can look for qualifying tuples merging relations.
May need to sort relations first. Now tuples with same join value are in
order. If assume join is *:* and each set of tuples
with same join value can be held in database buffer at same time, then each block of each relation need only be read once.

Hash Join
55
For Natural or Equijoin, hash join may be used.
Idea is to partition relations according to some hash function that provides uniformity and randomness.
Each equivalent partition should hold same value for join attributes, although it may hold more than one value.

Projection Operation
56
To implement projection need to: remove attributes that are not required; eliminate any duplicate tuples produced
from previous step. Only required if
projection attributes do not include a key. Two main approaches to eliminating
duplicates: sorting; hashing.