0% found this document useful (0 votes)
0 views

Module4 Query Optimization

The document outlines steps for query optimization in relational algebra/SQL, including moving SELECT operations down the tree and replacing CARTESIAN PRODUCT with JOIN operations. It provides two example queries: one to find parts supplied by a supplier in Chennai and another to list suppliers who have supplied more than three red color products weighing 2 kg. The document also presents the optimal relational algebra for both queries.

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Module4 Query Optimization

The document outlines steps for query optimization in relational algebra/SQL, including moving SELECT operations down the tree and replacing CARTESIAN PRODUCT with JOIN operations. It provides two example queries: one to find parts supplied by a supplier in Chennai and another to list suppliers who have supplied more than three red color products weighing 2 kg. The document also presents the optimal relational algebra for both queries.

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Query Optimization

Course: CSE2004 – DBMS

Steps:
i) Initial query for relational algebra/SQL
ii) Moving SELECT operations down the tree
iii) Applying more restrictive SELECT operations first
iv) Replacing CARTESIAN PRODUCT with JOIN operations
v) Moving PROJECT operations down the tree

Schema:
supplier(sno, sname, address, city)
parts(pno, pname, color, weight)
supply(s_no, p_no, quantity)

Question 1: Find the name of all parts that are supplied by


supplier who lives in ‘chennai’
RA:

π parts . pname (σ ¿ ¿((supplier . city ¿ ' chennai ' )˄ ( supplier . sno=supply sno ) ˄ ¿( parts . pno=supply . pno))( supplier × supply ×

i) Initial query for relational algebra/SQL

π parts . pname

σ ((supplier . city ¿ chenna i )˄ ( supplier .sno=supply .s


' '
no ) ˄(parts . pno= supply . pno ))

parts supply supplier

ii) Moving SELECT operations down the tree


π parts . pname

σ (supplier . sno=supply . s no )
×

σ ( parts . pno=supply . p σ (supplier . city ¿ chenna i )


' '

no )
×

supplier
parts supply
iii) Applying more restrictive SELECT operations first
π parts . pname

σ ( parts . pno=supply . p no )
×

σ (supplier . sno=supply . s )
parts
no

σ (supplier . city ¿ chenna i )


' '
supply

supplier

iv) Replacing CARTESIAN PRODUCT with JOIN operations


π pname

( pno , p no )

( sno=s no )
parts

σ (city ¿ chenna i )
' ' supply

supplier
v) Moving PROJECT operations down the tree
π pname

( pno=p no )

( sno=s no )
π pno , pname

π sno πs parts
no , pno

σ (city ¿ chenna i )
' '
supply

supplier
Optimal RA:
π pname ¿ ¿ ¿
π s , p (supply)¿ ¿
no no

Schema:
supplier(sno, sname, address, city)
parts(pno, pname, color, weight)
supply(s_no, p_no, quantity)

Question 2: List the supplier name with his address who have
supplied more than 3 red color products of 2 kg weight

RA:

π supplier . sname ,supplier . address (σ ¿¿ (( parts . ¿ ¿ ¿ ¿ ' ℜd )˄¿ ( parts . weight=2 ) ˄¿ ( supply . quantity >3 ) ˄¿ ( supplier . sno=supply . s
'
i) Initial query for relational algebra/SQL

π supplier . sname ,supplier . address

σ ((parts .¿ ¿¿¿ ' ℜd )˄ ( parts . weight=2 )˄ (supply .quantity >3) ˄ ¿ (supplier . sno=supply . s
'
) ˄ ( parts . pno=supply . pno ))
no

parts supply supplier

ii) Moving SELECT operations down the tree

π supplier . sname ,supplier . address

σ ( parts . pno=supply . p no
)
×

σ ((parts .¿ ¿¿¿ ' ℜd )˄ ( parts . weight=2 ))


'

σ (supplier . sno=supply . s no )
×

parts
supplier σ (supply .quantity >3)

supply
iii) Applying more restrictive SELECT operations first
π supplier . sname ,supplier . address

σ (supplier . sno=supply . s no )
×

σ ( parts . pno=supply . p )
no
supplier
×

σ (supply .quantity> 3) σ ((parts .¿ ¿¿¿ ' ℜd )˄ ( parts . weight=2 ))


'

supply parts

iv) Replacing CARTESIAN PRODUCT with JOIN operations


π sname , address

sno=s no

pno= p no
supplier

σ (quantity>3 ) σ ((¿ ¿¿¿' ℜd )˄ (weight =2))


'

supply parts
v) Moving PROJECT operations down the tree

π sname , address

( sno=s no )

π sno , sname, address


( pno= pno )

πs no , pno π pno supplier

σ (quantity>3 ) σ ((¿ ¿¿¿' ℜd )˄ (weight =2))


'

supply parts
Optimal RA

π sname , address ¿ ¿ ¿
π pno (σ ((¿¿ ¿ ¿' ℜ d )˄ ( weight=2 )) ( parts))¿ ¿
'

You might also like