Download presentation

Presentation is loading. Please wait.

Published byAva Griffin Modified over 3 years ago

1
Query Processing Chapter 21 in Textbook

2
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

3
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.

4
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.

5
Example 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);

6
Example 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))

7
Example 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.

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

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

10
Phases of Query Processing 10

11
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.

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

13
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.

14
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.

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

16
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 > branchNo = 'B003')

17
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.

18
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.

19
Example Checking Semantic Correctness 19 SELECT p.propertyNo, p.street FROM Client c, Viewing v, PropertyForRent p WHERE c.clientNo = v.clientNo AND c.maxRent >= 500 AND c.prefType = Flat AND p.ownerNo = CO93;

20
Example 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).

21
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.

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

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

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

25
Transformation Rules for RA Operations 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 {A 1, A 2, …, A m } For example: fName, lName ( lName='Beech' (Staff)) = lName='Beech' ( fName,lName (Staff))

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

27
Transformation Rules for RA Operations 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 {A 1, A 2, …, A n }

28
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))

29
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))

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

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

32
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)))

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

34
Transformation Rules for RA Operations 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)

35
Transformation Rules for RA Operations 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)

36
Transformation Rules for RA Operations 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)

37
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)

38
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.street FROM Client c, Viewing v, PropertyForRent p WHERE c.prefType = Flat AND c.clientNo = v.clientNo AND v.propertyNo = p.propertyNo AND c.maxRent >= p.rent AND c.prefType = p.type AND p.ownerNo = CO93;

39
Example 21.3 Use of Transformation Rules 39

40
Example 21.3 Use of Transformation Rules 40

41
Example 21.3 Use of Transformation Rules 41

42
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

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

44
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.

45
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.

46
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.

47
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.

48
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.

49
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.

50
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.

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

52
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.

53
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.

54
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.

55
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.

56
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.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google