如何track存储过程的编译次数

本文通过实验深入探讨了SQL Server中存储过程的重编译机制,并详细解析了plan_generation_num字段的变化规律,揭示了存储过程在何种情况下会发生重编译。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自此处

有个script我们很熟悉,是用来去查找当前SQL Server中哪些存储过程变重编译的次数最多的:

 

--Gives you the top 25 stored procedures that have been recompiled.

 

select top 25 sql_text.text, sql_handle, plan_generation_num,  execution_count,

    dbid,  objectid

into DMV_Top25_Recompile_Commands

from sys.dm_exec_query_stats a

    cross apply sys.dm_exec_sql_text(sql_handle) as sql_text

where plan_generation_num >1

order by plan_generation_num desc

go

 

那么,这个脚本究竟是记录什么情况下的存储过程recomile呢?

 

我们在SQL Server上创建一个这样的store procedure

 

create proc aaa

as

select plan_generation_num,* FROM DMV_Top25_Recompile_Commands where plan_generation_num  > 2

 

然后准备好用这个脚本来返回plan_generation_num的值

select top 25 sql_text.text, sql_handle, plan_generation_num,  execution_count,

    dbid,  objectid

from sys.dm_exec_query_stats a

    cross apply sys.dm_exec_sql_text(sql_handle) as sql_text

where  sql_text.text like '%aaa%'

order by plan_generation_num desc

 

 

Exec aaa之后的脚本返回结果:

 

这里的第六行结果集就是我们的存储过程aaa。这时的plan_generation_num值显示为1.

 

接下来我们mark recompile

sp_recompile aaa

然后再次执行 exec aaa

 

使用脚本查询:

 

 

这里看到存储过程重编译以后,plan_generation_num的值并没有增加。

那为什么我们还会使用这样的脚本来返回重编译次数很多的存储过程呢?

 

接下来我们再次将存储过程mark recompile,然后直接使用脚本查询:

这时,我们发现该存储过程的plan text已经从DMV中移除了。看起来sp_recompile会直接将cache中缓存的执行计划和语句直接标识成不可用。因此DMV中就没有相关的记录了。

这就是说,存储过程标识重编译这种模式导致的重编译,从DMV里面是没有办法跟踪的。

 

那么从性能监视器的计数器 sp recompilation/sec”里面能不能跟踪到呢?

我们反复执行:

sp_recompile aaa

exec aaa

 

性能监视器中一直显示为0

 

那么plan_generation_num的值究竟是什么含义呢?BOL中的解释很简单:

A sequence number that can be used to distinguish between instances of plans after a recompile.

中文版的含义为:可用于在重新编译后区分不同计划实例的序列号。

 

这里并没有说明如何去计算的序列号。我们从另一篇英文的blog中找到了更加详细的说明:

There are a lot of interesting columns in P and S, especially in S, and here I will only discuss what I have learned about plan_generation_num in S. SQL Server 2005 treats the compiled plan for a stored procedure as an array of subplans, one for each query statement. If an individual subplan needs recompilation, it does so without causing the whole plan to recompile. In doing so, SQL Server increments the plan_generation_num on the subplan record to be 1 + MAX(plan_generation_num for all subplans). The general distribution of plan_generation_num among all subplans for a given plan is such that it has multiple of 1's and distinct numbers > 1. That is because all subplans start with 1 as their plan_generation_num. Appendix A is the query for learning plan_generation_num.

 

http://lfsean.blogspot.com/2008/02/understanding-sql-plangenerationnum.html

 

这部分说明简单的来说,就是只要存储过程中有一条语句发生重编译,这个plan_generation_num值就会+1.这里并没有说是整个存储过程重编译的时候,这个值会+1.

 

接下来我们修改测试存储过程aaa

 

Alter TABLE aaa_table(

[text] [nvarchar](max) NULL,

[sql_handle] [varbinary](64) NOT NULL,

[plan_generation_num] [bigint] NOT NULL,

[execution_count] [bigint] NOT NULL,

[dbid] [smallint] NULL,

[objectid] [int] NULL

) ON [PRIMARY]

 

