全部产品

执行计划管理

更新时间:2020-08-17 09:01:41

背景信息

对于每一条SQL,优化器都会生成相应执行计划。但是很多情况下,应用请求的SQL都是重复的(仅参数不同),参数化之后的SQL完全相同。这时,我们可以按照参数化之后的SQL构造一个缓存,将除了参数以外的各种信息(比如执行计划)缓存起来,我们称之为执行计划缓存(Plan Cache)。

另一方面,对于较复杂的查询(例如涉及到多个表的Join),我们希望它的执行计划能保持相对稳定,不因为版本升级等原因发生变化。执行计划管理(Plan Management)允许我们为每个SQL记录一组执行计划,该执行计划会被持久化地保存,即使版本升级也会保留。

工作流程概览

当PolarDB-X收到一条查询SQL时,会经历以下流程:

  1. 对查询SQL进行参数化处理,将所有参数替换为占位符(?)。
  2. 以参数化的SQL作为Key,查找执行计划缓存中是否有缓存;如果没有,则调用优化器进行优化。
  3. 如果该SQL是简单查询,则直接执行,跳过执行计划管理相关步骤
  4. 如果该SQL是复杂查询,则使用基线(Baseline)中固化的执行计划;如果有多个,则选择代价最低的那个。
    x

执行计划缓存

PolarDB-X默认开启执行计划缓存功能。EXPLAIN结果中的HitCache一项就表示当前SQL是否命中执行计划缓存。

开启执行计划缓存后,PolarDB-X会对SQL做参数化处理,参数化会将SQL中的常量用占位符?替换,并构建出相应的参数列表。在执行计划中也可以看到LogicalView算子的SQL中含有?
x

执行计划管理

对于复杂SQL,经过执行计划缓存之后,还会经过执行计划管理流程。

执行计划缓存和执行计划管理都是采用参数化后的SQL作为Key来执行计划。执行计划缓存中会缓存所有SQL的执行计划,而执行计划管理仅对复杂查询SQL进行处理。

SQL模版和最优的执行计划并非一一对应的,很大程度上,这会受到具体参数的影响。可能在某一组参数下,执行计划A是最优的,而另一组参数下,执行计划B更优。

在执行计划管理中,每一条SQL对应一个基线,每个基线中包含一个或多个执行计划。实际使用中,会根据当时的参数选择其中代价最小的执行计划来执行。

计划选择

当执行计划缓存中的执行计划走进执行计划管理时,SPM会操作一个流程判断该执行计划是否是已知的,是已知的话,是否代价是最小的;不是已知的话,是否需要执行一下以判断该执行计划的优化程度。
x

运维指令

PolarDB-X提供了丰富的指令集用于管理执行计划,语法如下:

  1. BASELINE (LOAD|PERSIST|CLEAR|VALIDATE|LIST|DELETE) [Signed Integer,Signed Integer....]
  2. BASELINE (ADD|FIX) SQL (HINT Select Statemtnt)
  • BASELINE (ADD|FIX) SQL <HINT> <Select Statement>:将SQL以HINT修复过后的执行计划记录固定下来。
  • BASELINE LOAD:将系统表中指定的基线信息刷新到内存并使其生效。
  • BASELINE LOAD_PLAN:将系统表中指定的执行计划信息刷新到内存并使其生效。
  • BASELINE LIST:列出当前所有的基线信息。
  • BASELINE PERSIST:将指定的基线落盘。
  • BASELINE PERSIST_PLAN:将指定的执行计划落盘。
  • BASELINE CLEAR:内存中清理某个基线。
  • BASELINE CLEAR_PLAN:内存中清理某个执行计划。
  • BASELINE DELETE:磁盘中删除某个基线。
  • BASELINE DELETE_PLAN:磁盘中删除某个执行计划。

执行计划调优实战

数据发生变化或PolarDB-X优化器引擎升级后,针对同一条SQL,有可能会出现更好的执行计划。SPM在自动演化时会将CBO优化自动发现的更优执行计划加入到SQL的基线中。除此以外,您也可以通过SPM的指令主动优化执行计划。

例如以下的SQL:

  1. SELECT *
  2. FROM lineitem JOIN part ON l_partkey=p_partkey
  3. WHERE p_name LIKE '%green%';

