您好,登錄后才能下訂單哦!
小編給大家分享一下PostgreSQL中查詢優(yōu)化的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
下面是PG源碼目錄(/src/backend/optimizer)中的README文件對優(yōu)化器相關(guān)函數(shù)和數(shù)據(jù)結(jié)構(gòu)的總體說明:
Optimizer Functions ------------------- The primary entry point is planner(). planner()//優(yōu)化器主入口函數(shù) set up for recursive handling of subqueries//為子查詢配置處理器(遞歸方式) -subquery_planner()//調(diào)用(子)查詢優(yōu)化函數(shù) pull up sublinks and subqueries from rangetable, if possible//可以的話,上拉子鏈接和子查詢 canonicalize qual//表達(dá)式規(guī)范化 Attempt to simplify WHERE clause to the most useful form; this includes flattening nested AND/ORs and detecting clauses that are duplicated in different branches of an OR.//簡化WHERE語句 simplify constant expressions//簡化常量表達(dá)式 process sublinks//處理子鏈接 convert Vars of outer query levels into Params//轉(zhuǎn)換外查詢的Vars變量到Params中 --grouping_planner()// preprocess target list for non-SELECT queries//預(yù)處理非SELECT語句的投影列 handle UNION/INTERSECT/EXCEPT, GROUP BY, HAVING, aggregates,//處理集合操作/聚集函數(shù)/排序等 ORDER BY, DISTINCT, LIMIT --query_planner()// make list of base relations used in query//構(gòu)造查詢中的基表鏈表 split up the qual into restrictions (a=1) and joins (b=c)//拆分表達(dá)式為限制條件和連接 find qual clauses that enable merge and hash joins//查找可以讓Merge和Hash連接生效的表達(dá)式 ----make_one_rel()// set_base_rel_pathlists()//設(shè)置基表路徑鏈表 find seqscan and all index paths for each base relation//遍歷每個基表,尋找順序掃描和所有可能的索引掃描路徑 find selectivity of columns used in joins//查找連接中使用的列的選擇性 make_rel_from_joinlist()//通過join鏈表構(gòu)造Relation hand off join subproblems to a plugin, GEQO, or standard_join_search()// -----standard_join_search()//標(biāo)準(zhǔn)的連接搜索函數(shù) call join_search_one_level() for each level of join tree needed//每一個join tree調(diào)用join_search_one_level join_search_one_level(): For each joinrel of the prior level, do make_rels_by_clause_joins()//對于上一層的每一個joinrel,執(zhí)行make_rels_by_clause_joins if it has join clauses, or make_rels_by_clauseless_joins() if not. Also generate "bushy plan" joins between joinrels of lower levels. Back at standard_join_search(), generate gather paths if needed for//回到standard_join_search函數(shù),需要的話,收集相關(guān)的路徑并應(yīng)用set_cheapest函數(shù)獲取代價最小的路徑 each newly constructed joinrel, then apply set_cheapest() to extract the cheapest path for it. Loop back if this was not the top join level.//如果不是最頂層連接,循環(huán) Back at grouping_planner://回到grouping_planner函數(shù) do grouping (GROUP BY) and aggregation//處理分組和聚集 do window functions//處理窗口函數(shù) make unique (DISTINCT)//處理唯一性 do sorting (ORDER BY)//處理排序 do limit (LIMIT/OFFSET)//處理Limit Back at planner()://回到planner函數(shù) convert finished Path tree into a Plan tree//轉(zhuǎn)換最終的路徑樹到計劃樹 do final cleanup after planning//收尾工作 Optimizer Data Structures ------------------------- PlannerGlobal - global information for a single planner invocation//全局優(yōu)化信息 PlannerInfo - information for planning a particular Query (we make//某個Planner的優(yōu)化信息 a separate PlannerInfo node for each sub-Query) RelOptInfo - a relation or joined relations//某個Relation(包括連接)的優(yōu)化信息 RestrictInfo - WHERE clauses, like "x = 3" or "y = z"//限制條件 (note the same structure is used for restriction and join clauses) Path - every way to generate a RelOptInfo(sequential,index,joins)//構(gòu)造該關(guān)系(注意:中間結(jié)果也是關(guān)系的一種)的路徑 SeqScan - represents a sequential scan plan IndexPath - index scan BitmapHeapPath - top of a bitmapped index scan TidPath - scan by CTID SubqueryScanPath - scan a subquery-in-FROM ForeignPath - scan a foreign table, foreign join or foreign upper-relation CustomPath - for custom scan providers AppendPath - append multiple subpaths together MergeAppendPath - merge multiple subpaths, preserving their common sort order ResultPath - a childless Result plan node (used for FROM-less SELECT) MaterialPath - a Material plan node UniquePath - remove duplicate rows (either by hashing or sorting) GatherPath - collect the results of parallel workers GatherMergePath - collect parallel results, preserving their common sort order ProjectionPath - a Result plan node with child (used for projection) ProjectSetPath - a ProjectSet plan node applied to some sub-path SortPath - a Sort plan node applied to some sub-path GroupPath - a Group plan node applied to some sub-path UpperUniquePath - a Unique plan node applied to some sub-path AggPath - an Agg plan node applied to some sub-path GroupingSetsPath - an Agg plan node used to implement GROUPING SETS MinMaxAggPath - a Result plan node with subplans performing MIN/MAX WindowAggPath - a WindowAgg plan node applied to some sub-path SetOpPath - a SetOp plan node applied to some sub-path RecursiveUnionPath - a RecursiveUnion plan node applied to two sub-paths LockRowsPath - a LockRows plan node applied to some sub-path ModifyTablePath - a ModifyTable plan node applied to some sub-path(s) LimitPath - a Limit plan node applied to some sub-path NestPath - nested-loop joins MergePath - merge joins HashPath - hash joins EquivalenceClass - a data structure representing a set of values known equal//等價類 PathKey - a data structure representing the sort ordering of a path//排序鍵
看完了這篇文章,相信你對“PostgreSQL中查詢優(yōu)化的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。