Docsity

Logical Query Plan - Database Systems - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Logical Query Plan - Database Systems - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Query Compilation

Parsing

Logical Query Plan

2

parse

convert

apply laws

estimate result sizes

consider physical plans estimate costs

pick best

execute

{P1,P2,…..}

{(P1,C1),(P2,C2)...}

Pi

answer

SQL query

parse tree

logical query plan

“improved” l.q.p

l.q.p. +sizes

statistics

Parsing

  • Goal is to convert a text string containing a

query into a parse tree data structure:

  • leaves form the text string (broken into lexical

elements)

  • internal nodes are syntactic categories
  • Uses standard algorithmic techniques from

compilers

  • given a grammar for the language (e.g., SQL),

process the string and build the tree

Example: SQL query

SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ );

(Find the movies with stars born in 1960)

Assume we have a simplified grammar for SQL.

The Preprocessor

  • replaces each reference to a view with a parse

(sub)-tree that describes the view (i.e., a

query)

  • does semantic checking:
    • are relations and views mentioned in the schema?
    • are attributes mentioned in the current scope?
    • are attribute types correct?

Outline

  • Convert SQL query to a parse tree
    • Semantic checking: attributes, relation names, types
  • Convert to a logical query plan (relational algebra

expression)

  • deal with subqueries
  • Improve the logical query plan
  • use algebraic transformations
  • group together certain operators
  • evaluate logical plan based on estimated size of relations
  • Convert to a physical query plan
  • search the space of physical plans
  • choose order of operations
  • complete the physical query plan

Conversion

  • Suppose there are no subqueries.
  • SELECT att-list FROM rel-list WHERE cond

is converted into

PROJ att-list (SELECT cond (PRODUCT( rel-list ))), or

π att-list (σ cond ( X ( rel-list )))

SELECT movieTitle FROM StarsIn, MovieStar WHERE starName = name AND birthdate LIKE '%1960';

11

SELECT FROM WHERE

, AND

movieTitle StarsIn LIKE

MovieStar birthdate '%1960'

=

starName name

Handling Subqueries

  • Recall the (equivalent) query: SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ );
  • Use an intermediate format called two-

argument selection

14

Πtitle

σ

StarsIn

IN πname

σbirthdate LIKE ‘%1960’

starName MovieStar

Example: Two-Argument Selection

Rules for IN

16

σ

R

t IN S R^ δ S

σ

X
C

C is the condition that equates attributes in t with corresponding attributes in S

17

Example: Logical Query Plan

πtitle

σstarName=name

StarsIn

σbirthdate LIKE ‘%1960’

MovieStar

×

πname

Outline

  • Convert SQL query to a parse tree
    • Semantic checking: attributes, relation names, types
  • Convert to a logical query plan (relational algebra

expression)

  • deal with subqueries
  • Improve the logical query plan
  • use algebraic transformations
  • group together certain operators
  • evaluate logical plan based on estimated size of relations
  • Convert to a physical query plan
  • search the space of physical plans
  • choose order of operations
  • complete the physical query plan

Improving the Logical Query Plan

  • There are numerous algebraic laws concerning

relational algebra operations

  • By applying them to a logical query plan

judiciously, we can get an equivalent query

plan that can be executed more efficiently

  • Next we'll survey some of these laws