正常EXPLAIN发现该SQL生成的执行计划使用的是Hash Join,并且在Baseline List的基线中,该SQL仅有这一个执行计划:

  1. mysql> explain select * from lineitem join part on l_partkey=p_partkey where p_name like '%geen%';
  2. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | LOGICAL PLAN |
  4. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | Gather(parallel=true) |
  6. | ParallelHashJoin(condition="l_partkey = p_partkey", type="inner") |
  7. | LogicalView(tables="[00-03].lineitem", shardCount=4, sql="SELECT `l_orderkey`, `l_partkey`, `l_suppkey`, `l_linenumber`, `l_quantity`, `l_extendedprice`, `l_discount`, `l_tax`, `l_returnflag`, `l_linestatus`, `l_shipdate`, `l_commitdate`, `l_receiptdate`, `l_shipinstruct`, `l_shipmode`, `l_comment` FROM `lineitem` AS `lineitem`", parallel=true) |
  8. | LogicalView(tables="[00-03].part", shardCount=4, sql="SELECT `p_partkey`, `p_name`, `p_mfgr`, `p_brand`, `p_type`, `p_size`, `p_container`, `p_retailprice`, `p_comment` FROM `part` AS `part` WHERE (`p_name` LIKE ?)", parallel=true) |
  9. | HitCache:true |
  10. | |
  11. | |
  12. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  13. 7 rows in set (0.06 sec)
  14. mysql> baseline list;
  15. +-------------+--------------------------------------------------------------------------------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+
  16. | BASELINE_ID | PARAMETERIZED_SQL | PLAN_ID | EXTERNALIZED_PLAN | FIXED | ACCEPTED |
  17. +-------------+--------------------------------------------------------------------------------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+
  18. | -399023558 | SELECT *
  19. FROM lineitem
  20. JOIN part ON l_partkey = p_partkey
  21. WHERE p_name LIKE ? | -935671684 |
  22. Gather(parallel=true)
  23. ParallelHashJoin(condition="l_partkey = p_partkey", type="inner")
  24. LogicalView(tables="[00-03].lineitem", shardCount=4, sql="SELECT `l_orderkey`, `l_partkey`, `l_suppkey`, `l_linenumber`, `l_quantity`, `l_extendedprice`, `l_discount`, `l_tax`, `l_returnflag`, `l_linestatus`, `l_shipdate`, `l_commitdate`, `l_receiptdate`, `l_shipinstruct`, `l_shipmode`, `l_comment` FROM `lineitem` AS `lineitem`", parallel=true)
  25. LogicalView(tables="[00-03].part", shardCount=4, sql="SELECT `p_partkey`, `p_name`, `p_mfgr`, `p_brand`, `p_type`, `p_size`, `p_container`, `p_retailprice`, `p_comment` FROM `part` AS `part` WHERE (`p_name` LIKE ?)", parallel=true)
  26. | 0 | 1 |
  27. +-------------+--------------------------------------------------------------------------------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+
  28. 1 row in set (0.02 sec)

假如这个SQL在某些条件下采用BKA Join(Lookup Join)会有更好的性能,那么首先需要想办法利用HINT引导PolarDB-X生成符合预期的执行计划。BKA Join的HINT格式为:

  1. /*+TDDL:BKA_JOIN(lineitem, part)*/

通过EXPLAIN [HINT] [SQL]观察出来的执行计划是否符合预期:

  1. mysql> explain /*+TDDL:bka_join(lineitem, part)*/ select * from lineitem join part on l_partkey=p_partkey where p_name like '%geen%';
  2. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | LOGICAL PLAN |
  4. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | Gather(parallel=true) |
  6. | ParallelBKAJoin(condition="l_partkey = p_partkey", type="inner") |
  7. | LogicalView(tables="[00-03].lineitem", shardCount=4, sql="SELECT `l_orderkey`, `l_partkey`, `l_suppkey`, `l_linenumber`, `l_quantity`, `l_extendedprice`, `l_discount`, `l_tax`, `l_returnflag`, `l_linestatus`, `l_shipdate`, `l_commitdate`, `l_receiptdate`, `l_shipinstruct`, `l_shipmode`, `l_comment` FROM `lineitem` AS `lineitem`", parallel=true) |
  8. | Gather(concurrent=true) |
  9. | LogicalView(tables="[00-03].part", shardCount=4, sql="SELECT `p_partkey`, `p_name`, `p_mfgr`, `p_brand`, `p_type`, `p_size`, `p_container`, `p_retailprice`, `p_comment` FROM `part` AS `part` WHERE (`p_name` LIKE ?)") |
  10. | HitCache:false |
  11. | |
  12. | |
  13. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. 8 rows in set (0.14 sec)

