- 1.99 MB
- 2022-04-29 14:31:47 发布
- 1、本文档共5页,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
- 文档侵权举报电话:19940600175。
'Chapter13:QueryOptimization
Chapter13:QueryOptimizationIntroductionTransformationofRelationalExpressionsCatalogInformationforCostEstimationStatisticalInformationforCostEstimationCost-basedoptimizationDynamicProgrammingforChoosingEvaluationPlansMaterializedviews
IntroductionAlternativewaysofevaluatingagivenqueryEquivalentexpressionsDifferentalgorithmsforeachoperation
Introduction(Cont.)Anevaluationplandefinesexactlywhatalgorithmisusedforeachoperation,andhowtheexecutionoftheoperationsiscoordinated.Findouthowtoviewqueryexecutionplansonyourfavoritedatabase
Introduction(Cont.)CostdifferencebetweenevaluationplansforaquerycanbeenormousE.g.secondsvs.daysinsomecasesStepsincost-basedqueryoptimizationGeneratelogicallyequivalentexpressionsusingequivalencerulesAnnotateresultantexpressionstogetalternativequeryplansChoosethecheapestplanbasedonestimatedcostEstimationofplancostbasedon:Statisticalinformationaboutrelations.Examples:numberoftuples,numberofdistinctvaluesforanattributeStatisticsestimationforintermediateresultstocomputecostofcomplexexpressionsCostformulaeforalgorithms,computedusingstatistics
GeneratingEquivalentExpressions
TransformationofRelationalExpressionsTworelationalalgebraexpressionsaresaidtobeequivalentifthetwoexpressionsgeneratethesamesetoftuplesoneverylegaldatabaseinstanceNote:orderoftuplesisirrelevantwedon’tcareiftheygeneratedifferentresultsondatabasesthatviolateintegrityconstraintsInSQL,inputsandoutputsaremultisetsoftuplesTwoexpressionsinthemultisetversionoftherelationalalgebraaresaidtobeequivalentifthetwoexpressionsgeneratethesamemultisetoftuplesoneverylegaldatabaseinstance.AnequivalencerulesaysthatexpressionsoftwoformsareequivalentCanreplaceexpressionoffirstformbysecond,orviceversa
EquivalenceRules1.Conjunctiveselectionoperationscanbedeconstructedintoasequenceofindividualselections.2.Selectionoperationsarecommutative.3.Onlythelastinasequenceofprojectionoperationsisneeded,theotherscanbeomitted.SelectionscanbecombinedwithCartesianproductsandthetajoins.(E1XE2)=E1E21(E12E2)=E112E2
EquivalenceRules(Cont.)5.Theta-joinoperations(andnaturaljoins)arecommutative.E1E2=E2E16.(a)Naturaljoinoperationsareassociative:(E1E2)E3=E1(E2E3)(b)Thetajoinsareassociativeinthefollowingmanner:(E11E2)23E3=E113(E22E3)where2involvesattributesfromonlyE2andE3.
PictorialDepictionofEquivalenceRules
EquivalenceRules(Cont.)7.Theselectionoperationdistributesoverthethetajoinoperationunderthefollowingtwoconditions:(a)Whenalltheattributesin0involveonlytheattributesofoneoftheexpressions(E1)beingjoined.0E1E2)=(0(E1))E2(b)When1involvesonlytheattributesofE1and2involvesonlytheattributesofE2.1E1E2)=(1(E1))((E2))
EquivalenceRules(Cont.)8.Theprojectionoperationdistributesoverthethetajoinoperationasfollows:(a)ifinvolvesonlyattributesfromL1L2:(b)ConsiderajoinE1E2.LetL1andL2besetsofattributesfromE1andE2,respectively.LetL3beattributesofE1thatareinvolvedinjoincondition,butarenotinL1L2,andletL4beattributesofE2thatareinvolvedinjoincondition,butarenotinL1L2.))(())(()(21212121EEEELLLLÕÕ=ÕÈqq)))(())((()(212142312121EEEELLLLLLLLÈÈÈÈÕÕÕ=Õqq
EquivalenceRules(Cont.)ThesetoperationsunionandintersectionarecommutativeE1E2=E2E1E1E2=E2E1(setdifferenceisnotcommutative).Setunionandintersectionareassociative.(E1E2)E3=E1(E2E3)(E1E2)E3=E1(E2E3)Theselectionoperationdistributesover,and–.(E1–E2)=(E1)–(E2)andsimilarlyforandinplaceof–Also:(E1–E2)=(E1)–E2andsimilarlyforinplaceof–,butnotfor12.TheprojectionoperationdistributesoverunionL(E1E2)=(L(E1))(L(E2))
TransformationExample:PushingSelectionsQuery:FindthenamesofallinstructorsintheMusicdepartment,alongwiththetitlesofthecoursesthattheyteachname,title(dept_name=“Music”(instructor(teachescourse_id,title(course))))Transformationusingrule7a.name,title((dept_name=“Music”(instructor))(teachescourse_id,title(course)))Performingtheselectionasearlyaspossiblereducesthesizeoftherelationtobejoined.
ExamplewithMultipleTransformationsQuery:FindthenamesofallinstructorsintheMusicdepartmentwhohavetaughtacoursein2009,alongwiththetitlesofthecoursesthattheytaughtname,title(dept_name=“Music”gear=2009(instructor(teachescourse_id,title(course))))Transformationusingjoinassociatively(Rule6a):name,title(dept_name=“Music”gear=2009((instructorteaches)course_id,title(course)))Secondformprovidesanopportunitytoapplythe“performselectionsearly”rule,resultinginthesubexpressiondept_name=“Music”(instructor)year=2009(teaches)
MultipleTransformations(Cont.)
TransformationExample:PushingProjectionsConsider:name,title(dept_name=“Music”(instructor)teaches)course_id,title(course))))Whenwecompute(dept_name=“Music”(instructorteaches)weobtainarelationwhoseschemais:(ID,name,dept_name,salary,course_id,sec_id,semester,year)Pushprojectionsusingequivalencerules8aand8b;eliminateunneededattributesfromintermediateresultstoget:name,title(name,course_id(dept_name=“Music”(instructor)teaches))course_id,title(course))))Performingtheprojectionasearlyaspossiblereducesthesizeoftherelationtobejoined.
JoinOrderingExampleForallrelationsr1,r2,andr3,(r1r2)r3=r1(r2r3)(JoinAssociativity)Ifr2r3isquitelargeandr1r2issmall,wechoose(r1r2)r3sothatwecomputeandstoreasmallertemporaryrelation.
JoinOrderingExample(Cont.)Considertheexpressionname,title(dept_name=“Music”(instructor)teaches)course_id,title(course))))Couldcomputeteachescourse_id,title(course)first,andjoinresultwithdept_name=“Music”(instructor)buttheresultofthefirstjoinislikelytobealargerelation.Onlyasmallfractionoftheuniversity’sinstructorsarelikelytobefromtheMusicdepartmentitisbettertocomputedept_name=“Music”(instructor)teachesfirst.
EnumerationofEquivalentExpressionsQueryoptimizersuseequivalencerulestosystematicallygenerateexpressionsequivalenttothegivenexpressionCangenerateallequivalentexpressionsasfollows:RepeatapplyallapplicableequivalencerulesoneverysubexpressionofeveryequivalentexpressionfoundsofaraddnewlygeneratedexpressionstothesetofequivalentexpressionsUntilnonewequivalentexpressionsaregeneratedaboveTheaboveapproachisveryexpensiveinspaceandtimeTwoapproachesOptimizedplangenerationbasedontransformationrulesSpecialcaseapproachforquerieswithonlyselections,projectionsandjoins
ImplementingTransformationBasedOptimizationSpacerequirementsreducedbysharingcommonsub-expressions:whenE1isgeneratedfromE2byanequivalencerule,usuallyonlythetoplevelofthetwoaredifferent,subtreesbelowarethesameandcanbesharedusingpointersE.g.whenapplyingjoincommutativitySamesub-expressionmaygetgeneratedmultipletimesDetectduplicatesub-expressionsandshareonecopyTimerequirementsarereducedbynotgeneratingallexpressionsDynamicprogrammingWewillstudyonlythespecialcaseofdynamicprogrammingforjoinorderoptimizationE1E2
CostEstimationCostofeachoperatorcomputerasdescribedinChapter12NeedstatisticsofinputrelationsE.g.numberoftuples,sizesoftuplesInputscanberesultsofsub-expressionsNeedtoestimatestatisticsofexpressionresultsTodoso,werequireadditionalstatisticsE.g.numberofdistinctvaluesforanattributeMoreoncostestimationlater
ChoiceofEvaluationPlansMustconsidertheinteractionofevaluationtechniqueswhenchoosingevaluationplanschoosingthecheapestalgorithmforeachoperationindependentlymaynotyieldbestoverallalgorithm.E.g.merge-joinmaybecostlierthanhash-join,butmayprovideasortedoutputwhichreducesthecostforanouterlevelaggregation.nested-loopjoinmayprovideopportunityforpipeliningPracticalqueryoptimizersincorporateelementsofthefollowingtwobroadapproaches:1.Searchalltheplansandchoosethebestplaninacost-basedfashion.2.Usesheuristicstochooseaplan.
Cost-BasedOptimizationConsiderfindingthebestjoin-orderforr1r2...rn.Thereare(2(n–1))!/(n–1)!differentjoinordersforaboveexpression.Withn=7,thenumberis665280,withn=10,thenumberisgreaterthan176billion!Noneedtogenerateallthejoinorders.Usingdynamicprogramming,theleast-costjoinorderforanysubsetof{r1,r2,...rn}iscomputedonlyonceandstoredforfutureuse.
DynamicProgramminginOptimizationTofindbestjointreeforasetofnrelations:TofindbestplanforasetSofnrelations,considerallpossibleplansoftheform:S1(S–S1)whereS1isanynon-emptysubsetofS.RecursivelycomputecostsforjoiningsubsetsofStofindthecostofeachplan.Choosethecheapestofthe2n–2alternatives.Basecaseforrecursion:singlerelationaccessplanApplyallselectionsonRiusingbestchoiceofindicesonRiWhenplanforanysubsetiscomputed,storeitandreuseitwhenitisrequiredagain,insteadofrecomputingitDynamicprogramming
JoinOrderOptimizationAlgorithmprocedurefindbestplan(S)if(bestplan[S].cost)returnbestplan[S]//elsebestplan[S]hasnotbeencomputedearlier,computeitnowif(Scontainsonly1relation)setbestplan[S].planandbestplan[S].costbasedonthebestwayofaccessingS/*UsingselectionsonSandindicesonS*/elseforeachnon-emptysubsetS1ofSsuchthatS1SP1=findbestplan(S1)P2=findbestplan(S-S1)A=bestalgorithmforjoiningresultsofP1andP2cost=P1.cost+P2.cost+costofAifcost10IfindexonAisusedtofindtuplessatisfyingA>10,andtuplesupdatedimmediately,sametuplemaybefound(andupdated)multipletimesSolution1:Alwaysdeferupdatescollecttheupdates(oldandnewvaluesoftuples)andupdaterelationandindicesinsecondpassDrawback:extraoverheadevenife.g.updateisonlyonR.B,notonattributesinselectionconditionSolution2:DeferonlyifrequiredPerformimmediateupdateifupdatedoesnotaffectattributesinwhereclause,anddeferredupdatesotherwise.
JoinMinimizationJoinminimizationselectr.A,r.Bfromr,swherer.B=s.BCheckifjoinwithsisredundant,dropitE.g.joinconditionisonforeignkeyfromrtos,noselectiononsOthersufficientconditionspossibleselectr.A,s1.Bfromr,sass1,sass2wherer.B=s1.Bandr.B=s2.Bands1.A<20ands2.A<10joinwiths2isredundantandcanbedropped(alongwithselectionons2)Lotsofresearchinthisareasince70s/80s!
MultiqueryOptimizationExampleQ1:select*from(rnaturaljoint)naturaljoinsQ2:select*from(rnaturaljoinu)naturaljoinsBothqueriessharecommonsubexpression(rnaturaljoins)Maybeusefultocompute(rnaturaljoins)onceanduseitinbothqueriesButthismaybemoreexpensiveinsomesituationse.g.(rnaturaljoins)maybeexpensive,plansasshowninqueriesmaybecheaperMultiqueryoptimization:findbestoverallplanforasetofqueries,expoitingsharingofcommonsubexpressionsbetweenquerieswhereitisuseful
MultiqueryOptimization(Cont.)Simpleheuristicusedinsomedatabasesystems:optimizeeachqueryseparatelydetectandexploitingcommonsubexpressionsintheindividualoptimalqueryplansMaynotalwaysgivebestplan,butischeaptoimplementSharedscans:widelyusedspecialcaseofmultiqueryoptimizationSetofmaterializedviewsmaysharecommonsubexpressionsAsaresult,viewmaintenanceplansmaysharesubexpressionsMultiqueryoptimizationcanbeusefulinsuchsituations
ParametricQueryOptimizationExampleselect*fromrnaturaljoinswherer.a<$1valueofparameter$1notknownatcompiletimeknownonlyatruntimedifferentplansmaybeoptimalfordifferentvaluesof$1Solution1:optimizeatruntime,eachtimequeryissubmittedcanbeexpensiveSolution2:ParametricQueryOptimization:optimizergeneratesasetofplans,optimalfordifferentvaluesof$1Setofoptimalplansusuallysmallfor1to3parametersKeyissue:howtodofindsetofoptimalplansefficientlybestonefromthissetischosenatruntimewhen$1isknownSolution3:QueryPlanCachingIfoptimizerdecidesthatsameplanislikelytobeoptimalforallparametervalues,itcachesplanandreusesit,elsereoptimizeeachtimeImplementedinmanydatabasesystems
EndofChapter
Figure13.01
Figure13.02
Figure13.03
Figure13.04
Figure13.06
Figure13.08'
您可能关注的文档
- 汽车性能与检测技术全套配套课件PPT 学习情境4 汽车制动性能检测.ppt
- 施工企业会计第二版辛艳红配套教学课件PPT 第13章_财务报告.ppt
- 施工企业会计第二版辛艳红配套教学课件PPT 第8章 负债的核算(讲课).ppt
- 施工企业会计第二版辛艳红配套教学课件PPT 第2章 货币资金及交易性金融资产.ppt
- 施工企业会计第二版辛艳红配套教学课件PPT 第5章 长期股权投资.ppt
- 施工企业会计第二版辛艳红配套教学课件PPT 第4章_存货.ppt
- 数据结构课件PPT110章全 第三章 栈和队列1.ppt
- 数据结构课件PPT110章全 第二章.ppt
- 数据结构课件PPT110章全 第十章 内部排序old.ppt
- 数据库系统概念全套配套课件PPT ch5.ppt
- 数据库系统概念全套配套课件PPT ch3.ppt
- 数据库系统概念全套配套课件PPT ch1.ppt
- 数据库系统概念全套配套课件PPT ch25.ppt
- 数据库系统概念全套配套课件PPT ch24.ppt
- 数据库系统概念全套配套课件PPT ch17.ppt
- 数据库系统概念全套配套课件PPT ch20.ppt
- 数据库系统概念全套配套课件PPT ch19.ppt
- 数据库系统概念全套配套课件PPT ch11.ppt