[TOC] # 执行计划缓存 执行`EXPLAIN`语句看到的执行计划是预估的,在 Plan Cache 中缓存的执行计划才是真实的。Plan Cache 可以避免硬解析 SQL 语句,当同样的 SQL 请求到 OceanBase 数据库时,会从 Cache 中获得语句的已分析版本以加速语句执行。 ## 与 Plan Cache 相关的配置项 <table data-tag="table" id="table-bxp-6mj-jpm" class="table"><colgroup width="360" span="1" data-tag="col" id="col-pfy-tgw-xy0" colwidth="1*" colnum="1" colname="col1" style="width:33.33333333333333%" class="col"></colgroup><colgroup width="360" span="1" data-tag="col" id="col-msu-n9p-gvb" colwidth="2*" colnum="2" colname="col2" style="width:66.66666666666666%" class="col"></colgroup><thead id="thead-aq4-x1s-cs2" class="thead"><tr id="tr-g4g-1aq-7xi"><th id="td-n3o-gce-fsi"><p id="p-d9f-shi-wuy">配置项</p></th><th id="td-azv-tkm-ine"><p id="p-e7h-29f-ggd">说明</p></th></tr></thead><tbody data-tag="tbody" id="tbody-8ma-sm9-c50" class="tbody"><tr data-tag="tr" id="tr-ib0-lor-ydd" class="tr"><td data-tag="td" id="td-xfw-yfm-8ar" class="td"><p id="p-ps9-s2r-bq0">plan_cache_evict_interval</p></td><td data-tag="td" id="td-jdo-84r-lxp" class="td"><p id="p-acm-quz-iyl">该配置项用于设置检查执行计划是否需要淘汰的间隔时间,默认值为 30s。</p></td></tr></tbody></table> ## 与 Plan Cache 相关的系统变量 <table data-tag="table" id="table-rat-7n3-h8g" class="table"><colgroup width="360" span="1" data-tag="col" id="col-feg-vpa-6bp" colwidth="1*" colnum="1" colname="col1" style="width:33.33333333333333%" class="col"></colgroup><colgroup width="360" span="1" data-tag="col" id="col-939-do8-xsx" colwidth="2*" colnum="2" colname="col2" style="width:66.66666666666666%" class="col"></colgroup><thead id="thead-9yw-f9z-3e0" class="thead"><tr id="tr-6xm-zqb-1h7"><th id="td-lme-bt1-adf"><p id="p-g76-35g-i5x">系统变量</p></th><th id="td-r3w-gi6-v5m"><p id="p-uad-tmg-17b">说明</p></th></tr></thead><tbody data-tag="tbody" id="tbody-b4m-x87-z7i" class="tbody"><tr data-tag="tr" id="tr-3c5-117-5hg" class="tr"><td data-tag="td" id="td-ts9-46j-n3k" class="td"><p id="p-znw-46n-8x7">ob_plan_cache_percentage</p></td><td data-tag="td" id="td-srf-hr1-23h" class="td"><p id="p-kwa-3o2-pi7">该系统变量用于设置计划缓存可使用内存占租户内存的百分比。计划缓存最多可使用内存(内存上限绝对值)= 租户内存上限 * ob_plan_cache_percentage/100,默认值为 5。</p></td></tr><tr data-tag="tr" id="tr-jwm-chj-8s5" class="tr"><td data-tag="td" id="td-pf6-uoi-304" class="td"><p id="p-i99-vqe-mch">ob_plan_cache_evict_high_percentage</p></td><td data-tag="td" id="td-9mz-nus-g71" class="td"><p data-tag="p" id="p-p7g-8a5-dnj" class="p">该系统变量用于设置触发计划缓存淘汰的内存大小在内存上限绝对值的百分比。触发计划缓存淘汰的内存大小(淘汰计划的高水位线) = 内存上限绝对值 * ob_plan_cache_evict_high_percentage/100,默认值为 90。</p></td></tr><tr data-tag="tr" id="tr-fe6-4lr-cxr" class="tr"><td data-tag="td" id="td-a71-scg-fou" class="td"><p id="p-8pw-cse-vn9">ob_plan_cache_evict_low_percentage</p></td><td data-tag="td" id="td-dtr-zw4-hcx" class="td"><p id="p-8jr-m6l-8hy">该系统变量设置停止淘汰计划的内存。停止淘汰计划的内存(淘汰计划的低水位线) =内存上限绝对值 * ob_plan_cache_evict_low_percentage/100,默认值为 50。</p></td></tr></tbody></table> 举例来讲,假如一个租户内存大小为 10 G,`ob_plan_cache_percentage`的值为 10,`ob_plan_cache_evict_high_percentage`的值为 90,`ob_plan_cache_evict_low_percentage`的值为 50。则: * 计划缓存内存上限绝对值 = 10 G \* 10 / 100 = 1 G * 淘汰计划的高水位线 = 1 G \* 90 / 100 = 0.9 G * 淘汰计划的低水位线 = 1 G \* 50 / 100 = 0.5 G 当该租户在某个 OBServer 上的计划缓存使用超过 0.9 G 时,会触发淘汰,且优先淘汰最久未执行的计划。当淘汰到使用内存只有 0.5 G 时,则停止淘汰。如果淘汰速度没有新计划生成的速度快,则当计划缓存使用内存达到内存上限绝对值 1 G 时,将不再往计划缓存中添加新计划,直到淘汰后使用的内存小于 1 G 时才会再次添加新计划到计划缓存中。