注意此时由于Hint的干预,Join的算法已修正为BKA Join。但是这并不会对基线造成变动,如果想以后每次遇到这条SQL都使用上面的计划,还需要将其加入到基线中。

可以采用执行计划管理的Baseline Add指令为该SQL增加一个执行计划。这样就会同时有两套执行计划存在于该SQL的基线中,CBO优化器会根据代价选择一个执行计划执行。

  1. mysql> baseline add sql /*+TDDL:bka_join(lineitem, part)*/ select * from lineitem join part on l_partkey=p_partkey where p_name like '%geen%';
  2. +-------------+--------+
  3. | BASELINE_ID | STATUS |
  4. +-------------+--------+
  5. | -399023558 | OK |
  6. +-------------+--------+
  7. 1 row in set (0.09 sec)
  8. mysql> baseline list;
  9. +-------------+--------------------------------------------------------------------------------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+
  10. | BASELINE_ID | PARAMETERIZED_SQL | PLAN_ID | EXTERNALIZED_PLAN | FIXED | ACCEPTED |
  11. +-------------+--------------------------------------------------------------------------------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+
  12. | -399023558 | SELECT *
  13. FROM lineitem
  14. JOIN part ON l_partkey = p_partkey
  15. WHERE p_name LIKE ? | -1024543942 |
  16. Gather(parallel=true)
  17. ParallelBKAJoin(condition="l_partkey = p_partkey", type="inner")
  18. LogicalView(tables="[00-03].lineitem", shardCount=4, sql="SELECT `l_orderkey`, `l_partkey`, `l_suppkey`, `l_linenumber`, `l_quantity`, `l_extendedprice`, `l_discount`, `l_tax`, `l_returnflag`, `l_linestatus`, `l_shipdate`, `l_commitdate`, `l_receiptdate`, `l_shipinstruct`, `l_shipmode`, `l_comment` FROM `lineitem` AS `lineitem`", parallel=true)
  19. Gather(concurrent=true)
  20. LogicalView(tables="[00-03].part", shardCount=4, sql="SELECT `p_partkey`, `p_name`, `p_mfgr`, `p_brand`, `p_type`, `p_size`, `p_container`, `p_retailprice`, `p_comment` FROM `part` AS `part` WHERE (`p_name` LIKE ?)")
  21. | 0 | 1 |
  22. | -399023558 | SELECT *
  23. FROM lineitem
  24. JOIN part ON l_partkey = p_partkey
  25. WHERE p_name LIKE ? | -935671684 |
  26. Gather(parallel=true)
  27. ParallelHashJoin(condition="l_partkey = p_partkey", type="inner")
  28. LogicalView(tables="[00-03].lineitem", shardCount=4, sql="SELECT `l_orderkey`, `l_partkey`, `l_suppkey`, `l_linenumber`, `l_quantity`, `l_extendedprice`, `l_discount`, `l_tax`, `l_returnflag`, `l_linestatus`, `l_shipdate`, `l_commitdate`, `l_receiptdate`, `l_shipinstruct`, `l_shipmode`, `l_comment` FROM `lineitem` AS `lineitem`", parallel=true)
  29. LogicalView(tables="[00-03].part", shardCount=4, sql="SELECT `p_partkey`, `p_name`, `p_mfgr`, `p_brand`, `p_type`, `p_size`, `p_container`, `p_retailprice`, `p_comment` FROM `part` AS `part` WHERE (`p_name` LIKE ?)", parallel=true)
  30. | 0 | 1 |
  31. +-------------+--------------------------------------------------------------------------------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+
  32. 2 rows in set (0.03 sec)

通过以上Baseline List指令展示出来的结果,可以看到基于BKA_JOIN的执行计划已增加到该 SQL的基线中。此时EXPLAIN这条SQL,发现随SQL中p_name LIKE ? 条件变化,PolarDB-X会选择不同的执行计划。

