数据库查询优化技术(9.2.3)LiHaiXiang2013-101数据库查询优化技术:DatabasePostgreSQLMySQLBigDataetc23Email:database_XX@126.com4Blog:`dliketosharetechnologywithfriends5WhoamI?2数据库查询优化技术?WhatistheQueryOptimizationTechnologyOfPostgreSQL?23HowtoimplementtheQueryOptimizationOfPostgreSQL?3数据库查询优化技术①Thequeryresultreuse②Thequeryplanreuse2.TheRuleOfQueryRewrite①BasedonRelationalAlgebraandHeuristicRule②ViewRewrite、Sub-queryOptimization、EquivalentPredicateRewrite、ConditionSimplification、OuterJoinElimination、JoinElimination、NestJoinElimination③SematicOptimization3.TheAlgorithmOfQueryOptimization①SingleTableScanAlgorithm②TwoTableJoinAlgorithm③Multi-tableJoinAlgorithm4.ParallelQueryOptimization5.DistributeQueryOptimizationTheTechnologyOfQueryOptimization14数据库查询优化技术①Thequeryresultreuse②Thequeryplanreuse2.TheRuleOfQueryRewrite①BasedonRelationalAlgebraandHeuristicRule②ViewRewrite、Sub-queryOptimization、EquivalentPredicateRewrite、ConditionSimplification、OuterJoinElimination、JoinElimination、NestJoinElimination③SematicOptimization3.TheAlgorithmOfQueryOptimization①SingleTableScanAlgorithm②TwoTableJoinAlgorithm③Multi-tableJoinAlgorithm4.ParallelQueryOptimization5.DistributeQueryOptimizationTheQueryOptimizationTechnologyOfPostgreSQL25数据库查询优化技术:HowtoimplementtheQueryOptimizationofPostgreSQL?3HowtoimplementtheQueryOptimizationOfPostgreSQL?6数据库查询优化技术?37QueryOptimizerplannerfunctionSQLStatementParser:LexcialParse、SynataxParse、SematicCheckRewriter:QueryRewrite(ViewRewrite)LogicalQueryOptimizationPhysicalQueryOptimizationSPJOptimizationNonSPJOptimizationSetOpertionsystemtable:pg_classetcCostevaluationExecuterResultSetOperationDividedIntoSQLExecuterPlanYStatisticaldataN数据库查询优化技术()standard_planner()subquery_planner()pull_up_sublinks()standard_join_search()make_rel_from_joinlist()join_search_one_level()make_rels_by_clause_joins()pull_up_subqueries()build_join_rel()标准化表达式make_one_rel()set_base_rel_pathlist()第1层第2层第3层第4层第5层第6层第7层第8层preprocess_expression()消除外连接reduce_outer_joins()生成查询执行计划grouping_planner()query_planner()optimize_minmax_aggregates()create_plan()set_plain_rel_pathlist()add_path(rel,create_seqscan_path(root,rel,NULL));//顺序扫描方式create_index_paths(root,rel);//索引扫描方式create_tidscan_paths(root,rel);//Tid扫描方式set_cheapest(rel);//对单表所有三种扫描方式求解最优的扫描方式make_rels_by_clauseless_joins()make_join_rel()上拉子链接上拉子查询生成2个候选的最优路径对min、max聚集计划优化创建查询执行计划set_plan_references()生成新关系geqo()动态规划算法遗传算法两表连接set_cheapest(rel)访问基表的方法表连接动态规划求最优路径第9层第10层第11层://blog.163.com/li_hx/blog/static/183991413201322982935548/PostgreSQL查询优化器源码分析--整体流程9查询语句分析、优化与执行查询优化器模块动态规划算法—最优路径(Path)系统表pg_class【M1】src\backend\parser语法分析模块pg_statistic【M2】Rewitite查询重写模块【M3】Planner查询优化器【M4】Executer查询执行器器SQL语句查找对象定义等返回结果语法查询树(Query)辅助代码【M3-S1】Planner查询优化器---逻辑查询优化【M3-S2】Planner查询优化器---物理查询优化求解最优路径、生成查询执行计划SQL结果视图被替换为基表rc\backend\rewrite用关系代数的原理进行逻辑查询优化查找对象定义返回结果生成最优的查询执行计划(Plan)代价估算clausesel.c遗传算法--最优查询路径(Path)获取数据返回结果返回结果构造连接关系src\backend\optimizer\pathjoinrels.c(make_join_rel函数)代价估算路径生成逻辑优化执行器目录src\backend\executor数据库查询优化技术:HowtoimplementtheSub-QueryOptimizationofPostgreSQL?3HowtoimplementtheSubqueryOptimizationOfPostgreSQL?11数据库查询优化技术:SubqueryisanapproachthatprovidesthecapabilityofembeddingthefirstqueryintotheotherPostgreSQLincludes:3.2.1Sublink3.2.2Sub-querySublink+Sub-query==Sub-query-----------------------------------PostgreSQLDatabase3HowtoimplementtheSub-queryOptimizationOfPostgreSQL?12数据库查询优化技术数据库查询优化技术**SubLink*ASubLinkrepresentsasubselectappearinginanexpression,andinsome*casesalsothecombiningoperator(s)justaboveit.ThesubLinkType*indicatestheformoftheexpressionrepresented:*EXISTS_SUBLINKEXISTS(SELECT...)*ALL_SUBLINK(lefthand)opALL(SELECT...)*ANY_SUBLINK(lefthand)opANY(SELECT...)*ROWCOMPARE_SUBLINK(lefthand)op(SELECT...)*EXPR_SUBLINK(SELECTwithsingletargetlistitem...)*ARRAY_SUBLINKARRAY(SELECTwithsingletargetlistitem...)*CTE_SUBLINKWITHquery(neveractuallypartofanexpression)*……*/14数据库查询优化技术?3.2.115数据库查询优化技术(1)16数据库查询优化技术(2)17数据库查询优化技术