Query Execution and Optimization
- 38Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What transformation is normally applied to an SQL query before its relational algebra operations are executed?
38 questions
- 1
What transformation is normally applied to an SQL query before its relational algebra operations are executed?
Model answer
The SQL query is converted into a relational algebra expression tree, which is then rewritten into an equivalent form that is more suitable for execution.
Explanation
The rewriting preserves the query's meaning while preparing the expression for efficient execution.
- 2
What two approaches are used to rewrite a relational algebra expression for execution?
Model answer
The expression can be rewritten using heuristic rules and algebraic transformations.
Explanation
Heuristics guide useful changes, while algebraic laws permit equivalent rearrangements of operators.
- 3
What is the purpose of cost-based query optimization?
Model answer
It compares execution choices for operators and selects a combination of methods with a low estimated overall cost.
Explanation
A relational algebra operator may have several applicable algorithms, each with a different estimated cost.
- 4
What does an execution plan specify?
Model answer
It specifies which algorithm will be used to evaluate each operator in the query.
Explanation
After the choices are fixed in the plan, the database can execute the query.
- 5
Why must an optimizer consider algorithm choices for different operators together rather than independently?
Model answer
Operator choices are interdependent: choosing a method for one operation can affect which methods or costs are appropriate elsewhere in the query.
Explanation
The operators are connected in the expression tree, so their execution choices can influence one another.
- 6
Why is query optimization based on estimated costs rather than guaranteed execution costs?
Model answer
The optimizer uses estimates based on available information, so it cannot know exactly how the data will behave during execution.
Explanation
Because decisions rely on estimates, an optimizer can sometimes choose a plan that performs poorly.
- 7
What is the main heuristic for placing selection and projection operators in a relational algebra tree?
Model answer
Push selections and projections as close to the leaf relations as possible, when the expression allows it.
Explanation
Applying these operations early can reduce the data that later operators must process.
- 8
How does pushing a selection toward the leaves affect intermediate results?
Model answer
It filters rows earlier, reducing the number of tuples passed to higher operators in the tree.
Explanation
Relational algebra tree nodes produce intermediate results, and early filtering can make those results smaller.
- 9
How does pushing a projection toward the leaves affect intermediate results?
Model answer
It removes unneeded attributes early, reducing the width of the tuples carried through intermediate results.
Explanation
A projection can limit each intermediate tuple to the fields required by later operations.
- 10
What is the general rule for deciding which attributes to carry upward from an intermediate result?
Model answer
Carry an attribute upward only if it is needed by a later operation or in the final output.
Explanation
Projections can discard attributes that will not be used higher in the tree.
- 11
How can the commutativity of two selection operations help rewrite a query?
Model answer
If two selections are both required, their order can be exchanged without changing the result.
Explanation
The lecture identifies selection operations as an example of operators whose order can sometimes be rearranged using algebraic laws.
- 12
How can the commutativity of a join help rewrite a query?
Model answer
A join can be written with its operands in either order, such as joining A with B or B with A.
Explanation
Equivalent operand orderings can offer alternative expression forms for optimization.
- 13
What does associativity allow an optimizer to change in a sequence of joins?
Model answer
It allows the grouping of joins to change, for example from (AextjoinB)extjoinC to Aextjoin(BextjoinC).
Explanation
Changing the grouping can create alternative evaluation orders while preserving the expression's result.
- 14
How can a selection condition be split when it is a conjunction of predicates?
Model answer
The conjunction can be separated into individual selections, which can then be applied in a suitable order or pushed toward the relations they concern.
Explanation
Separating predicates helps identify which relation or combination of relations each condition can be applied to.
- 15
When can a selection over a cross product be rewritten as a join?
Model answer
When the selection condition relates attributes from the cross product's relations, the cross product followed by that selection can be expressed as a join using the condition.
Explanation
This rewrite represents the required matching directly as a join rather than first forming the full cross product.
- 16
What factors can an operator's cost function take into account?
Model answer
It can consider the sizes of the input relations or files, the availability of indexes, and measures such as the number of block accesses.
Explanation
The optimizer uses these factors to estimate the cost of applying a particular algorithm.
- 17
How can the estimated costs of individual operations be used to compare complete execution plans?
Model answer
The optimizer estimates the overall cost of each plan, taking account of the choices made for its operators, and selects a plan with a low estimated cost.
Explanation
A plan's cost reflects the combined execution choices rather than just one operator in isolation.
- 18
Why is finding the best combination of operator methods described as an intractable problem?
Model answer
The possible choices are numerous and interdependent, making it difficult to find the best combination by exhaustive search.
Explanation
The lecture notes that optimization therefore relies heavily on heuristics and evaluation of candidate plans.
- 19
Why do query optimizers use heuristics when searching for an execution plan?
Model answer
Because the full optimization problem is intractable, heuristics help limit the search and identify promising plans.
Explanation
Candidate plans can then be assessed using estimated costs.
- 20
What are the main relations and requested output in the example query about professors?
Model answer
It uses professor, teaching, and enrollment relations and returns the professor's employee ID, name, and phone details.
Explanation
The query asks for professors who taught a course taken by a specified student.
- 21
What role does the specified student roll number play in the example query?
Model answer
It is used as a selection condition on the enrollment relation to identify the student's enrollments.
Explanation
Filtering enrollment by the roll number reduces the enrollment tuples considered.
- 22
How does the example query connect enrollment records to teaching records?
Model answer
It matches their course IDs, so the teaching record corresponds to a course in which the student is enrolled.
Explanation
The course ID equality is a join condition between enrollment and teaching.
- 23
How does the example query connect teaching records to professor records?
Model answer
It matches the teaching record's employee ID with the professor's employee ID.
Explanation
This join identifies the professor associated with the relevant teaching record.
- 24
How are the example query's selection predicates organized before being pushed down?
Model answer
The conjunctive condition is divided into groups that apply to enrollment, teaching, or multiple relations.
Explanation
Predicates that refer only to one relation can be applied to that relation before joining.
- 25
Why can a predicate involving both enrollment and teaching not be pushed to either relation alone?
Model answer
It depends on attributes from both relations, so it can be applied only after those relations have been combined.
Explanation
In the example, the course ID equality requires attributes from both enrollment and teaching.
- 26
After filtering enrollment in the example query, which attribute must be retained for the join?
Model answer
The course ID must be retained so the enrollment result can be joined with teaching.
Explanation
Other enrollment attributes can be discarded if they are not needed later in the query.
- 27
After filtering teaching in the example query, which attributes must be retained for later operations?
Model answer
The course ID and employee ID must be retained for the joins to enrollment and professor.
Explanation
The course ID connects teaching to enrollment, and the employee ID connects teaching to professor.
- 28
Why must the professor employee ID, name, and phone attributes be retained in the example query?
Model answer
They are needed to connect the professor to teaching and to produce the requested output.
Explanation
The employee ID supports the join, while the name and phone are part of the final result.
- 29
How can selection and projection be combined with reading records to prepare input for a join?
Model answer
As records are read, the system can apply the selection and keep only the required attributes, producing the reduced join input in one pass.
Explanation
Combining these steps avoids separately writing and rereading an intermediate result when it is unnecessary.
- 30
What is pipelining in query execution?
Model answer
Pipelining passes a stream of records from one operation to another so that multiple operators can process records without first materializing every intermediate result.
Explanation
Operators can be applied as records flow through the execution process.
- 31
When should an intermediate result be kept in memory rather than materialized?
Model answer
If it can be handled in memory, it is preferable to keep it there and avoid materializing it; materialize only when necessary.
Explanation
Keeping intermediate data in memory can support further processing without writing it out as a separate relation.
- 32
What does materializing an intermediate result mean in the context of query execution?
Model answer
It means creating and storing an intermediate relation rather than passing its records directly to the next operation.
Explanation
The lecture contrasts materializing results with keeping them in memory or processing them through a pipeline.
- 33
How can the size of an intermediate result influence its execution strategy?
Model answer
If the result fits in memory, it can be kept there; otherwise, materialization may be needed.
Explanation
The choice between in-memory handling and materialization depends in part on the intermediate result's size.
- 34
What distinguishes a compiled query from an ad hoc query?
Model answer
A compiled query is known in advance as part of a transaction or program, while an ad hoc query is newly submitted, such as by an analyst at a terminal.
Explanation
Knowing a compiled query ahead of time allows the system to prepare its execution plan in advance.
- 35
Why can compiled queries justify spending more time on optimization?
Model answer
They can be optimized offline and may be executed many times, so the cost of more careful optimization can be worthwhile.
Explanation
A better plan can benefit repeated executions of the same known query.
- 36
Why may an ad hoc query receive a lighter form of optimization?
Model answer
The user expects results quickly, and extensive optimization itself adds time before execution begins.
Explanation
A faster, less intensive optimization process can be appropriate when response time is important.
- 37
How does the expected frequency of execution affect optimization effort?
Model answer
A query expected to run many times can justify more effort to find a good plan, while a new one-off query may call for quicker optimization.
Explanation
The benefit of optimization depends partly on how often its chosen plan will be used.
- 38
What trade-off does a database system face when optimizing an ad hoc query?
Model answer
More optimization may improve the execution plan but also increases the time spent before the query runs.
Explanation
The optimizer must balance plan quality against the user's need for prompt results.

Ask anything
We've read this whole study set, so questions about it get the best answers.