MySQL窗口函数 -MySQL 8.0 窗口函数性能优化实践:从瓶颈到高效查询 - }}@keyframes dash{to{stroke-dashoffset:0

📅 2026-11-30 👤 拼多多助力营销技巧 📂 pdd助力 👁️ 446 次阅读

一、MySQL 8.0 窗口函数性能瓶颈分析:定位慢查询根源

1.1 窗口函数执行机制与性能开销

MySQL 8.0 引入的窗口函数(如 ROW_NUMBER(), RANK(), LAG() 等)极大地简化了复杂分析查询的编写。然而,其底层执行机制决定了它可能带来显著的性能开销。窗口函数的计算通常需要先对数据进行排序(ORDER BY)和分组(PARTITION BY),这些操作在没有合适索引的情况下,会导致全表扫描和文件排序,从而成为查询的瓶颈。

1.2 常见性能问题场景

窗口函数的性能问题通常出现在以下场景:

在大数据集上使用窗口函数,且 ORDER BY 或 PARTITION BY 子句未使用索引。多个窗口函数嵌套使用,导致计算复杂度急剧增加。查询中同时包含窗口函数和关联子查询,优化器难以做出最优的执行计划。

二、核心优化策略:索引与查询改写

2.1 利用索引消除排序:ROW_NUMBER() 与 ORDER BY

对于使用 ROW_NUMBER() 等需要排序的窗口函数,最有效的优化手段是为 ORDER BY 子句创建索引。这样,MySQL 可以利用索引的有序性,避免昂贵的文件排序操作,直接获取排序后的数据。

#publish-mermaid-0-1792431602969{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;}}#publish-mermaid-0-1792431602969 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-0-1792431602969 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-0-1792431602969 .error-icon{fill:#552222;}#publish-mermaid-0-1792431602969 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-0-1792431602969 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-0-1792431602969 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-0-1792431602969 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-0-1792431602969 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-0-1792431602969 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-0-1792431602969 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-0-1792431602969 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-0-1792431602969 .marker.cross{stroke:#333333;}#publish-mermaid-0-1792431602969 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-0-1792431602969 p{margin:0;}#publish-mermaid-0-1792431602969 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-0-1792431602969 .cluster-label text{fill:#333;}#publish-mermaid-0-1792431602969 .cluster-label span{color:#333;}#publish-mermaid-0-1792431602969 .cluster-label span p{background-color:transparent;}#publish-mermaid-0-1792431602969 .label text,#publish-mermaid-0-1792431602969 span{fill:#333;color:#333;}#publish-mermaid-0-1792431602969 .node rect,#publish-mermaid-0-1792431602969 .node circle,#publish-mermaid-0-1792431602969 .node ellipse,#publish-mermaid-0-1792431602969 .node polygon,#publish-mermaid-0-1792431602969 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-0-1792431602969 .rough-node .label text,#publish-mermaid-0-1792431602969 .node .label text,#publish-mermaid-0-1792431602969 .image-shape .label,#publish-mermaid-0-1792431602969 .icon-shape .label{text-anchor:middle;}#publish-mermaid-0-1792431602969 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-0-1792431602969 .rough-node .label,#publish-mermaid-0-1792431602969 .node .label,#publish-mermaid-0-1792431602969 .image-shape .label,#publish-mermaid-0-1792431602969 .icon-shape .label{text-align:center;}#publish-mermaid-0-1792431602969 .node.clickable{cursor:pointer;}#publish-mermaid-0-1792431602969 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-0-1792431602969 .arrowheadPath{fill:#333333;}#publish-mermaid-0-1792431602969 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-0-1792431602969 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-0-1792431602969 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-0-1792431602969 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-0-1792431602969 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-0-1792431602969 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-0-1792431602969 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-0-1792431602969 .cluster text{fill:#333;}#publish-mermaid-0-1792431602969 .cluster span{color:#333;}#publish-mermaid-0-1792431602969 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;}#publish-mermaid-0-1792431602969 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-0-1792431602969 rect.text{fill:none;stroke-width:0;}#publish-mermaid-0-1792431602969 .icon-shape,#publish-mermaid-0-1792431602969 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-0-1792431602969 .icon-shape p,#publish-mermaid-0-1792431602969 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-0-1792431602969 .icon-shape .label rect,#publish-mermaid-0-1792431602969 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-0-1792431602969 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-0-1792431602969 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-0-1792431602969 .node .neo-node{stroke:#9370DB;}#publish-mermaid-0-1792431602969 [data-look="neo"].node rect,#publish-mermaid-0-1792431602969 [data-look="neo"].cluster rect,#publish-mermaid-0-1792431602969 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792431602969 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-0-1792431602969 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-0-1792431602969 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792431602969 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-0-1792431602969 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792431602969 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-0-1792431602969 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792431602969 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792431602969 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}原始数据表无索引 ORDER BY全表扫描 + 文件排序窗口函数计算慢查询结果有索引 ORDER BY索引范围扫描窗口函数计算快查询结果

该图对比了有无索引时,基于 ORDER BY 的窗口函数查询执行路径。有索引时,数据库可以直接利用索引的有序性,跳过文件排序步骤,显著提升性能。

2.2 避免全表扫描:PARTITION BY 与索引的结合

PARTITION BY 子句用于将数据分区,窗口函数在分区内独立计算。如果分区键(PARTITION BY)是高基数字段,并且该字段上有索引,MySQL 可以利用索引快速定位到相关分区,减少数据读取量。

2.3 查询拆分:将窗口函数与 JOIN 分离

当窗口函数与 JOIN 操作结合时,有时可以将复杂的查询拆分为多个步骤。例如,先通过 JOIN 获取所需数据,再对结果集应用窗口函数。这样可以避免优化器在复杂的执行计划中做出次优选择,有时甚至可以允许窗口函数部分利用前面步骤产生的临时结果集的有序性。

三、实战案例:优化前后效果对比

3.1 案例场景:分析用户行为数据

假设我们有一个 user_actions 表,记录了用户的登录时间,我们需要为每个用户按登录时间排序,并计算其每次登录的序号。

3.2 优化前:全表扫描与慢查询

优化前的查询语句如下,由于 user_id 和 login_time 上没有合适的复合索引,导致全表扫描和文件排序。

SELECT

user_id,

login_time,

ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_time) AS login_seq

FROM

user_actions;

3.3 优化后:索引与查询改写

优化后的策略是为 (user_id, login_time) 创建复合索引,并确保查询可以直接利用该索引。

-- 假设已创建索引: ALTER TABLE user_actions ADD INDEX idx_user_login(user_id, login_time);

SELECT 拼多多帮助力入口

user_id,

login_time,

ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_time) AS login_seq

FROM

user_actions;

3.4 性能提升数据可视化

优化前,查询执行时间主要消耗在排序上。优化后,由于索引的直接访问,排序开销被消除,性能得到显著提升。

#publish-mermaid-1-1792431603020{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;}}#publish-mermaid-1-1792431603020 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1-1792431603020 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1-1792431603020 .error-icon{fill:#552222;}#publish-mermaid-1-1792431603020 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1-1792431603020 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1-1792431603020 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1-1792431603020 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1-1792431603020 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1-1792431603020 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1-1792431603020 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1-1792431603020 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1-1792431603020 .marker.cross{stroke:#333333;}#publish-mermaid-1-1792431603020 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1-1792431603020 p{margin:0;}#publish-mermaid-1-1792431603020 .pieCircle{stroke:black;stroke-width:2px;opacity:0.7;}#publish-mermaid-1-1792431603020 .pieCircle.highlighted{scale:1.05;opacity:1;}#publish-mermaid-1-1792431603020 .pieCircle.highlightedOnHover:hover{transition-duration:250ms;scale:1.05;opacity:1;}#publish-mermaid-1-1792431603020 .pieOuterCircle{stroke:black;stroke-width:2px;fill:none;}#publish-mermaid-1-1792431603020 .pieTitleText{text-anchor:middle;font-size:25px;fill:black;font-family:"trebuchet ms",verdana,arial,sans-serif;}#publish-mermaid-1-1792431603020 .slice{font-family:"trebuchet ms",verdana,arial,sans-serif;fill:#333;font-size:17px;}#publish-mermaid-1-1792431603020 .legend text{fill:black;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:17px;}#publish-mermaid-1-1792431603020 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1-1792431603020 [data-look="neo"].node rect,#publish-mermaid-1-1792431603020 [data-look="neo"].cluster rect,#publish-mermaid-1-1792431603020 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792431603020 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-1-1792431603020 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1-1792431603020 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792431603020 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1-1792431603020 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792431603020 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-1-1792431603020 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792431603020 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792431603020 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}20%70%10%优化前查询时间构成全表扫描文件排序窗口计算

优化前查询的执行时间构成显示,文件排序占用了绝大部分时间,是主要的性能瓶颈。

#publish-mermaid-2-1792431603030{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;}}#publish-mermaid-2-1792431603030 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-2-1792431603030 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-2-1792431603030 .error-icon{fill:#552222;}#publish-mermaid-2-1792431603030 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-2-1792431603030 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-2-1792431603030 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-2-1792431603030 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-2-1792431603030 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-2-1792431603030 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-2-1792431603030 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-2-1792431603030 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-2-1792431603030 .marker.cross{stroke:#333333;}#publish-mermaid-2-1792431603030 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-2-1792431603030 p{margin:0;}#publish-mermaid-2-1792431603030 .pieCircle{stroke:black;stroke-width:2px;opacity:0.7;}#publish-mermaid-2-1792431603030 .pieCircle.highlighted{scale:1.05;opacity:1;}#publish-mermaid-2-1792431603030 .pieCircle.highlightedOnHover:hover{transition-duration:250ms;scale:1.05;opacity:1;}#publish-mermaid-2-1792431603030 .pieOuterCircle{stroke:black;stroke-width:2px;fill:none;}#publish-mermaid-2-1792431603030 .pieTitleText{text-anchor:middle;font-size:25px;fill:black;font-family:"trebuchet ms",verdana,arial,sans-serif;}#publish-mermaid-2-1792431603030 .slice{font-family:"trebuchet ms",verdana,arial,sans-serif;fill:#333;font-size:17px;}#publish-mermaid-2-1792431603030 .legend text{fill:black;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:17px;}#publish-mermaid-2-1792431603030 .node .neo-node{stroke:#9370DB;}#publish-mermaid-2-1792431603030 [data-look="neo"].node rect,#publish-mermaid-2-1792431603030 [data-look="neo"].cluster rect,#publish-mermaid-2-1792431603030 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792431603030 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-2-1792431603030 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-2-1792431603030 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792431603030 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-2-1792431603030 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792431603030 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-2-1792431603030 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792431603030 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792431603030 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}80%15%5%优化后查询时间构成索引范围扫描窗口计算其他

优化后,索引范围扫描成为主要操作,文件排序时间几乎消失,整体查询效率大幅提升。

四、高级优化技巧:临时表与物化视图

4.1 临时表:分解复杂窗口计算

对于极其复杂的窗口函数计算,可以将其拆解为多个步骤,并使用临时表(或 CTE)来存储中间结果。这有助于优化器更好地理解和优化每一步,有时也能利用临时表的物理存储特性。

#publish-mermaid-3-1792431603039{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;}}#publish-mermaid-3-1792431603039 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-3-1792431603039 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-3-1792431603039 .error-icon{fill:#552222;}#publish-mermaid-3-1792431603039 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-3-1792431603039 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-3-1792431603039 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-3-1792431603039 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-3-1792431603039 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-3-1792431603039 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-3-1792431603039 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-3-1792431603039 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-3-1792431603039 .marker.cross{stroke:#333333;}#publish-mermaid-3-1792431603039 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-3-1792431603039 p{margin:0;}#publish-mermaid-3-1792431603039 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-3-1792431603039 .cluster-label text{fill:#333;}#publish-mermaid-3-1792431603039 .cluster-label span{color:#333;}#publish-mermaid-3-1792431603039 .cluster-label span p{background-color:transparent;}#publish-mermaid-3-1792431603039 .label text,#publish-mermaid-3-1792431603039 span{fill:#333;color:#333;}#publish-mermaid-3-1792431603039 .node rect,#publish-mermaid-3-1792431603039 .node circle,#publish-mermaid-3-1792431603039 .node ellipse,#publish-mermaid-3-1792431603039 .node polygon,#publish-mermaid-3-1792431603039 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-3-1792431603039 .rough-node .label text,#publish-mermaid-3-1792431603039 .node .label text,#publish-mermaid-3-1792431603039 .image-shape .label,#publish-mermaid-3-1792431603039 .icon-shape .label{text-anchor:middle;}#publish-mermaid-3-1792431603039 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-3-1792431603039 .rough-node .label,#publish-mermaid-3-1792431603039 .node .label,#publish-mermaid-3-1792431603039 .image-shape .label,#publish-mermaid-3-1792431603039 .icon-shape .label{text-align:center;}#publish-mermaid-3-1792431603039 .node.clickable{cursor:pointer;}#publish-mermaid-3-1792431603039 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-3-1792431603039 .arrowheadPath{fill:#333333;}#publish-mermaid-3-1792431603039 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-3-1792431603039 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-3-1792431603039 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-3-1792431603039 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792431603039 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792431603039 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-3-1792431603039 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-3-1792431603039 .cluster text{fill:#333;}#publish-mermaid-3-1792431603039 .cluster span{color:#333;}#publish-mermaid-3-1792431603039 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;}#publish-mermaid-3-1792431603039 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-3-1792431603039 rect.text{fill:none;stroke-width:0;}#publish-mermaid-3-1792431603039 .icon-shape,#publish-mermaid-3-1792431603039 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-3-1792431603039 .icon-shape p,#publish-mermaid-3-1792431603039 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-3-1792431603039 .icon-shape .label rect,#publish-mermaid-3-1792431603039 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792431603039 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-3-1792431603039 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-3-1792431603039 .node .neo-node{stroke:#9370DB;}#publish-mermaid-3-1792431603039 [data-look="neo"].node rect,#publish-mermaid-3-1792431603039 [data-look="neo"].cluster rect,#publish-mermaid-3-1792431603039 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792431603039 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-3-1792431603039 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-3-1792431603039 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792431603039 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-3-1792431603039 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792431603039 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-3-1792431603039 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792431603039 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792431603039 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}复杂窗口查询创建临时表分步计算步骤1分步计算步骤2最终结果

该流程图展示了使用临时表分解复杂计算的过程,将一个大的计算任务拆解为多个可控的步骤。

4.2 物化视图:预计算与缓存结果

在 MySQL 8.0.19 及以上版本中,引入了物化视图(Materialized View)。物化视图可以预先计算并存储窗口函数的结果,当基表数据更新时,物化视图可以增量或全量刷新。这对于需要频繁执行的复杂分析查询,能提供近乎即时的响应。

#publish-mermaid-4-1792431603082{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;}}#publish-mermaid-4-1792431603082 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-4-1792431603082 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-4-1792431603082 .error-icon{fill:#552222;}#publish-mermaid-4-1792431603082 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-4-1792431603082 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-4-1792431603082 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-4-1792431603082 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-4-1792431603082 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-4-1792431603082 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-4-1792431603082 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-4-1792431603082 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792431603082 .marker.cross{stroke:#333333;}#publish-mermaid-4-1792431603082 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-4-1792431603082 p{margin:0;}#publish-mermaid-4-1792431603082 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-4-1792431603082 .cluster-label text{fill:#333;}#publish-mermaid-4-1792431603082 .cluster-label span{color:#333;}#publish-mermaid-4-1792431603082 .cluster-label span p{background-color:transparent;}#publish-mermaid-4-1792431603082 .label text,#publish-mermaid-4-1792431603082 span{fill:#333;color:#333;}#publish-mermaid-4-1792431603082 .node rect,#publish-mermaid-4-1792431603082 .node circle,#publish-mermaid-4-1792431603082 .node ellipse,#publish-mermaid-4-1792431603082 .node polygon,#publish-mermaid-4-1792431603082 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792431603082 .rough-node .label text,#publish-mermaid-4-1792431603082 .node .label text,#publish-mermaid-4-1792431603082 .image-shape .label,#publish-mermaid-4-1792431603082 .icon-shape .label{text-anchor:middle;}#publish-mermaid-4-1792431603082 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-4-1792431603082 .rough-node .label,#publish-mermaid-4-1792431603082 .node .label,#publish-mermaid-4-1792431603082 .image-shape .label,#publish-mermaid-4-1792431603082 .icon-shape .label{text-align:center;}#publish-mermaid-4-1792431603082 .node.clickable{cursor:pointer;}#publish-mermaid-4-1792431603082 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-4-1792431603082 .arrowheadPath{fill:#333333;}#publish-mermaid-4-1792431603082 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-4-1792431603082 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-4-1792431603082 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-4-1792431603082 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-4-1792431603082 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-4-1792431603082 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-4-1792431603082 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-4-1792431603082 .cluster text{fill:#333;}#publish-mermaid-4-1792431603082 .cluster span{color:#333;}#publish-mermaid-4-1792431603082 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;}#publish-mermaid-4-1792431603082 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-4-1792431603082 rect.text{fill:none;stroke-width:0;}#publish-mermaid-4-1792431603082 .icon-shape,#publish-mermaid-4-1792431603082 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-4-1792431603082 .icon-shape p,#publish-mermaid-4-1792431603082 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-4-1792431603082 .icon-shape .label rect,#publish-mermaid-4-1792431603082 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-4-1792431603082 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-4-1792431603082 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-4-1792431603082 .node .neo-node{stroke:#9370DB;}#publish-mermaid-4-1792431603082 [data-look="neo"].node rect,#publish-mermaid-4-1792431603082 [data-look="neo"].cluster rect,#publish-mermaid-4-1792431603082 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792431603082 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-4-1792431603082 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792431603082 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792431603082 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-4-1792431603082 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792431603082 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-4-1792431603082 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792431603082 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792431603082 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}原始数据表物化视图查询物化视图快速响应

物化视图通过预计算和缓存结果,将复杂的窗口函数计算从查询时转移到数据更新时,从而极大提升查询性能。

五、监控与调优工具:Percona Toolkit 与 Performance Schema

5.1 使用 pt-query-digest 分析慢查询

Percona Toolkit 中的 pt-query-digest 工具可以分析慢查询日志,帮助我们识别包含窗口函数的慢查询,并分析其执行计划和耗时分布。

pt-query-digest慢查询日志MySQL用户pt-query-digest慢查询日志MySQL用户#publish-mermaid-5-1792431603119{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;}}#publish-mermaid-5-1792431603119 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-5-1792431603119 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-5-1792431603119 .error-icon{fill:#552222;}#publish-mermaid-5-1792431603119 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-5-1792431603119 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-5-1792431603119 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-5-1792431603119 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-5-1792431603119 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-5-1792431603119 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-5-1792431603119 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-5-1792431603119 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-5-1792431603119 .marker.cross{stroke:#333333;}#publish-mermaid-5-1792431603119 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-5-1792431603119 p{margin:0;}#publish-mermaid-5-1792431603119 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-5-1792431603119 rect.actor.outer-path[data-look="neo"]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792431603119 rect.note[data-look="neo"]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792431603119 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-5-1792431603119 .actor-line{stroke:#9370DB;}#publish-mermaid-5-1792431603119 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-5-1792431603119 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-5-1792431603119 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-5-1792431603119 [id$="-arrowhead"] path{fill:#333;stroke:#333;}#publish-mermaid-5-1792431603119 .sequenceNumber{fill:white;}#publish-mermaid-5-1792431603119 [id$="-sequencenumber"]{fill:#333;}#publish-mermaid-5-1792431603119 [id$="-crosshead"] path{fill:#333;stroke:#333;}#publish-mermaid-5-1792431603119 .messageText{fill:#333;stroke:none;}#publish-mermaid-5-1792431603119 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-5-1792431603119 .labelText,#publish-mermaid-5-1792431603119 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-5-1792431603119 .loopText,#publish-mermaid-5-1792431603119 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-5-1792431603119 .sectionTitle,#publish-mermaid-5-1792431603119 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-5-1792431603119 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-5-1792431603119 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-5-1792431603119 .noteText,#publish-mermaid-5-1792431603119 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-5-1792431603119 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-5-1792431603119 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-5-1792431603119 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-5-1792431603119 .actorPopupMenu{position:absolute;}#publish-mermaid-5-1792431603119 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#publish-mermaid-5-1792431603119 .actor-man circle,#publish-mermaid-5-1792431603119 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-5-1792431603119 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-5-1792431603119 .node .neo-node{stroke:#9370DB;}#publish-mermaid-5-1792431603119 [data-look="neo"].node rect,#publish-mermaid-5-1792431603119 [data-look="neo"].cluster rect,#publish-mermaid-5-1792431603119 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792431603119 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-5-1792431603119 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-5-1792431603119 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792431603119 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-5-1792431603119 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792431603119 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-5-1792431603119 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792431603119 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792431603119 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}执行慢查询记录查询分析慢查询日志生成分析报告

该时序图展示了使用 pt-query-digest 分析慢查询日志的流程,帮助我们定位性能问题。

5.2 Performance Schema 监控窗口函数执行

MySQL 的 Performance Schema 提供了详细的执行统计信息。通过查询 events_statements_summary_by_digest 等表,可以监控特定窗口函数查询的执行时间、扫描行数等指标,为调优提供数据支持。

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