Lecture 11.pdf

πŸ’‘Recall

πŸ–‹Β Notes

SQL Statements Execution

Examples

  1. programs[id, pname, pdescription]

    groups[id, program, yearofstudy, gdescription]

    students[cnp, lastname, firstname, sgroup, gpa, addr, email]

    -- query: 
    --  Find students (lastname, firstname, year of study, program name, gpa) 
    --  in a given program (e.g. with id = 2, can be a parameter), 
    --  with a gpa >= 9 (can be a parameter).
    -- a
    SELECT lastname, firstname, yearofstudy, pname, gpa
    FROM students st, groups gr, programs pr
    WHERE st.sgroup = gr.id AND gr.program = pr.id AND program = 2 and gpa >= 9
    
    **πœ‹π›½(𝜎𝐢(𝑠𝑑𝑒𝑑𝑒𝑛𝑑𝑠 Γ— π‘”π‘Ÿπ‘œπ‘’π‘π‘  Γ— π‘π‘Ÿπ‘œπ‘”π‘Ÿπ‘Žπ‘šπ‘ ))**
    
    -- b
    SELECT lastname, firstname, yearofstudy, pname, gpa
    FROM (students st INNER JOIN groups gr ON st.sgroup = gr.id)
    																			 INNER JOIN programs pr ON gr.program = pr.id
    WHERE program = 2 AND gpa >= 9
    
    πœ‹π›½(𝜎𝐢1((𝑠𝑑𝑒𝑑𝑒𝑛𝑑𝑠⨂𝐢2π‘”π‘Ÿπ‘œπ‘’π‘π‘ )⨂𝐢3π‘π‘Ÿπ‘œπ‘”π‘Ÿπ‘Žπ‘šπ‘ ))
    
    -- c
    SELECT lastname, firstname, yearofstudy, pname, gpa
    FROM(
    			(
    				SELECT lastname, firstname, sgroup, gpa
    				FROM students
    				WHERE gpa >= 9
    			) st INNER JOIN (
    												SELECT * 
    												FROM groups 
    												WHERE program = 2
    											) gr ON st.sgroup = gr.id
    													 ) INNER JOIN (
    																					SELECT id, pname 
    																					FROM programs 
    																					WHERE id = 2
    																				) pr ON gr.program = pr.id
    
    πœ‹π›½(((πœ‹π›½1(𝜎𝐢2(𝑠𝑑𝑒𝑑𝑒𝑛𝑑𝑠))) βŠ—πΆ3 (𝜎𝐢4(π‘”π‘Ÿπ‘œπ‘’π‘π‘ ))) βŠ—πΆ5 (πœ‹π›½2(𝜎𝐢6(π‘π‘Ÿπ‘œπ‘”π‘Ÿπ‘Žπ‘šπ‘ ))))
    
  2. Galaxies[GID, GName, Diameter]

    SolarSystems[SID, SName, Galaxy, Mass]

    Planets[PID, PName, SolarSystem, AvgOrbitalSpeed, AvgRadius, Volume]

    -- Find planets (planet name, avg speed, solar system name, galaxy name, avg radius) 
    -- from galaxy with GID = 2, with a volume >= 1.5*10^15 km^3.
    
    -- a
    SELECT PName, AvgOrbitalSpeed, SName, GName, AvgRadius
    FROM Planets P, SolarSystems S, Galaxies G -- cross product by just enumerating 
    WHERE P.SolarSystem = S.SID AND S.Galaxy = G.GID AND Galaxy = 2 AND Volume >= 150000000000000
    
    **πœ‹π›½(𝜎𝐢(Planets Γ— SolarSystem Γ— Galaxies))**
    
    -- b
    SELECT PName, AvgOrbitalSpeed, SName, GName, AvgRadius
    FROM (Planets P INNER JOIN SolarSystems S ON P.SolarSystem = S.SID)
    																			    INNER JOIN Galaxies G ON S.Galaxy = G.GID
    WHERE Galaxy = 2 AND Volume >= 150000000000000
    
    πœ‹π›½(𝜎𝐢1((**Planets**⨂𝐢2**SolarSystem**)⨂𝐢3**Galaxies**))
    
    -- c
    SELECT PName, AvgOrbitalSpeed, SName, GName, AvgRadius
    FROM(
    			(
    				SELECT PName, AvgOrbitalSpeed, AvgRadius, SolarSystem
    				FROM Planets
    				WHERE Volume >= 150000000000000
    			) P INNER JOIN (
    												SELECT * 
    												FROM SolarSystems 
    												WHERE Galaxy = 2
    											) S ON P.SolarSystem = S.SID
    													 ) INNER JOIN (
    																					SELECT GID, GName 
    																					FROM Galaxies 
    																					WHERE GID = 2
    																				) G ON S.Galaxy = G.GID
    
    πœ‹π›½(((πœ‹π›½1(𝜎𝐢2(**Planets**))) βŠ—πΆ3 (𝜎𝐢4(**SolarSystem**))) βŠ—πΆ5 (πœ‹π›½2(𝜎𝐢6(**Galaxies**))))
    
    

Relational Algebra Operators - Evaluation

Algorithms

Table Scan

Index Seek

Index Scan

e.g.

B+ tree, entries a2

To obtain desired records:

β‡’ index scan

β‡’ get records’ addresses

β‡’ get records from relation

Cross Join

Nested Loops Join

Indexed Nested Loops Join

Merge Join

Hash Join