1. Home
  2. Explore
  3. Computer Science
  4. Query Execution and Optimization

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Query Execution and Optimization

  • 45Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Query Plans

What is the usual first representation of an SQL query for query optimization?

45 questions

  1. 1

    What is the usual first representation of an SQL query for query optimization?

    1. A

      A relational algebra expression tree

    2. B

      A schedule of transactions to execute

    3. C

      A list of database indexes to create

    4. D

      A sequence of physical disk operations

    Correct answer
    A

    A relational algebra expression tree

    Explanation

    An SQL query is converted into a relational algebra expression tree, whose operators can then be optimized and assigned execution methods.

  2. 2

    What is the purpose of heuristic or algebraic optimization before choosing specific algorithms for operators?

    1. A

      To execute the original expression once and use its output to select a query plan

    2. B

      To determine the physical storage layout of every relation referenced by the query

    3. C

      To rewrite the relational algebra expression in a meaning-preserving way that is more suitable for execution

    4. D

      To replace the SQL query with a transaction schedule that guarantees serial execution

    Correct answer
    C

    To rewrite the relational algebra expression in a meaning-preserving way that is more suitable for execution

    Explanation

    Heuristic and algebraic optimization rewrite the initial relational algebra expression while preserving its meaning, aiming to make it more efficient to execute.

  3. 3

    What does cost-based query optimization add after the relational algebra expression has been rewritten?

    1. A

      It determines which relations should be included in the database

    2. B

      It changes the query's requested result to reduce its complexity

    3. C

      It replaces all relational algebra operators with indexes

    4. D

      It selects execution methods for operators using estimated costs

    Correct answer
    D

    It selects execution methods for operators using estimated costs

    Explanation

    Cost-based optimization compares applicable execution methods for operators and uses their estimated costs to choose a plan.

  4. 4

    Why must a query optimizer consider choices for multiple operators in combination?

    1. A

      The cost of each operator is independent of the relations produced by other operators

    2. B

      Every operator in a query tree must use the same execution algorithm

    3. C

      Operators can be optimized only after all intermediate results have been materialized

    4. D

      The choice made for one operator can affect the choices and costs of other operators in the tree

    Correct answer
    D

    The choice made for one operator can affect the choices and costs of other operators in the tree

    Explanation

    Operator choices are interdependent: an earlier method can affect the size or form of data consumed by later operators, so choices must be considered as part of an overall plan.

  5. 5

    What does an execution plan specify?

    1. A

      Which SQL statements will be stored in the database

    2. B

      Which values must be returned before the query can begin

    3. C

      Which attributes users are permitted to query

    4. D

      Which execution method will be used for each operator in the query

    Correct answer
    D

    Which execution method will be used for each operator in the query

    Explanation

    An execution plan fixes the method used to evaluate each operator and provides the plan that will be executed.

  6. 6

    Which sequence best describes the overall query-processing approach presented?

    1. A

      Translate SQL to relational algebra, rewrite the expression, choose methods using costs, and execute the plan

    2. B

      Choose operator algorithms, create indexes for every relation, and then translate the query into SQL

    3. C

      Materialize every possible intermediate result, select a result, and then construct an execution plan

    4. D

      Execute the SQL first, translate its results to relational algebra, and then estimate the storage costs

    Correct answer
    A

    Translate SQL to relational algebra, rewrite the expression, choose methods using costs, and execute the plan

    Explanation

    The query is first represented as relational algebra, then optimized through rewrites and cost-based method choices, and finally executed according to the resulting plan.

  7. 7

    Which heuristic generally improves a relational algebra tree by applying selections early?

    1. A

      Delay selections until after all projections have been removed

    2. B

      Apply each selection only after the complete query result is formed

    3. C

      Move selections above every join so they run on the largest results

    4. D

      Push selections toward the leaf relations whenever possible

    Correct answer
    D

    Push selections toward the leaf relations whenever possible

    Explanation

    Pushing selections toward the leaves filters base relations earlier, reducing the number of tuples that later operators must process.

  8. 8

    Why does pushing a selection closer to a base relation often reduce query-processing work?

    1. A

      It increases the number of attributes available to later operators

    2. B

      It can reduce the number of tuples passed to later operators

    3. C

      It eliminates the need to evaluate join conditions

    4. D

      It guarantees that every later operator can use an index

    Correct answer
    B

    It can reduce the number of tuples passed to later operators

    Explanation

    A selection applied early removes nonmatching tuples before they contribute to larger intermediate results.

  9. 9

    What is the main purpose of pushing projections toward the leaves?

    1. A

      To ensure that every intermediate tuple contains all base-relation attributes

    2. B

      To avoid carrying attributes that later operations do not need

    3. C

      To postpone attribute removal until after the final result is produced

    4. D

      To increase the number of rows available to each join

    Correct answer
    B

    To avoid carrying attributes that later operations do not need

    Explanation

    Pushing projections downward lets the query carry only needed attributes through intermediate results, reducing their tuple width.

  10. 10

    A query applies two selections to the same relation. Which rewrite is supported by the algebraic properties described?

    1. A

      One selection must be replaced by a projection

    2. B

      The two selections must be postponed until after all joins

    3. C

      The selections may be reordered only if the relation is first joined

    4. D

      The two selections may be applied in either order

    Correct answer
    D

    The two selections may be applied in either order

    Explanation

    The lecture notes that two selections can be applied in either order without changing the result.

  11. 11

    Which statement correctly describes the commutativity of a join?

    1. A

      Joining A with B gives the same result as projecting only A

    2. B

      A join can be reordered only when both relations have identical attributes

    3. C

      A join is equivalent to applying a selection to either input alone

    4. D

      Joining relation A with relation B gives the same result as joining B with A

    Correct answer
    D

    Joining relation A with relation B gives the same result as joining B with A

    Explanation

    The lecture identifies joins as commutative: the order of the two join operands can be exchanged without changing the result.

  12. 12

    What does associativity allow an optimizer to do with a sequence of joins?

    1. A

      Remove a join whenever its operands are evaluated in different orders

    2. B

      Replace every join with a selection on a single relation

    3. C

      Change the join predicates without affecting the result

    4. D

      Change the grouping of the joins while preserving the result

    Correct answer
    D

    Change the grouping of the joins while preserving the result

    Explanation

    Associativity permits regrouping, such as evaluating A joined with B and then C, or A joined with B joined to C under a different grouping.

  13. 13

    Which other operators are specifically described as associative, in addition to joins?

    1. A

      Union and intersection

    2. B

      Selection and projection

    3. C

      Projection and Cartesian product

    4. D

      Difference and renaming

    Correct answer
    A

    Union and intersection

    Explanation

    The lecture states that union, join, and intersection are associative, allowing certain regroupings of expressions.

  14. 14

    Which principle explains why selections and projections are pushed downward only when possible?

    1. A

      A rewrite must preserve the query's meaning and respect which relations an expression uses

    2. B

      Every operator must be placed directly above a base relation, regardless of its inputs

    3. C

      An operator may be moved anywhere as long as the final tree has the same height

    4. D

      Selections and projections can be pushed down only if the query contains no joins

    Correct answer
    A

    A rewrite must preserve the query's meaning and respect which relations an expression uses

    Explanation

    Optimization uses meaning-preserving algebraic laws, and an operation can be pushed down only to a point where its required input attributes and relations are available.

  15. 15

    What kinds of information can a cost function for an operator use?

    1. A

      Operand or file sizes and the availability of indexes

    2. B

      Only the name of the operator and the SQL keywords used

    3. C

      The order in which users submitted unrelated queries

    4. D

      Only the number of attributes in the final output

    Correct answer
    A

    Operand or file sizes and the availability of indexes

    Explanation

    Cost estimates can depend on the sizes of files or operands and on whether indexes are available, among other relevant parameters.

  16. 16

    How can the cost of a candidate relational algebra method be estimated, according to the lecture?

    1. A

      Measure only the number of output attributes

    2. B

      Assume every algorithm reads each relation exactly once

    3. C

      Count the number of words in the SQL query

    4. D

      Use formulas such as estimates of the number of block accesses

    Correct answer
    D

    Use formulas such as estimates of the number of block accesses

    Explanation

    The discussed formulas, including estimates of block accesses, can be used to construct cost estimates for alternative methods.

  17. 17

    What is the overall cost of a query plan intended to represent?

    1. A

      The cost of the most expensive operator considered in isolation

    2. B

      The size of the final result, regardless of how it was produced

    3. C

      The estimated cost of carrying out the plan with its selected methods

    4. D

      The number of distinct SQL statements in the database

    Correct answer
    C

    The estimated cost of carrying out the plan with its selected methods

    Explanation

    A plan's overall estimated cost accounts for the choices made for its operators, rather than considering only one isolated method.

  18. 18

    If several execution plans are available, what is the basic cost-based selection rule?

    1. A

      Choose the plan with the fewest relational algebra operators

    2. B

      Choose the plan that materializes the most intermediate results

    3. C

      Choose the plan that uses the same method for every operator

    4. D

      Choose the plan with the lowest estimated overall cost

    Correct answer
    D

    Choose the plan with the lowest estimated overall cost

    Explanation

    Cost-based optimization evaluates candidate plans using cost estimates and seeks the plan with the best, typically lowest, estimated cost.

  19. 19

    Why are query optimizers described as relying on estimates rather than certainty?

    1. A

      They choose methods without considering relation sizes or indexes

    2. B

      They do not have access to the query's relational algebra expression

    3. C

      They estimate costs and data behavior instead of knowing the exact execution behavior in advance

    4. D

      They execute every candidate plan fully before making a decision

    Correct answer
    C

    They estimate costs and data behavior instead of knowing the exact execution behavior in advance

    Explanation

    Optimization decisions are based on estimates, so actual data behavior or execution costs can differ from what the optimizer predicted.

  20. 20

    What consequence can follow from inaccurate cost estimates?

    1. A

      Every alternative execution method becomes unavailable

    2. B

      The optimizer may choose a plan that performs much worse than expected

    3. C

      The rewritten query must return a different logical result

    4. D

      The database can no longer evaluate relational algebra operators

    Correct answer
    B

    The optimizer may choose a plan that performs much worse than expected

    Explanation

    Because cost-based decisions depend on estimates, an optimizer can sometimes make choices that lead to very poor actual performance.

  21. 21

    Why are heuristics useful in query optimization?

    1. A

      The combined optimization problem is intractable, so practical optimizers use strategies to narrow or guide choices

    2. B

      The optimizer cannot compare costs for different execution methods

    3. C

      Heuristics make it unnecessary to preserve the query's meaning

    4. D

      Heuristics guarantee that estimated and actual costs are identical

    Correct answer
    A

    The combined optimization problem is intractable, so practical optimizers use strategies to narrow or guide choices

    Explanation

    The overall problem of choosing among interdependent operator methods is intractable, which motivates the use of heuristics and plan evaluation strategies.

  22. 22

    In the example query, what information is requested about professors?

    1. A

      Their course titles, grades, and classroom locations

    2. B

      Their course IDs, semester, and year

    3. C

      Their employee IDs, names, and phone details

    4. D

      Their students' roll numbers and enrollment dates

    Correct answer
    C

    Their employee IDs, names, and phone details

    Explanation

    The example projects the professor's employee ID, name, and phone from the professor relation.

  23. 23

    Which roles do the three relations play in the example query?

    1. A

      Professor stores semester dates, Teaching stores enrollment records, and Enrollment stores course instructors

    2. B

      Professor stores student enrollment, Teaching stores phone details, and Enrollment links professors to courses

    3. C

      Professor stores course grades, Teaching stores student roll numbers, and Enrollment stores professor names

    4. D

      Professor stores professor details, Teaching links professors to courses, and Enrollment records student course enrollment

    Correct answer
    D

    Professor stores professor details, Teaching links professors to courses, and Enrollment records student course enrollment

    Explanation

    The example describes professor details in Professor, who teaches what in Teaching, and student enrollment in Enrollment.

  24. 24

    Which condition selects the relevant student's enrollment records in the example?

    1. A

      The professor's employee ID equals CS08B207

    2. B

      The enrollment name equals the teaching semester

    3. C

      The enrollment roll number equals CS08B207

    4. D

      The teaching course ID equals the professor's phone number

    Correct answer
    C

    The enrollment roll number equals CS08B207

    Explanation

    The query restricts Enrollment using the specified student roll number, CS08B207.

  25. 25

    Which time-period conditions are applied to both Enrollment and Teaching in the example?

    1. A

      The semester is even and the year is 2010

    2. B

      The semester is spring and the year is 2012

    3. C

      The semester is even and the year is 2008

    4. D

      The semester is odd and the year is 2010

    Correct answer
    A

    The semester is even and the year is 2010

    Explanation

    The example applies an even-semester condition and a year-2010 condition to both the enrollment and teaching records.

  26. 26

    Before the example's selection conditions are decomposed, how is its initial relational algebra expression represented?

    1. A

      Select from a projection of Teaching after taking the union of all three relations

    2. B

      Project every attribute from a join of Professor with Enrollment only

    3. C

      Join Professor, Teaching, and Enrollment after projecting only the professor's phone

    4. D

      Project the professor's employee ID, name, and phone from a selection over the Cartesian product of Professor, Teaching, and Enrollment

    Correct answer
    D

    Project the professor's employee ID, name, and phone from a selection over the Cartesian product of Professor, Teaching, and Enrollment

    Explanation

    The initial expression places the full WHERE condition as a selection over the Cartesian product of the three FROM relations, with a projection for the requested professor attributes.

  27. 27

    Why can the example's WHERE condition be broken into smaller selection predicates?

    1. A

      The condition contains no references to relations, so all parts can be removed

    2. B

      Every WHERE condition can be split regardless of whether its predicates are logically connected

    3. C

      A conjunction requires every predicate to be applied only after the final projection

    4. D

      The condition is a conjunction, so its predicates can be separated and applied where their required relations are available

    Correct answer
    D

    The condition is a conjunction, so its predicates can be separated and applied where their required relations are available

    Explanation

    Because the condition is a conjunction, it can be separated into component predicates, including predicates that apply to individual relations or groups of relations.

  28. 28

    Which predicates from the example can be pushed directly to their corresponding base relations?

    1. A

      The professor-teaching employee-ID condition to Enrollment, and the course condition to Professor

    2. B

      The course-matching condition to Professor alone, and the roll-number condition to Teaching

    3. C

      All predicates only to Professor, because it supplies the projected attributes

    4. D

      The enrollment roll-number and time conditions to Enrollment, and the time conditions to Teaching

    Correct answer
    D

    The enrollment roll-number and time conditions to Enrollment, and the time conditions to Teaching

    Explanation

    Predicates involving only Enrollment can be applied to Enrollment, and predicates involving only Teaching can be applied to Teaching before those relations are combined.

  29. 29

    Why can a predicate involving attributes from two relations not initially be applied to either relation alone?

    1. A

      Such a predicate can be applied only after the final output is projected

    2. B

      Evaluating it requires values from both relations, which are available together only after the relations are combined

    3. C

      A predicate involving two relations is not part of relational algebra

    4. D

      Two-relation predicates always require a materialized copy of every base relation

    Correct answer
    B

    Evaluating it requires values from both relations, which are available together only after the relations are combined

    Explanation

    A predicate that references attributes from two relations needs both sets of values, so it cannot be evaluated on either base relation by itself.

  30. 30

    Which two matching conditions connect the relations in the example?

    1. A

      Professor employee ID equals Enrollment roll number, and Teaching course ID equals Enrollment semester

    2. B

      Professor phone equals Enrollment roll number, and Teaching semester equals Professor employee ID

    3. C

      Professor name equals Teaching course ID, and Enrollment year equals Professor phone

    4. D

      Professor employee ID equals Teaching employee ID, and Enrollment course ID equals Teaching course ID

    Correct answer
    D

    Professor employee ID equals Teaching employee ID, and Enrollment course ID equals Teaching course ID

    Explanation

    The example matches Professor to Teaching by employee ID and Enrollment to Teaching by course ID.

  31. 31

    How can a Cartesian product followed by a selection on matching attributes be rewritten?

    1. A

      As two independent selections that never combine the inputs

    2. B

      As a projection that removes the selection condition

    3. C

      As a union of the two input relations

    4. D

      As a join using the selection condition as the join condition

    Correct answer
    D

    As a join using the selection condition as the join condition

    Explanation

    A Cartesian product followed by a selection that specifies how the tuples match can be replaced by a join with that condition.

  32. 32

    After filtering Enrollment in the example, which attribute must be retained for its later join with Teaching?

    1. A

      Course ID

    2. B

      Professor phone

    3. C

      Teaching semester

    4. D

      Student name

    Correct answer
    A

    Course ID

    Explanation

    After the enrollment selection, Course ID is needed to match the enrollment record with Teaching.

  33. 33

    After filtering Teaching, which attributes must be retained for the later joins?

    1. A

      Phone and student roll number

    2. B

      Course ID and employee ID

    3. C

      Professor name and enrollment year

    4. D

      Semester and student name

    Correct answer
    B

    Course ID and employee ID

    Explanation

    Teaching needs Course ID to match Enrollment and employee ID to match Professor, so both attributes must be retained.

  34. 34

    After the Enrollment–Teaching join, which Teaching-derived attribute is still needed to connect the result to Professor?

    1. A

      Course name

    2. B

      Semester

    3. C

      Employee ID

    4. D

      Student roll number

    Correct answer
    C

    Employee ID

    Explanation

    The next join matches the result to Professor using employee ID, so that attribute must remain after the Enrollment–Teaching join.

  35. 35

    Which attributes must be retained from Professor to produce the example query's requested output?

    1. A

      Employee ID, name, and phone

    2. B

      Employee ID, course title, and classroom

    3. C

      Course ID, semester, and year

    4. D

      Student roll number, course ID, and grade

    Correct answer
    A

    Employee ID, name, and phone

    Explanation

    The final projection requests the professor's employee ID, name, and phone.

  36. 36

    What is the purpose of combining selection and projection with the reading of relation records?

    1. A

      To change the query's join conditions based on the first record read

    2. B

      To write a complete unfiltered copy of each relation before processing it

    3. C

      To defer all filtering until after every join has completed

    4. D

      To filter tuples and retain needed attributes while preparing the join input

    Correct answer
    D

    To filter tuples and retain needed attributes while preparing the join input

    Explanation

    As records are read, selection and projection can be applied to prepare a smaller join input without first writing an unnecessary intermediate relation.

  37. 37

    What does pipelining mean in the context of query execution?

    1. A

      Writing every operator's complete output to disk before starting the next operator

    2. B

      Executing all joins before reading any records from the base relations

    3. C

      Running several independent copies of the same query simultaneously

    4. D

      Passing a stream of records from one operator to another so multiple operations can be applied in sequence

    Correct answer
    D

    Passing a stream of records from one operator to another so multiple operations can be applied in sequence

    Explanation

    Pipelining lets operators process a stream of records and pass results onward, rather than requiring every intermediate result to be fully materialized first.

  38. 38

    When should an intermediate result be kept in memory rather than materialized?

    1. A

      Whenever the query includes a selection, regardless of its size

    2. B

      When it can be handled in memory; materialize it only when necessary

    3. C

      Never, because all intermediate results must be written to storage

    4. D

      Only when it contains every attribute from every base relation

    Correct answer
    B

    When it can be handled in memory; materialize it only when necessary

    Explanation

    The lecture recommends keeping intermediate tables in memory when feasible and materializing them only when necessary.

  39. 39

    Why is it often wasteful to materialize the output of a selection before immediately projecting it?

    1. A

      Materialization removes the join attributes from the base relation

    2. B

      The selection and projection can be performed as records are read, avoiding an unnecessary intermediate write

    3. C

      Projection must always be evaluated before selection

    4. D

      A selection cannot produce records that a projection can process

    Correct answer
    B

    The selection and projection can be performed as records are read, avoiding an unnecessary intermediate write

    Explanation

    The operators can be combined during input processing, so writing the selection result and then reading it again just to project attributes may be unnecessary.

  40. 40

    How should an optimizer choose among different methods for evaluating joins in a query?

    1. A

      Always choose the method with the most complex implementation

    2. B

      Use the same join method in every query, regardless of available indexes

    3. C

      Compare applicable methods using cost estimates and account for the surrounding plan

    4. D

      Choose a method before considering the sizes of the join inputs

    Correct answer
    C

    Compare applicable methods using cost estimates and account for the surrounding plan

    Explanation

    Different join methods may apply, and their estimated costs depend on factors such as input sizes and indexes; choices also interact with the rest of the plan.

  41. 41

    What is a compiled query in the lecture's comparison?

    1. A

      A query that contains no relational algebra operators

    2. B

      A query whose use is known ahead of time because it is part of a transaction program

    3. C

      A query that has already materialized every intermediate relation

    4. D

      A query newly typed by an analyst with no prior knowledge of its use

    Correct answer
    B

    A query whose use is known ahead of time because it is part of a transaction program

    Explanation

    Compiled queries are known in advance as part of transactions or programs that perform database work.

  42. 42

    Why can a compiled query justify spending more time on optimization?

    1. A

      Their results are always small enough to remain in memory

    2. B

      Compiled queries never need cost estimates

    3. C

      They do not incur any execution cost after compilation

    4. D

      Its plan may be reused many times, so optimization effort can pay off across repeated executions

    Correct answer
    D

    Its plan may be reused many times, so optimization effort can pay off across repeated executions

    Explanation

    Because compiled queries may execute hundreds of times, spending more effort to find an effective plan can be worthwhile.

  43. 43

    What is an ad-hoc query in the described setting?

    1. A

      A query that can only be run after all tables are copied to disk

    2. B

      A query embedded in a transaction program and known in advance

    3. C

      A new query entered by an analyst to investigate data

    4. D

      A query that is executed repeatedly with a fixed plan

    Correct answer
    C

    A new query entered by an analyst to investigate data

    Explanation

    An ad-hoc query is a new query, such as one an analyst types while exploring data, rather than a query known in advance as part of a program.

  44. 44

    Why might an optimizer use a lighter optimization process for an ad-hoc query?

    1. A

      Ad-hoc queries do not use intermediate results

    2. B

      A lighter process guarantees a lower total execution cost

    3. C

      The user expects a quick response, and optimization itself adds time before execution

    4. D

      Ad-hoc queries cannot be represented as relational algebra

    Correct answer
    C

    The user expects a quick response, and optimization itself adds time before execution

    Explanation

    For ad-hoc queries, spending too long optimizing can delay the response, so a faster, lighter optimization approach may be preferable.

  45. 45

    Which optimization trade-off best distinguishes compiled queries from ad-hoc queries?

    1. A

      Compiled queries must use materialization, while ad-hoc queries must use pipelining

    2. B

      Compiled queries require no optimization, while ad-hoc queries require exhaustive optimization

    3. C

      Compiled queries use only selections, while ad-hoc queries use only joins

    4. D

      Compiled queries can justify more offline optimization, while ad-hoc queries may need faster optimization

    Correct answer
    D

    Compiled queries can justify more offline optimization, while ad-hoc queries may need faster optimization

    Explanation

    Known, repeatedly executed compiled queries can justify substantial offline optimization, while ad-hoc queries often need quicker optimization because response time matters.

Question 1 of 45
Question 1 of 45

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent