L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution...
Transcript of L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution...
![Page 1: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/1.jpg)
256
L22:TheRelationalModel(continued)
CS3200 Databasedesign(sp18 s2)https://course.ccs.neu.edu/cs3200sp18s2/4/5/2018
![Page 2: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/2.jpg)
257
Announcements!
• Pleasepickupyourexamifyouhavenotyet• HW6 willincludeRedis andMongoDBexerciseswithminimalinstalloverhead(stillinpreparation)
• Finalclasscalendar• Outlinetoday- Relationalalgebra- Optimization- NoSQL(start,continuingnexttime)
![Page 3: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/3.jpg)
258
![Page 4: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/4.jpg)
259
RDBMSArchitecture
• HowdoesaSQLenginework?
SQLQuery
RelationalAlgebra(RA)
Plan
OptimizedRAPlan Execution
Declarativequery(fromuser)
Translatetorelationalalgebraexpresson
Findlogicallyequivalent- butmoreefficient- RAexpression
Executeeachoperatoroftheoptimizedplan!
![Page 5: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/5.jpg)
260
RDBMSArchitecture
• HowdoesaSQLenginework?
SQLQuery
RelationalAlgebra(RA)
Plan
OptimizedRAPlan Execution
RelationalAlgebraallowsustotranslatedeclarative(SQL)queriesintopreciseandoptimizable expressions!
![Page 6: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/6.jpg)
261
RelationalAlgebra(RA)
• Fivebasicoperators:1. Selection:s2. Projection:P3. CartesianProduct:´4. Union:È5. Difference:-
• Derivedorauxiliaryoperators:- Intersection,complement- Joins(natural,equi-join,thetajoin,semi-join)- Renaming:r- Division
We’lllookatthesefirst!
Andalsoatoneexampleofaderivedoperator(naturaljoin)andaspecialoperator(renaming)
![Page 7: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/7.jpg)
262
Keepinmind:RAoperatesonsets!
• RDBMSsusemultisets,howeverinrelationalalgebraformalismwewillconsidersets!
• Also:wewillconsiderthenamedperspective,whereeveryattributemusthaveauniquename- àattributeorderdoesnotmatter…
NowontothebasicRAoperators…
![Page 8: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/8.jpg)
263
1.Selection(𝜎)• Returnsalltupleswhichsatisfyacondition• Notation:sc (R)• Examples- sSalary >40000 (Employee)- sname =“Smith” (Employee)
• Theconditionccanbe=,<,£,>, ³,<>
SELECT *FROM StudentsWHERE gpa > 3.5;
SQL:
RA:𝜎{|}~�.w(𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠)
Students(sid,sname,gpa)
![Page 9: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/9.jpg)
264
sSalary > 40000 (Employee)
SSN Name Salary1234545 John 2000005423341 Smith 6000004352342 Fred 500000
SSN Name Salary5423341 Smith 6000004352342 Fred 500000
Anotherexample:
![Page 10: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/10.jpg)
265
2.Projection(Π)
• Eliminatescolumns,thenremovesduplicates
• Notation:P A1,…,An (R)• Example:projectsocial-securitynumberandnames:- P SSN,Name (Employee)- Outputschema:Answer(SSN,Name)
SELECT DISTINCTsname,gpa
FROM Students;
SQL:
RA:Π�&}(�,{|}(𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠)
Students(sid,sname,gpa)
![Page 11: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/11.jpg)
266
P SSN (Employee)
SSN Name Salary1234545 John 2000005423341 John 6000004352342 John 200000
Anotherexample:
SSN123454554233414352342
![Page 12: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/12.jpg)
267
P Name,Salary (Employee)
SSN Name Salary1234545 John 2000005423341 John 6000004352342 John 200000
Name SalaryJohn 200000John 600000
Anotherexample:
![Page 13: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/13.jpg)
268
NotethatRAOperatorsareCompositional!
SELECT DISTINCTsname,gpa
FROM StudentsWHERE gpa > 3.5;
Students(sid,sname,gpa)
HowdowerepresentthisqueryinRA?
Π�&}(�,{|}(𝜎{|}~�.w(𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠))
𝜎{|}~�.w(Π�&}(�,{|}(𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠))
Aretheselogicallyequivalent?
![Page 14: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/14.jpg)
269
3.Cross-Product(×)
• EachtupleinR1witheachtupleinR2
• Notation:R1´ R2• Example:- Employee´ Dependents
• Rareinpractice;mainlyusedtoexpressjoins
SELECT *FROM Students, People;
SQL:
RA:𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠×𝑃𝑒𝑜𝑝𝑙𝑒
Students(sid,sname,gpa)People(ssn,pname,address)
![Page 15: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/15.jpg)
270
ssn pname address1234545 John 216 Rosse
5423341 Bob 217 Rosse
sid sname gpa001 John 3.4
002 Bob 1.3
𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠×𝑃𝑒𝑜𝑝𝑙𝑒
×
ssn pname address sid sname gpa1234545 John 216 Rosse 001 John 3.4
5423341 Bob 217 Rosse 001 John 3.4
1234545 John 216 Rosse 002 Bob 1.3
5423341 Bob 216 Rosse 002 Bob 1.3
People StudentsAnotherexample:
![Page 16: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/16.jpg)
271
4.Renaming(𝜌)
• Changestheschema,nottheinstance
• A‘special’operator- neitherbasicnorderived
• Notation:r B1,…,Bn (R)
• Note:thisisshorthand fortheproperform(sincenames,notorder matters!):- r A1àB1,…,AnàBn (R)
SELECTsid AS studId,sname AS name,gpa AS gradePtAvg
FROM Students;
SQL:
RA:𝜌������,&}(�,{�}��k���{(𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠)
Students(sid,sname,gpa)
Wecareaboutthisoperatorbecause weareworkinginanamedperspective
![Page 17: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/17.jpg)
272
sid sname gpa001 John 3.4
002 Bob 1.3
𝜌������,&}(�,{�}��k���{(𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠)
Students
studId name gradePtAvg001 John 3.4
002 Bob 1.3
Students
Anotherexample:
![Page 18: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/18.jpg)
273
5.NaturalJoin(⋈)
• Notation:R1⋈R2
• JoinsR1 andR2 onequalityofallsharedattributes- IfR1 hasattributesetA,andR2 hasattributesetB,and
theyshareattributesA⋂B=C,canalsobewritten:R1⋈ 𝐶R2
• OurfirstexampleofaderivedRAoperator:- Meaning:R1⋈ R2 =PAUB(sC=D(𝜌�→�(R1)´ R2))- Where:
• Therename𝜌�→� renamesthesharedattributesinoneoftherelations
• TheselectionsC=Dchecksequalityofthesharedattributes• TheprojectionPAUBeliminatestheduplicatecommon
attributes
SELECT DISTINCTssid, S.name, gpa,ssn, address
FROM Students S,People P
WHERE S.name = P.name;
SQL:
RA:𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 ⋈ 𝑃𝑒𝑜𝑝𝑙𝑒
Students(sid,name,gpa)People(ssn,name,address)
![Page 19: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/19.jpg)
274
ssn P.name address1234545 John 216 Rosse
5423341 Bob 217 Rosse
sid S.name gpa001 John 3.4
002 Bob 1.3
𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 ⋈ 𝑃𝑒𝑜𝑝𝑙𝑒
⋈
sid S.name gpa ssn address001 John 3.4 1234545 216 Rosse
002 Bob 1.3 5423341 216 Rosse
People PStudents SAnotherexample:
![Page 20: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/20.jpg)
275
NaturalJoinpractice
• GivenschemasR(A,B,C,D),S(A,C,E),whatistheschemaofR⋈S?
• GivenR(A,B,C),S(D,E),whatisR⋈S?
• GivenR(A,B),S(A,B),whatisR⋈S?
![Page 21: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/21.jpg)
276
Example:ConvertingSFWQuery->RA
SELECT DISTINCTgpa,address
FROM Students S,People P
WHERE gpa > 3.5 ANDsname = pname;
HowdowerepresentthisqueryinRA?
Π{|},}������(𝜎{|}~�.w(𝑆 ⋈ 𝑃))
Students(sid,sname,gpa)People(ssn,sname,address)
![Page 22: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/22.jpg)
277
LogicalEquivaleceofRAPlans
• GivenrelationsR(A,B)andS(B,C):
- Here,projection&selectioncommute:• 𝜎�Sw(Π�(𝑅)) = Π�(𝜎�Sw(𝑅))
- Whatabouthere?• 𝜎�Sw(Π�(𝑅))? = Π�(𝜎�Sw(𝑅))
We’lllookatthisinmoredepthinqueryoptimization…
![Page 23: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/23.jpg)
278
RDBMSArchitecture
• HowdoesaSQLenginework?
SQLQuery
RelationalAlgebra(RA)
Plan
OptimizedRAPlan Execution
WesawhowwecantransformdeclarativeSQLqueriesintoprecise,compositionalRAplans
![Page 24: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/24.jpg)
279
RDBMSArchitecture
• HowdoesaSQLenginework?
SQLQuery
RelationalAlgebra(RA)
Plan
OptimizedRAPlan Execution
We’lllookathowtothenoptimizetheseplans
![Page 25: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/25.jpg)
280
RDBMSArchitecture
• HowistheRA“plan”executed?
SQLQuery
RelationalAlgebra(RA)
Plan
OptimizedRAPlan Execution
Wehavealreadyseenhowtoexecuteafewbasicoperators!
![Page 26: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/26.jpg)
281
RAPlanExecution
• NaturalJoin/Join:- Wesawhowtousememory&IOcostconsiderations topickthecorrectalgorithmto
executeajoinwithBNLJ orSMJ (weskippedHJ)
• Selection:- Wesawhowtouseindexestoaidselection- Canalwaysfallbackonscan/binarysearchaswell
• Projection:- Themainoperationhereisfindingdistinctvaluesoftheprojecttuples;webriefly
discussedhowtodothiswithsorting (weskippedhashing)
Wealreadyknowhowtoexecuteallthebasicoperators!
![Page 27: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/27.jpg)
282
3.AdvancedRelationalAlgebra(verybrief)
![Page 28: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/28.jpg)
283
Whatwewillbrieflycovernext
• SetOperationsinRA
• Extensions&Limitations
![Page 29: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/29.jpg)
284
RelationalAlgebra(RA)
• Fivebasicoperators:1. Selection:s2. Projection:P3. CartesianProduct:´4. Union:È5. Difference:-
• Derivedorauxiliaryoperators:- Intersection,complement- Joins(natural,equi-join,thetajoin,semi-join)- Renaming:P- Division
We’lllookatthese
![Page 30: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/30.jpg)
285
1.Union(È) and2.Difference(–)
• R1È R2• Example:- ActiveEmployeesÈ RetiredEmployees
• R1– R2• Example:- AllEmployees-- RetiredEmployees
R1 R2
R1 R2
![Page 31: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/31.jpg)
286
WhataboutIntersection(Ç) ?
• Itisaderivedoperator• R1Ç R2 =R1 – (R1 – R2)• Alsoexpressedasajoin!• Example- UnionizedEmployeesÇ RetiredEmployees
R1 R2
![Page 32: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/32.jpg)
287
RAExpressionsCanGetComplex!
sname=fred sname=gizmo
P pidP ssn
seller-ssn=ssn
pid=pid
buyer-ssn=ssn
P name
PersonPurchasePersonProduct
![Page 33: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/33.jpg)
288
OperationsonMultisets
• AllRAoperationsneedtobedefinedcarefullyonbags
- sC(R):preservethenumberofoccurrences
- PA(R):noduplicateelimination
- Cross-product,join:noduplicateelimination
Thisisimportant- relationalenginesworkonmultisets,notsets!
![Page 34: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/34.jpg)
289
RAhasLimitations!
• Cannotcompute“transitiveclosure”
• FindalldirectandindirectrelativesofFred• CannotbeexpressedinRA!- NeedtowriteCprogram,useagraphengine,ormodernSQL…
Name1 Name2 RelationshipFred Mary FatherMary Joe CousinMary Bill SpouseNancy Lou Sister
![Page 35: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/35.jpg)
290
Activity-45.ipynb
aspartofHW6
![Page 36: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/36.jpg)
291
L22:QueryOptimization
CS3200 Databasedesign(sp18 s2)https://course.ccs.neu.edu/cs3200sp18s2/4/5/2018
![Page 37: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/37.jpg)
292
Logicalvs.PhysicalOptimization
• Logicaloptimization:- Findequivalentplansthataremoreefficient- Intuition:Minimize#oftuplesateachstepbychangingthe
orderofRAoperators
• Physicaloptimization:- FindalgorithmwithlowestIOcosttoexecuteourplan- Intuition:Calculatebasedonphysicalparameters(buffer
size,etc.)andestimatesofdatasize(histograms)
• WeonlydiscussLogicaloptimizationtoday
Execution
SQLQuery
RelationalAlgebra(RA)Plan
OptimizedRAPlan
![Page 38: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/38.jpg)
293
1.LogicalOptimization
1) OptimizationofRAPlans2) ACTIVITY:RAPlanOptimization
![Page 39: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/39.jpg)
294
RDBMSArchitecture
• HowdoesaSQLenginework?
SQLQuery
RelationalAlgebra(RA)
Plan
OptimizedRAPlan Execution
Declarativequery(fromuser)
Translatetorelationalalgebraexpresson
Findlogicallyequivalent- butmoreefficient- RAexpression
Executeeachoperatoroftheoptimizedplan!
![Page 40: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/40.jpg)
295
RDBMSArchitecture
• HowdoesaSQLenginework?
SQLQuery
RelationalAlgebra(RA)
Plan
OptimizedRAPlan Execution
RelationalAlgebraallowsustotranslatedeclarative(SQL)queriesintopreciseandoptimizable expressions!
![Page 41: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/41.jpg)
296
RelationalAlgebra(RA)
• Fivebasicoperators:1. Selection:s2. Projection:P3. CartesianProduct:´4. Union:È5. Difference:-
• Derivedorauxiliaryoperators:- Intersection,complement- Joins(natural,equi-join,thetajoin,semi-join)- Renaming:r- Division
We’lllookatthesefirst!
Andalsoatoneexampleofaderivedoperator(naturaljoin)andaspecialoperator(renaming)
![Page 42: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/42.jpg)
297
Recall:ConvertingSFWQuery->RA
SELECT DISTINCTgpa,address
FROM Students S,People P
WHERE gpa > 3.5 ANDsname = pname;
HowdowerepresentthisqueryinRA?
Π{|},}������(𝜎{|}~�.w(𝑆 ⋈ 𝑃))
Students(sid,sname,gpa)People(ssn,sname,address)
![Page 43: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/43.jpg)
298
Recall:LogicalEquivalenceofRAPlans
• GivenrelationsR(A,B)andS(B,C):
- Here,projection&selectioncommute:• 𝜎�Sw(Π�(𝑅)) = Π�(𝜎�Sw(𝑅))
- Whatabouthere?• 𝜎�Sw(Π�(𝑅))?= Π�(𝜎�Sw(𝑅))
We’lllookatthisinmoredepthlaterinthelecture…
![Page 44: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/44.jpg)
299
RDBMSArchitecture
• HowdoesaSQLenginework?
SQLQuery
RelationalAlgebra(RA)
Plan
OptimizedRAPlan Execution
We’lllookathowtothenoptimizetheseplansnow
![Page 45: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/45.jpg)
300
Note:Wecanvisualizetheplanasatree
Π�
R(A,B) S(B,C)
Π�(𝑅 𝐴, 𝐵 ⋈ 𝑆 𝐵, 𝐶 )
Bottom-uptreetraversal=orderofoperationexecution!
![Page 46: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/46.jpg)
301
Asimpleplan
WhatSQLquerydoesthiscorrespondto?
ArethereanylogicallyequivalentRAexpressions?
Π�
R(A,B) S(B,C)
![Page 47: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/47.jpg)
302
“Pushingdown”projection
Π�
R(A,B) S(B,C)
Π�
Whymightwepreferthisplan?
Π�
R(A,B) S(B,C)
![Page 48: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/48.jpg)
303
Takeaways
• Thisprocessiscalledlogicaloptimization
• Manyequivalentplansusedtosearchfor“goodplans”
• Relationalalgebraisanimportantabstraction.
![Page 49: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/49.jpg)
304
RAcommutators
• Thebasiccommutators:- Pushprojection through(1)selection,(2)join- Pushselection through(3)selection,(4)projection,(5)join- Also:Joinscanbere-ordered!
• Notethatthisisnotanexhaustivesetofoperations
ThissimplesetoftoolsallowsustogreatlyimprovetheexecutiontimeofqueriesbyoptimizingRAplans!
![Page 50: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/50.jpg)
305
OptimizingtheSFW RAPlan
![Page 51: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/51.jpg)
306
Π�,�
R(A,B) S(B,C)
T(C,D)
sA<10
Π�,�(𝜎��"T 𝑇 ⋈ 𝑅 ⋈ 𝑆 )
SELECT R.A,S.DFROM R,S,TWHERE R.B = S.B
AND S.C = T.CAND R.A < 10;
R(A,B) S(B,C) T(C,D)
TranslatingtoRA
![Page 52: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/52.jpg)
307
LogicalOptimization
• Heuristically,wewantselectionsandprojectionstooccurasearlyaspossibleintheplan- Terminology:“pushdownselections”and“pushingdownprojections.”
• Intuition:Wewillhavefewertuplesinaplan.- Couldfailiftheselectionconditionisveryexpensive(sayrunssomeimageprocessing
algorithm).- Projectioncouldbeawasteofeffort,butmorerarely.
![Page 53: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/53.jpg)
308
Π�,�
R(A,B) S(B,C)
T(C,D)
sA<10
Π�,�(𝜎��"T 𝑇 ⋈ 𝑅 ⋈ 𝑆 )
SELECT R.A,S.DFROM R,S,TWHERE R.B = S.B
AND S.C = T.CAND R.A < 10;
OptimizingRAPlan
PushdownselectiononAsoitoccursearlier
R(A,B) S(B,C) T(C,D)
![Page 54: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/54.jpg)
309
Π�,�
R(A,B)
S(B,C)
T(C,D)
Π�,� 𝑇 ⋈ 𝜎��"T(𝑅) ⋈ 𝑆
SELECT R.A,S.DFROM R,S,TWHERE R.B = S.B
AND S.C = T.CAND R.A < 10;
OptimizingRAPlan
PushdownselectiononAsoitoccursearlier
sA<10
R(A,B) S(B,C) T(C,D)
![Page 55: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/55.jpg)
310
Π�,�
R(A,B)
S(B,C)
T(C,D)
Π�,� 𝑇 ⋈ 𝜎��"T(𝑅) ⋈ 𝑆
SELECT R.A,S.DFROM R,S,TWHERE R.B = S.B
AND S.C = T.CAND R.A < 10;
OptimizingRAPlan
Pushdownprojectionsoitoccursearlier
sA<10
R(A,B) S(B,C) T(C,D)
![Page 56: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/56.jpg)
311
Π�,�
R(A,B)
S(B,C)
T(C,D)
Π�,� 𝑇 ⋈ Π�,� 𝜎��"T(𝑅) ⋈ 𝑆
SELECT R.A,S.DFROM R,S,TWHERE R.B = S.B
AND S.C = T.CAND R.A < 10;
OptimizingRAPlanWeeliminateBearlier!
sA<10
Π�,�
Ingeneral,whenisanattributenotneeded…?
R(A,B) S(B,C) T(C,D)
![Page 57: L22: The Relational Model (continued)...Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expresson Find logically](https://reader033.fdocuments.us/reader033/viewer/2022060802/60867d4ce8ec9b4023302148/html5/thumbnails/57.jpg)
312
Activity-47.ipynb
aspartofHW6