Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query...
-
Upload
ava-griffin -
Category
Documents
-
view
228 -
download
1
Transcript of Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query...
![Page 1: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/1.jpg)
Query Processing
Chapter 21 in Textbook
![Page 2: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/2.jpg)
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
![Page 3: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/3.jpg)
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.
![Page 4: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/4.jpg)
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.
![Page 5: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/5.jpg)
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’);
![Page 6: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/6.jpg)
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))
![Page 7: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/7.jpg)
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.
![Page 8: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/8.jpg)
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.
![Page 9: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/9.jpg)
Phases of Query Processing
9
QP has four main phases:
decomposition (consisting of parsing and validation);
optimization; code generation; execution.
![Page 10: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/10.jpg)
Phases of Query Processing
10
![Page 11: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/11.jpg)
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.
![Page 12: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/12.jpg)
1.a. Analysis
12
Analyze query lexically and syntactically using compiler techniques.
Verify relations and attributes exist. Verify operations are appropriate for
object type.
![Page 13: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/13.jpg)
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.
![Page 14: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/14.jpg)
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.
![Page 15: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/15.jpg)
Example 21.1 – Relational Algebra Tree (R.A.T.)
15
![Page 16: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/16.jpg)
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')
![Page 17: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/17.jpg)
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.
![Page 18: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/18.jpg)
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.
![Page 19: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/19.jpg)
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’;
![Page 20: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/20.jpg)
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) .
![Page 21: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/21.jpg)
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.
![Page 22: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/22.jpg)
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))
![Page 23: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/23.jpg)
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))
![Page 24: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/24.jpg)
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)
![Page 25: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/25.jpg)
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))
![Page 26: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/26.jpg)
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
![Page 27: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/27.jpg)
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}
![Page 28: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/28.jpg)
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))
![Page 29: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/29.jpg)
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))
![Page 30: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/30.jpg)
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))
![Page 31: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/31.jpg)
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)))
![Page 32: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/32.jpg)
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)))
![Page 33: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/33.jpg)
Transformation Rules for RA Operations
33
8. Commutativity of Union and Intersection (but not set difference).
R S = S R
R S = S R
![Page 34: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/34.jpg)
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)
![Page 35: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/35.jpg)
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)
![Page 36: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/36.jpg)
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)
![Page 37: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/37.jpg)
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)
![Page 38: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/38.jpg)
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’;
![Page 39: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/39.jpg)
Example 21.3 Use of Transformation Rules
39
![Page 40: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/40.jpg)
Example 21.3 Use of Transformation Rules
40
![Page 41: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/41.jpg)
Example 21.3 Use of Transformation Rules
41
![Page 42: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/42.jpg)
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
![Page 43: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/43.jpg)
2. Query Optimization 2 kinds:
a. Using heuristics on deciding the best plan.b. Comparing costs of different plans.
43
![Page 44: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/44.jpg)
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.
![Page 45: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/45.jpg)
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.
![Page 46: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/46.jpg)
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.
![Page 47: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/47.jpg)
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.
![Page 48: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/48.jpg)
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.
![Page 49: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/49.jpg)
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.
![Page 50: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/50.jpg)
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.
![Page 51: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/51.jpg)
Join Operation
51
Main strategies for implementing join:
Block Nested Loop Join. Indexed Nested Loop Join. Sort-Merge Join. Hash Join.
![Page 52: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/52.jpg)
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.
![Page 53: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/53.jpg)
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.
![Page 54: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/54.jpg)
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.
![Page 55: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/55.jpg)
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.
![Page 56: Query Processing Chapter 21 in Textbook. Overview What is Query Processing? What is Query Optimization? Example. Phases of Query Processing. 1. Decomposition.](https://reader033.fdocuments.in/reader033/viewer/2022061304/5513c2815503463a298b4a3a/html5/thumbnails/56.jpg)
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.