在之前的一篇月报中,我们已经简单地分析过PG的优化器(PgSQL · 源码分析 · PG优化器浅析),着重分析了SQL逻辑优化,也就是尽量对SQL进行等价或者推倒变换,以达到更有效率的执行计划。本次月报将会深入分析PG优化器原理,着重物理查询优化,包括表的扫描方式选择、多表组合方式、多表组合顺序等。

表扫描方式

表扫描方式主要包含顺序扫描、索引扫描以及Tid扫描等方式,不同的扫描方式

  • Seq scan,顺序扫描物理数据页
  1. postgres=> explain select * from t1 ;
  2. QUERY PLAN
  3. -----------------------------------------------------
  4. Seq Scan on t1 (cost=0.00..14.52 rows=952 width=8)
  • Index scan,先通过索引值获得物理数据的位置,再到物理页读取
  1. postgres=> explain select * from t1 where a1 = 10;
  2. QUERY PLAN
  3. --------------------------------------------------------------------
  4. Index Scan using t1_a1_key on t1 (cost=0.28..8.29 rows=1 width=8)
  5. Index Cond: (a1 = 10)
  • Tid scan,通过page号和item号直接定位到物理数据
  1. postgres=> explain select * from t1 where ctid='(1,10)';
  2. QUERY PLAN
  3. --------------------------------------------------
  4. Tid Scan on t1 (cost=0.00..4.01 rows=1 width=8)
  5. TID Cond: (ctid = '(1,10)'::tid)

选择度计算

  • 全表扫描选择度计算

全表扫描时每条记录都会返回,所以选择度为1,所以rows=10000

  1. EXPLAIN SELECT * FROM tenk1;
  2. QUERY PLAN
  3. -------------------------------------------------------------
  4. Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=244)
  5. SELECT relpages, reltuples FROM pg_class WHERE relname = 'tenk1';
  6. relpages | reltuples
  7. ----------+-----------
  8. 358 | 10000
  • 整型大于或者小于选择度计算
  1. EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 1000;
  2. QUERY PLAN
  3. --------------------------------------------------------------------------------
  4. Bitmap Heap Scan on tenk1 (cost=24.06..394.64 rows=1007 width=244)
  5. Recheck Cond: (unique1 < 1000)
  6. -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..23.80 rows=1007 width=0)
  7. Index Cond: (unique1 < 1000)
  8. SELECT histogram_bounds FROM pg_stats
  9. WHERE tablename='tenk1' AND attname='unique1';
  10. histogram_bounds
  11. ------------------------------------------------------
  12. {0,993,1997,3050,4040,5036,5957,7057,8029,9016,9995}
  13. selectivity = (1 + (1000 - bucket[2].min)/(bucket[2].max - bucket[2].min))/num_buckets
  14. = (1 + (1000 - 993)/(1997 - 993))/10
  15. = 0.100697
  16. rows = rel_cardinality * selectivity
  17. = 10000 * 0.100697
  18. = 1007 (rounding off)
  • 字符串等值选择度计算
  1. EXPLAIN SELECT * FROM tenk1 WHERE stringu1 = 'CRAAAA';
  2. QUERY PLAN
  3. ----------------------------------------------------------
  4. Seq Scan on tenk1 (cost=0.00..483.00 rows=30 width=244)
  5. Filter: (stringu1 = 'CRAAAA'::name)
  6. SELECT null_frac, n_distinct, most_common_vals, most_common_freqs FROM pg_stats
  7. WHERE tablename='tenk1' AND attname='stringu1';
  8. null_frac | 0
  9. n_distinct | 676
  10. most_common_vals|{EJAAAA,BBAAAA,CRAAAA,FCAAAA,FEAAAA,GSAAAA,JOAAAA,MCAAAA,NAAAAA,WGAAAA}
  11. most_common_freqs | {0.00333333,0.003,0.003,0.003,0.003,0.003,0.003,0.003,0.003,0.003}
  12. selectivity = mcf[3]
  13. = 0.003
  14. rows = 10000 * 0.003
  15. = 30

备注:如果值不在most_common_vals里面,计算公式为:

  1. selectivity = (1 - sum(mvf))/(num_distinct - num_mcv)
  • cost计算

代价模型:总代价=CPU代价+IO代价+启动代价

  1. postgres=> explain select * from t1 where a1 > 10;
  2. QUERY PLAN
  3. -----------------------------------------------------
  4. Seq Scan on t1 (cost=0.00..16.90 rows=942 width=8)
  5. Filter: (a1 > 10)
  6. (2 rows)
  7. 其中:
  8. postgres=> select relpages, reltuples from pg_class where relname = 't1';
  9. relpages | reltuples
  10. ----------+-----------
  11. 5 | 952
  12. (1 row)
  13. cpu_operator_cost=0.0025
  14. cpu_tuple_cost=0.01
  15. seq_page_cost=1
  16. random_page_cost=4

总cost = cpu_tuple_cost * 952 + seq_page_cost * 5 + cpu_operator_cost * 952 = 16.90 其他扫描方式cost计算可以参考如下函数:

  1. postgres=> select amcostestimate,amname from pg_am ;
  2. amcostestimate | amname
  3. ------------------+--------
  4. btcostestimate | btree
  5. hashcostestimate | hash
  6. gistcostestimate | gist
  7. gincostestimate | gin
  8. spgcostestimate | spgist
  9. (5 rows)

表组合方式

  • Nest Loop

screenshot.png

  1. SELECT * FROM t1 L, t2 R WHERE L.id=R.id

假设:

M = 20000 pages in L, pL = 40 rows per page, N = 400 pages in R, pR = 20 rows per page.

  1. select relpages, reltuples from pg_class where relname=‘t1

L和R进行join

  1. for l in L do
  2. for r in R do
  3. if rid == lid then ret += (r, s)

对于外表L每一个元组扫描内表R所有的元组 总IO代价: M + (pL * M) * N = 20000 + (4020000)400
\= 320020000

  • MergeJoin

screenshot.png

主要分为3步:

(1) Sort L on lid 代价MlogM

(2) Sort R on rid 代价NlogN

(3) Merge the sorted L and R on lid and rid 代价M+N

  • HashJoin

使用HashJoin的前提是其中假设一个表可以完全放在内存中,实际过程中可能统计信息有偏差,优化器认为一个表可以放到内存中,事实上数据在内存中放不下,需要使用临时文件,这样会降低性能。

screenshot.png

表的组合顺序

不同的组合顺序将会产生不同的代价,想要获得最佳的组合顺序,如果枚举所有组合顺序,那么将会有N!的排列组合,计算量对于优化器来说难以承受。PG优化器使用两种算法计算更优的组合顺序,动态规划和遗传算法。对于连接比较少的情况使用动态规划,否则使用遗传算法。

  • 动态规划求解过程

PG优化器主要考虑将执行计划树生成以下三种形式:

screenshot.png

动态规划的思想可以参考百度百科动态规划,主要将待求解问题分解成若干个子问题,先求解子问题,然后从这些子问题的解得到原问题的解。具体应用在表组合顺序上,则是先考虑单表最优访问访问,然后考虑两种组合,再考虑多表组合,最终得到更优的解。

screenshot.png