0% found this document useful (0 votes)
31 views21 pages

ADBS Chapter 02 Updated

Uploaded by

wachemounversity
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views21 pages

ADBS Chapter 02 Updated

Uploaded by

wachemounversity
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Advanced Database Systems [InSy3071]

Chapter 2
Query Processing and Optimization
Introduction Strategies for Query Processing
 DBMS techniques to process a query
 Scanner identifies query tokens

 Parser checks the query syntax

 Validation checks all attribute and relation names

 Query tree (or query graph) created

 Execution strategy or query plan devised

 Query optimization
 Planning a good execution strategy

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Query Processing
Figure 18.1 Typical steps when processing a high-level query

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


18.1 Translating SQL Queries into
Relational Algebra and Other Operators (1
of 3)

 SQ L
 Query language used in most RDBMSs

 Query decomposed into query blocks


 Basic units that can be translated into the algebraic

operators
 Contains single SELECT-FROM-WHERE expression

 May contain GROUP BY and HAVING clauses

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


18.1 Translating SQL Queries into Relational
Algebra and Other Operators (2 of 3)

• Example:

– Inner block

– Outer block

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


18.1 Translating SQL Queries into
Relational Algebra and Other Operators (3
of 3)

 Inner block translated into:

 Outer block translated into:

 Query optimizer chooses execution plan for each query


block

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Additional Operators Semi-Join and Anti-
Join (1 of 2)
• Semi-join
– Generally used for unnesting EXISTS, IN, and ANY
subqueries
– Syntax: T1.X S = T2.Y
▪ T1 is the left table and T2 is the right table of the
semi-join
– A row of T1 is returned as soon as T1.X finds a match
with any value of T2.Y without searching for
further matches

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Additional Operators Semi-Join and Anti-
Join (2 of 2)
• Anti-join
– Used for unnesting NOT EXISTS, NOT IN, and ALL
subqueries
– Syntax: T1.x A = T2.y
 T1 is the left table and T2 is the right table of the

anti-join
 A row of T1 is rejected as soon as
T1.x finds a match
with any value of T2.y
 A row of T1 is returned only if
T1.x does not match
with any value of T2.y

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Query Optimization

Copyright © 2016, 2011, 2007 Pearson Education, Inc. All Rights Reserved
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Introduction
 Query optimization
 Conducted by a query optimizer in a D BMS

 Goal: select best available strategy for executing query

 Based on information available

 Most RDBMSs use a tree as the internal representation of a


query

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


19.1 Query Trees and Heuristics for
Query Optimization (1 of 3)
 Step 1: scanner and parser generate initial query representation
 Step 2: representation is optimized according to heuristic rules
 Step 3: query execution plan is developed
 Execute groups of operations based on access paths

available and files involved

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


19.1 Query Trees and Heuristics for Query
Optimization (2 of 3)
• Example heuristic rule
– Apply SELECT and PROJECT before JOIN
▪ Reduces size of files to be joined
• Query tree
– Represents relational algebra expression
• Query graph
– Represents relational calculus expression
• Example for Q2 on next slide

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Query Trees and Query Graph
Corresponding to Q2
Figure 19.1 Two query trees
for the query Q2. (a) Query
tree corresponding to the
relational algebra expression
for Q2. (b) Initial (canonical)
query tree for SQL query Q2.
(c) Query graph for Q2.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


19.1 Query Trees and Heuristics for
Query Optimization (3 of 3)
 Query tree represents a specific order of operations for
executing a query
 Preferred to query graph for this reason

 Query graph
 Relation nodes displayed as single circles

 Constants represented by constant nodes

 Double circles or ovals

 Selection or join conditions represented as edges

 Attributes to be retrieved displayed in square brackets

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Heuristic Optimization of Query Trees
 Many different query trees can be used to represent the query
and get the same results
 Figure 19.1b shows initial tree for Q2
 Very inefficient - will never be executed

 Optimizer will transform into equivalent final query tree

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Query Transformation Example (1 of 5)
Figure 19.2 Steps in converting a query tree during heuristic optimization.
(a) Initial (canonical) query tree for S QL query Q.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Query Transformation Example (2 of 5)
Figure 19.2 Steps in converting a query tree during heuristic
optimization.
(b) Moving SELECT operations down the query tree.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Query Transformation Example (3 of 5)
Figure 19.2 Steps in converting a query tree during heuristic
optimization.
(c) Applying the more restrictive SELECT operation first.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Query Transformation Example (4 of 5)
Figure 19.2 Steps in converting a query tree during heuristic
optimization.
(d) Replacing CARTESIAN PRODUCT and SELECT with JOIN
operations.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Query Transformation Example (5 of 5)
Figure 19.2 Steps in converting a query tree during heuristic
optimization.
(e) Moving PROJECT operations down the query tree.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


End of Chapter 2

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy