欢迎光临
我们一直在努力

3.4.3 索引下推

索引条件下推(Index Condition Pushdown,ICP) 是 MySQL 5.6 引入的一项关键优化技术。它专门针对使用二级索引但无法完全精确定位的查询,通过将部分 WHERE 条件“下压”到存储引擎层,在索引扫描阶段就过滤掉不满足条件的记录,从而大幅减少不必要的回表操作。ICP 是高性能索引策略中的重要一环,尤其与联合索引和最左前缀原则配合时,能让“部分失效”的索引仍发挥巨大作用。


🔍 一、ICP 要解决什么问题?

设想一个典型的场景:联合索引 idx_name_age (name, age),执行以下查询:

SELECT * FROM users WHERE name LIKE \’张%\’ AND age = 20;

  • 根据最左前缀原则,name LIKE \’张%\’ 是范围条件,索引可以快速定位到姓“张”的记录,并沿着叶子链表顺序扫描。
  • 但 age = 20 是范围列 name 之后的等值条件,因为范围条件会“截断”后续列的索引排序,所以 age 无法用于缩小扫描范围。
  • 无 ICP 时:MySQL 会扫描所有 name LIKE \’张%\’ 的索引条目,逐条回表,取出整行后再判断 age = 20。如果姓“张”的有1000行,但年龄为20的只有10行,那么990次回表都是徒劳的随机 I/O。
  • 有 ICP 时:存储引擎在扫描索引叶子节点时,直接检查 age 列的值是否等于20,因为 age 本身就在索引叶子节点中(存有 (name, age, 主键))。不满足条件的记录根本不会回表,只会对那10行执行回表操作。

ICP 的核心思想就是 “能先在索引里判断的,绝不拖到回表后判断”。


⚙️ 二、ICP 的工作原理

下图对比了有无 ICP 的执行流程差异:

#mermaid-svg-RCAaFiIb3ki43wV3{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-RCAaFiIb3ki43wV3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-RCAaFiIb3ki43wV3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-RCAaFiIb3ki43wV3 .error-icon{fill:#552222;}#mermaid-svg-RCAaFiIb3ki43wV3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RCAaFiIb3ki43wV3 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-RCAaFiIb3ki43wV3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RCAaFiIb3ki43wV3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RCAaFiIb3ki43wV3 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-RCAaFiIb3ki43wV3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RCAaFiIb3ki43wV3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RCAaFiIb3ki43wV3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RCAaFiIb3ki43wV3 .marker.cross{stroke:#333333;}#mermaid-svg-RCAaFiIb3ki43wV3 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RCAaFiIb3ki43wV3 p{margin:0;}#mermaid-svg-RCAaFiIb3ki43wV3 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-RCAaFiIb3ki43wV3 .cluster-label text{fill:#333;}#mermaid-svg-RCAaFiIb3ki43wV3 .cluster-label span{color:#333;}#mermaid-svg-RCAaFiIb3ki43wV3 .cluster-label span p{background-color:transparent;}#mermaid-svg-RCAaFiIb3ki43wV3 .label text,#mermaid-svg-RCAaFiIb3ki43wV3 span{fill:#333;color:#333;}#mermaid-svg-RCAaFiIb3ki43wV3 .node rect,#mermaid-svg-RCAaFiIb3ki43wV3 .node circle,#mermaid-svg-RCAaFiIb3ki43wV3 .node ellipse,#mermaid-svg-RCAaFiIb3ki43wV3 .node polygon,#mermaid-svg-RCAaFiIb3ki43wV3 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RCAaFiIb3ki43wV3 .rough-node .label text,#mermaid-svg-RCAaFiIb3ki43wV3 .node .label text,#mermaid-svg-RCAaFiIb3ki43wV3 .image-shape .label,#mermaid-svg-RCAaFiIb3ki43wV3 .icon-shape .label{text-anchor:middle;}#mermaid-svg-RCAaFiIb3ki43wV3 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-RCAaFiIb3ki43wV3 .rough-node .label,#mermaid-svg-RCAaFiIb3ki43wV3 .node .label,#mermaid-svg-RCAaFiIb3ki43wV3 .image-shape .label,#mermaid-svg-RCAaFiIb3ki43wV3 .icon-shape .label{text-align:center;}#mermaid-svg-RCAaFiIb3ki43wV3 .node.clickable{cursor:pointer;}#mermaid-svg-RCAaFiIb3ki43wV3 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-RCAaFiIb3ki43wV3 .arrowheadPath{fill:#333333;}#mermaid-svg-RCAaFiIb3ki43wV3 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RCAaFiIb3ki43wV3 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RCAaFiIb3ki43wV3 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RCAaFiIb3ki43wV3 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-RCAaFiIb3ki43wV3 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RCAaFiIb3ki43wV3 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-RCAaFiIb3ki43wV3 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RCAaFiIb3ki43wV3 .cluster text{fill:#333;}#mermaid-svg-RCAaFiIb3ki43wV3 .cluster span{color:#333;}#mermaid-svg-RCAaFiIb3ki43wV3 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RCAaFiIb3ki43wV3 .flowchartTitleText{text-anchor:middl

赞(0)
未经允许不得转载:171主机测评 » 3.4.3 索引下推
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址