如果想让PolarDB-X固定使用上述的执行计划(而非在两个中挑选一个),可以采用Baseline Fix指令强制PolarDB-X走指定的执行计划。

  1. mysql> baseline fix sql /*+TDDL:bka_join(lineitem, part)*/ select * from lineitem join part on l_partkey=p_partkey where p_name like '%geen%';
  2. +-------------+--------+
  3. | BASELINE_ID | STATUS |
  4. +-------------+--------+
  5. | -399023558 | OK |
  6. +-------------+--------+
  7. 1 row in set (0.07 sec)
  8. mysql> baseline list\G
  9. *************************** 1. row ***************************
  10. BASELINE_ID: -399023558
  11. PARAMETERIZED_SQL: SELECT *
  12. FROM lineitem
  13. JOIN part ON l_partkey = p_partkey
  14. WHERE p_name LIKE ?
  15. PLAN_ID: -1024543942
  16. EXTERNALIZED_PLAN:
  17. Gather(parallel=true)
  18. ParallelBKAJoin(condition="l_partkey = p_partkey", type="inner")
  19. LogicalView(tables="[00-03].lineitem", shardCount=4, sql="SELECT `l_orderkey`, `l_partkey`, `l_suppkey`, `l_linenumber`, `l_quantity`, `l_extendedprice`, `l_discount`, `l_tax`, `l_returnflag`, `l_linestatus`, `l_shipdate`, `l_commitdate`, `l_receiptdate`, `l_shipinstruct`, `l_shipmode`, `l_comment` FROM `lineitem` AS `lineitem`", parallel=true)
  20. Gather(concurrent=true)
  21. LogicalView(tables="[00-03].part", shardCount=4, sql="SELECT `p_partkey`, `p_name`, `p_mfgr`, `p_brand`, `p_type`, `p_size`, `p_container`, `p_retailprice`, `p_comment` FROM `part` AS `part` WHERE (`p_name` LIKE ?)")
  22. FIXED: 1
  23. ACCEPTED: 1
  24. *************************** 2. row ***************************
  25. BASELINE_ID: -399023558
  26. PARAMETERIZED_SQL: SELECT *
  27. FROM lineitem
  28. JOIN part ON l_partkey = p_partkey
  29. WHERE p_name LIKE ?
  30. PLAN_ID: -935671684
  31. EXTERNALIZED_PLAN:
  32. Gather(parallel=true)
  33. ParallelHashJoin(condition="l_partkey = p_partkey", type="inner")
  34. LogicalView(tables="[00-03].lineitem", shardCount=4, sql="SELECT `l_orderkey`, `l_partkey`, `l_suppkey`, `l_linenumber`, `l_quantity`, `l_extendedprice`, `l_discount`, `l_tax`, `l_returnflag`, `l_linestatus`, `l_shipdate`, `l_commitdate`, `l_receiptdate`, `l_shipinstruct`, `l_shipmode`, `l_comment` FROM `lineitem` AS `lineitem`", parallel=true)
  35. LogicalView(tables="[00-03].part", shardCount=4, sql="SELECT `p_partkey`, `p_name`, `p_mfgr`, `p_brand`, `p_type`, `p_size`, `p_container`, `p_retailprice`, `p_comment` FROM `part` AS `part` WHERE (`p_name` LIKE ?)", parallel=true)
  36. FIXED: 0
  37. ACCEPTED: 1
  38. 2 rows in set (0.01 sec)

Baseline Fix指令执行完后,可以看到BKA Join执行计划的Fix状态位已被置为1。此时就算不加HINT,任意条件下Explain这条SQL,都一定会采用这个执行计划。

  1. mysql> explain select * from lineitem join part on l_partkey=p_partkey where p_name like '%green%';
  2. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | LOGICAL PLAN |
  4. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | Gather(parallel=true) |
  6. | ParallelBKAJoin(condition="l_partkey = p_partkey", type="inner") |
  7. | LogicalView(tables="[00-03].lineitem", shardCount=4, sql="SELECT `l_orderkey`, `l_partkey`, `l_suppkey`, `l_linenumber`, `l_quantity`, `l_extendedprice`, `l_discount`, `l_tax`, `l_returnflag`, `l_linestatus`, `l_shipdate`, `l_commitdate`, `l_receiptdate`, `l_shipinstruct`, `l_shipmode`, `l_comment` FROM `lineitem` AS `lineitem`", parallel=true) |
  8. | Gather(concurrent=true) |
  9. | LogicalView(tables="[00-03].part", shardCount=4, sql="SELECT `p_partkey`, `p_name`, `p_mfgr`, `p_brand`, `p_type`, `p_size`, `p_container`, `p_retailprice`, `p_comment` FROM `part` AS `part` WHERE (`p_name` LIKE ?)") |
  10. | HitCache:true |
  11. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. 8 rows in set (0.01 sec)