Query Processing and Optimization
Query Processing and Optimization
Database System Concepts - 7th Edition 15.2 ©Silberschatz, Korth and Sudarshan
Basic Steps in Query Processing (Cont.)
Database System Concepts - 7th Edition 15.3 ©Silberschatz, Korth and Sudarshan
Basic Steps in Query Processing:
Optimization
A relational algebra expression may have many equivalent expressions
• E.g., salary75000(salary(instructor)) is equivalent to
salary(salary75000(instructor))
Each relational algebra operation can be evaluated using one of several
different algorithms
• Correspondingly, a relational-algebra expression can be evaluated in
many ways.
Annotated expression specifying detailed evaluation strategy is called an
evaluation-plan. E.g.,:
• Use an index on salary to find instructors with salary < 75000,
• Or perform complete relation scan and discard instructors with salary
75000
Database System Concepts - 7th Edition 15.4 ©Silberschatz, Korth and Sudarshan
Basic Steps: Optimization (Cont.)
Database System Concepts - 7th Edition 15.5 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost
Database System Concepts - 7th Edition 15.6 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost
Database System Concepts - 7th Edition 15.7 ©Silberschatz, Korth and Sudarshan
Evaluation of Expressions
Database System Concepts - 7th Edition 15.8 ©Silberschatz, Korth and Sudarshan
Materialization
Database System Concepts - 7th Edition 15.9 ©Silberschatz, Korth and Sudarshan
Pipelining
Database System Concepts - 7th Edition 15.10 ©Silberschatz, Korth and Sudarshan
Query Optimization
Database System Concepts - 7th Edition 15.12 ©Silberschatz, Korth and Sudarshan
Introduction (Cont.)
Database System Concepts - 7th Edition 15.13 ©Silberschatz, Korth and Sudarshan
Generating Equivalent Expressions
Database System Concepts - 7th Edition 15.14 ©Silberschatz, Korth and Sudarshan
Transformation of Relational Expressions
Database System Concepts - 7th Edition 15.15 ©Silberschatz, Korth and Sudarshan
Equivalence Rules
Database System Concepts - 7th Edition 15.16 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
E1 ⨝ E2 ≡ E2 ⨝ E1
Database System Concepts - 7th Edition 15.17 ©Silberschatz, Korth and Sudarshan
Pictorial Depiction of Equivalence Rules
Database System Concepts - 7th Edition 15.18 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
7. The selection operation distributes over the theta join operation under the
following two conditions:
(a) When all the attributes in 0 involve only the attributes of one
of the expressions (E1) being joined.
Database System Concepts - 7th Edition 15.19 ©Silberschatz, Korth and Sudarshan
Transformation Example: Pushing Selections
Query: Find the names of all instructors in the Music department, along
with the titles of the courses that they teach
• name, title(dept_name= ‘Music’
(instructor ⨝ (teaches ⨝ course_id, title (course))))
Transformation using rule 7a.
• name, title((dept_name= ‘Music’(instructor)) ⨝
(teaches ⨝ course_id, title (course)))
Performing the selection as early as possible reduces the size of the
relation to be joined.
Database System Concepts - 7th Edition 15.20 ©Silberschatz, Korth and Sudarshan
Join Ordering Example
(r1 ⨝ r2) ⨝ r3
so that we compute and store a smaller temporary relation.
Database System Concepts - 7th Edition 15.23 ©Silberschatz, Korth and Sudarshan
Join Ordering Example (Cont.)
Consider the expression
name, title(dept_name= “Music” (instructor) ⨝ teaches)
⨝ course_id, title (course))))
Could compute teaches ⨝ course_id, title (course) first, and join result with
dept_name= “Music” (instructor)
but the result of the first join is likely to be a large relation.
Only a small fraction of the university’s instructors are likely to be from the
Music department
• it is better to compute
dept_name= “Music” (instructor) ⨝ teaches
first.
Database System Concepts - 7th Edition 15.24 ©Silberschatz, Korth and Sudarshan
Cost-Based Optimization
Database System Concepts - 7th Edition 15.25 ©Silberschatz, Korth and Sudarshan
Dynamic Programming in Optimization
Database System Concepts - 7th Edition 15.26 ©Silberschatz, Korth and Sudarshan
Heuristic Optimization
Cost-based optimization is expensive, even with dynamic programming.
Systems may use heuristics to reduce the number of choices that must be
made in a cost-based fashion.
Heuristic optimization transforms the query-tree by using a set of rules that
typically (but not in all cases) improve execution performance:
• Perform selection early (reduces the number of tuples)
• Perform projection early (reduces the number of attributes)
• Perform most restrictive selection and join operations (i.e., with smallest
result size) before other similar operations.
• Some systems use only heuristics, others combine heuristics with partial
cost-based optimization.
Database System Concepts - 7th Edition 15.27 ©Silberschatz, Korth and Sudarshan