insert into aaa_table select * from DMV_Top25_Recompile_Commands where 1=2

insert into aaa_table select * from DMV_Top25_Recompile_Commands where 1=2

insert into aaa_table select * from DMV_Top25_Recompile_Commands where 1=2

insert into aaa_table select * from DMV_Top25_Recompile_Commands where 1=2

insert into aaa_table select * from DMV_Top25_Recompile_Commands where 1=2

 

 

然后我们执行存储过程,收集profiler trace,同时继续监控性能监视器

开始重新执行存储过程aaa

 

这里我们可以看到sp recompilation/sec立刻变成了7

Profiler trace中可以看到每条insert语句上都触发了一个sp:recompile

 

 

脚本的查询结果:

 

可以看到plan_generation_num的值增加到6了。

 

为什么这样写存储过程会导致重编译?http://support.microsoft.com/kb/243586 这篇文章中列举了多种会导致存储过程重编译的情况:

aaa这个存储过程符合这个条件:

The procedure interleaves Data Definition Language (DDL) and Data Manipulation Language (DML) operations.

 

因此我们的结论是,使用这个脚本去查询重编译次数高的存储过程是没有错的,但是这个脚本并不包含由于sp_recompile已经定义存储过程时使用了with recompile的选项而导致的存储过程重编译的情况。


### 关于内核编译时出现的 `track` 报错 在 Linux 内核编译过程中遇到 `track` 报错可能涉及多种原因,包括但不限于工具链不匹配、源码版本问题或配置文件错误。以下是针对该问题的具体分析和解决方案: #### 已知信息 当前运行的内核版本为 `5.15.0-52-generic`[^1]。此版本属于较新的稳定分支之一,在编译新内核时需要注意兼容性和依赖项。 --- #### 可能的原因及解决方法 ##### 1. **工具链版本不匹配** 如果使用的 GCC 或其他构建工具版本与目标内核版本不兼容,则可能导致类似的报错。建议验证并更新开发环境中的工具链至推荐版本。 验证命令如下: ```bash gcc --version make --version ``` 若发现版本过旧或存在冲突,请重新安装适合的目标工具链。例如: ```bash sudo apt update && sudo apt install build-essential linux-source ``` 此外,某些情况下需要显式指定交叉编译器路径以避免默认工具链干扰[^2]。 --- ##### 2. **内核配置文件损坏或缺失** 编译前需确保 `.config` 文件正确无误。可以通过以下方式恢复默认配置: ```bash make defconfig ``` 或者基于现有内核生成配置文件: ```bash cp /boot/config-$(uname -r) ./.config make olddefconfig ``` 上述操作可有效减少因配置不当引发的错误。 --- ##### 3. **特定模块未加载或丢失** 当尝试启用某些功能(如跟踪支持)而对应模块不存在时也会触发此类异常。确认是否遗漏必要组件,比如调试框架相关选项。 修改 Kconfig 设置时注意勾选关联条目,例如: ```plaintext CONFIG_KALLSYMS=y CONFIG_DEBUG_INFO=y ``` 同时清理残留数据后再重试: ```bash make clean make mrproper ``` --- ##### 4. **代码逻辑缺陷** 假设问题是由于上游提交引入的新 bug 导致,则考虑回退到更稳定的标签发布版而非最新主线 HEAD 提交点。克隆官方仓库后切换合适分支即可规避潜在风险: ```bash git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git cd linux git checkout v5.15.x ``` 对应调整依据实际需求灵活选取[^3]。 --- #### 总结 综合以上几点排查方向逐一试验直至定位根本诱因为止。通常通过升级基础软件包以及修正初始化参数能够快速缓解大部分常见状况;而对于复杂场景则有必要深入研究具体日志提示进一步细化处理策略。 ```python # 示例脚本用于自动化部分流程检测 import os os.system('gcc --version') os.system('make --version') if not os.path.exists('.config'): print("Config file missing! Generating default...") os.system('make defconfig') else: print("Using existing config.") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值