90 likes | 212 Views
CPS216: Advanced Database Systems Query Rewrite Rules for Subqueries. Shivnath Babu. More Query Rewrite Rules. Transform one logical plan into another Do not use statistics Equivalences in relational algebra Push-down predicates Do projects early Avoid cross-products if possible
E N D
CPS216: Advanced Database SystemsQuery Rewrite Rules for Subqueries Shivnath Babu
More Query Rewrite Rules • Transform one logical plan into another • Do not use statistics • Equivalences in relational algebra • Push-down predicates • Do projects early • Avoid cross-products if possible • Use left-deep trees • Use of constraints, e.g., uniqueness • Subqueries Joins (we will study this rewrite rule after we do physical plan selection)
SQL Query with an Uncorrelated Subquery Find the movies with stars born in 1960 MovieStar(name, address, gender, birthdate) StarsIn(title, year, starName) SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ );
Parse Tree <Query> <SFW> SELECT <SelList> FROM <FromList> WHERE <Condition> <Attribute> <RelName> <Tuple> IN <Query> title StarsIn <Attribute> ( <Query> ) starName <SFW> SELECT <SelList> FROM <FromList> WHERE <Condition> <Attribute> <RelName> <Attribute> LIKE <Pattern> name MovieStar birthDate ‘%1960’
title Two-argument selection StarsIn <condition> <tuple> IN name <attribute> birthdate LIKE ‘%1960’ starName MovieStar Generating Relational Algebra
Rewrite Rule for Two-argument Selection with Conditions Involving IN Two-argument selection <condition> X Lexp <condition> Lexp δ <tuple> IN Rexp Rexp
title starName=name StarsInδ name birthdate LIKE ‘%1960’ MovieStar Applying the Rewrite Rule title StarsIn <condition> <tuple> IN name <attribute> birthdate LIKE ‘%1960’ starName MovieStar
title title starName=name starName=name StarsIn name StarsInδ birthdate LIKE ‘%1960’ name birthdate LIKE ‘%1960’ MovieStar MovieStar Improving the Logical Query Plan
SQL Query with an Correlated Subquery MovieStar(name, address, gender, birthdate) StarsIn(title, year, starName) SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE name LIKE ‘Tom%’ and year = birthdate + 30 ); Can we rewrite this query as a single-block join query?