dbcc

This article gives an overview of the SQL Server DBCC FREEPROCCACHE command and its usage with different examples.

本文通过不同的示例概述了SQL Server DBCC FREEPROCCACHE命令及其用法。

To learn more about DBCC commands in SQL Server, I would recommend going through this in-depth article Concept and basics of DBCC Commands in SQL Server

若要了解有关SQL Server中DBCC命令的更多信息,建议您阅读这篇深入的文章,以了解SQL Server中DBCC命令的概念和基础。

执行计划和过程缓存简介 (Introduction to Execution plan and Procedural cache)

Once we execute a query, SQL Server prepares the optimized execution plan and stores that execution plan into the plan cache. Next time, if the same query is run, SQL Server uses the same execution plan instead of generating a new one. It is a good thing to reuse an existing execution plan because SQL Server does not need to do full optimization for the query. SQL Server might create another execution plan if query optimizer thinks that it can improve query performance. I have seen the scenarios, in which the new execution plan starts causing performance issues such as high CPU and memory utilization. You want to get rid of that particular execution plan so that query optimizer can prepare a new execution plan.

一旦执行查询,SQL Server将准备优化的执行计划并将该执行计划存储到计划缓存中。 下次,如果运行相同的查询,SQL Server将使用相同的执行计划,而不是生成新的执行计划。 重用现有的执行计划是一件好事,因为SQL Server不需要对查询进行完全优化。 如果查询优化器认为SQL Server可以提高查询性能,则SQL Server可能会创建另一个执行计划。 我已经看到了一些场景,其中新的执行计划开始引起性能问题,例如CPU和内存利用率高。 您想要摆脱那个特定的执行计划,以便查询优化器可以准备一个新的执行计划。

You might think of restarting SQL Services, but it might be a costly affair. You require application downtime. In the production instance, it might be challenging to get downtime and that too for removing a specific execution plan from the cache.

您可能会考虑重新启动SQL Services,但这可能是一件昂贵的事情。 您需要停机。 在生产实例中,获得停机时间和从缓存中删除特定的执行计划也可能具有挑战性。

We might want to clear the entire cache to resolve performance issues. Below are a few examples.

我们可能希望清除整个缓存以解决性能问题。 以下是一些示例。

  • Due to long-running queries, your server might face memory pressure 由于长期运行的查询,您的服务器可能面临内存压力
  • In the case of high recompilations 在高重新编译的情况下
  • A large number of ad-hoc query workloads 大量的临时查询工作负载
  • Dynamic t-SQL 动态t-SQL

Let’s explore to clear buffer cache without taking the restart of SQL Services.

让我们探索清除缓冲区缓存而无需重新启动SQL Services。

DBCC FREEPROCCACHE命令概述 (Overview of DBCC FREEPROCCACHE command)

We can use the DBCC FREEPROCCACHE command to clear the procedural cache in SQL Server. We might drop a single execution plan or all plans from the buffer cache. SQL Server needs to create new execution plans once the user reruns the query.

我们可以使用DBCC FREEPROCCACHE命令清除SQL Server中的过程缓存。 我们可能会从缓冲区缓存中删除一个执行计划或所有计划。 用户重新运行查询后,SQL Server需要创建新的执行计划。

Let’s use the demo to create a stored procedure and view the execution plan in the cache.

让我们使用该演示创建一个存储过程并查看缓存中的执行计划。

USE WideWorldImporters;
GO
CREATE PROCEDURE [usp_GetCustomerInfo] @CustomerName NVARCHAR(100)
ASSELECT *FROM Sales.Customers AS sLEFT OUTER JOIN Sales.CustomerCategories AS sc ON s.CustomerCategoryID = sc.CustomerCategoryIDLEFT OUTER JOIN [Application].People AS pp ON s.PrimaryContactPersonID = pp.PersonIDWHERE CustomerName = @CustomerName;

SQL Server do not create the execution plan during execution plan creation. The execution plan gets created during first execution of the stored procedure.

SQL Server在执行计划创建期间不会创建执行计划。 在执行存储过程期间会创建执行计划。

示例1:使用DBCC FREEPROCCACHE清除特定的执行计划 (Example 1: Using DBCC FREEPROCCACHE to clear a specific execution plan)

Let’s run the stored procedure with different parameters.

让我们使用不同的参数运行存储过程。

Exec [usp_GetCustomerInfo] @CustomerName='Abel Spirlea'
Exec [usp_GetCustomerInfo] @CustomerName='Shah Alizadeh'

Now, we will use the dynamic management views sys.dm_exec_query_plan, sys.dm_exec_sql_text and sys.dm_exec_query_stats to get the execution statistics, query plan , last execution time.

现在,我们将使用动态管理视图 sys.dm_exec_query_plan , sys.dm_exec_sql_text和sys.dm_exec_query_stats来获取执行统计信息,查询计划和最后执行时间。

SELECT
[qs].[last_execution_time],
[qs].[execution_count],
[qs].[total_logical_reads]/[qs].[execution_count] [AvgLogicalReads],
[qs].[max_logical_reads],
[qs].[plan_handle],
[p].[query_plan]
FROM sys.dm_exec_query_stats [qs]
CROSS APPLY sys.dm_exec_sql_text([qs].sql_handle) [t]
CROSS APPLY sys.dm_exec_query_plan([qs].[plan_handle]) [p]
WHERE [t].text LIKE '%usp_GetCustomerInfo%';
GO

In the output, we can see two different execution plans for this stored procedure. We get the plan handle as well for both the execution plans.

在输出中,我们可以看到此存储过程的两个不同的执行计划。 我们也获得了两个执行计划的计划句柄。

You can click on the hyperlink in the ‘query_plan’ column to check the execution plan of this stored procedure in both the graphical and XML format.

您可以单击“ query_plan”列中的超链接,以图形和XML格式检查此存储过程的执行计划。

Suppose we diagnosed the performance issue in this stored procedure due to this change in the execution plan and we want to remove a specific plan from the cache.

假设由于执行计划中的这一更改,我们诊断出此存储过程中的性能问题,并且我们想从缓存中删除特定计划。

We can drop a single execution plan using the following DBCC FREEPROCACHE command. Specify the plan handle of the procedure that we want to remove.

我们可以使用以下DBCC FREEPROCACHE命令删除单个执行计划。 指定我们要删除的过程的计划句柄。

DBCC FREEPROCCACHE (plan_handle_id_)

DBCC免费程序(plan_handle_id_)

Here plan handle uniquely identifies a query execution plan in the plan cache.

此处的计划句柄唯一地标识计划缓存中的查询执行计划。

We can also specify the SQL handle of the batch in the DBCC FREEPROCACHE command.

我们还可以在DBCC FREEPROCACHE命令中指定批处理SQL句柄。

Let’s execute this query for the plan handle from my example.

让我们对我的示例中的计划句柄执行此查询。

DBCC FREEPROCCACHE (0x05000900996DB224D002DAFF3802000001000000000000000000000000000000000000000000000000000000)

Execute this command with the plan cache, and you get the following informational message.

使用计划缓存执行此命令,您将收到以下参考消息。

The output is a generic DBCC execution message. If we do not want this message, we can run the DBCC FREEPROCCACHE command with ‘WITH NO_INFOMSGS’ clause. It suppresses the information message as an output of this query.

输出是通用DBCC执行消息。 如果我们不希望看到此消息,可以使用'WITH NO_INFOMSGS'子句运行DBCC FREEPROCCACHE命令。 它禁止信息消息作为此查询的输出。

DBCC FREEPROCCACHE(0x060009007F4272304099DAFF3802000001000000000000000000000000000000000000000000000000000000) WITH NO_INFOMSGS;

It removes the specific execution plan from the cache. You can validate this by re-running the earlier query.

它从缓存中删除特定的执行计划。 您可以通过重新运行先前的查询来验证这一点。

示例2:使用DBCC FREEPROCCACHE清除所有执行计划缓存 (Example 2: Using DBCC FREEPROCCACHE to clear all execution plan cache)

Suppose we do not want to remove a specific plan handle from the cache. Instead, we want to clear all plans cached for the stored procedures. In this case, we do not need to pass the plan handle. We can run the following command.

假设我们不想从缓存中删除特定的计划句柄。 相反,我们要清除所有为存储过程缓存的计划。 在这种情况下,我们不需要传递计划句柄。 我们可以运行以下命令。

DBCC FREEPROCCACHE

Or

要么

DBCC FREEPROCCACHE WITH NO_INFOMSGS;

It also logs an entry in the SQL Server error logs. Go to the Management folder of the SQL instance and view the current SQL Server error logs.

它还在SQL Server错误日志中记录一个条目。 转到SQL实例的“管理”文件夹,然后查看当前SQL Server错误日志。

示例3:使用DBCC FREEPROCCACHE清除特定的资源池 (Example 3: Using DBCC FREEPROCCACHE to clear a specific resource pool)

We can clear the cache at the resource pool level as well. First, check the resource pool cache and used memory using the DMV sys.dm_resource_governor_resource_pools.

我们也可以在资源池级别清除缓存。 首先,使用DMV sys.dm_resource_governor_resource_pools检查资源池缓存和已用内存。

SELECT name AS 'Pool Name',
cache_memory_kb/1024.0 AS [cache_memory_MB],
used_memory_kb/1024.0 AS [used_memory_MB]
FROM sys.dm_resource_governor_resource_pools;

Let’s say we want to clear the procedural cache for the internal resource pool. Specify resource pool name with the DBCC FREEPROCCACHE command.

假设我们要清除内部资源池的过程缓存。 使用DBCC FREEPROCCACHE命令指定资源池名称。

DBCC FREEPROCCACHE ('internal');

最大并行度和DBCC FREEPROCCACHE命令 (Maximum Degree of Parallelism and DBCC FREEPROCCACHE command)

Usually, experienced DBA with performance troubleshooting skills, modify the SQL Server max degree of parallelism (MAXDOP) setting to control the number of processors in the parallel query execution plan. The default value of MAXDOP is 0, and it allows SQL Server to use all available CPU for the parallel execution plan.

通常,经验丰富的具有性能疑难解答技能的DBA会修改SQL Server最大并行度(MAXDOP)设置,以控制并行查询执行计划中的处理器数量。 MAXDOP的默认值为0,它允许SQL Server将所有可用的CPU用于并行执行计划。

This article does not cover the detailed information of MAXDOP; you can refer to the article Max Degree of Parallelism in SQL Server.

本文不介绍MAXDOP的详细信息; 您可以参考SQL Server中的最大并行度文章。

Let’s say we modify the max degree of parallelism value to 6 for our workload. We do it using the following SQL query.

假设我们将工作负载的最大并行度值修改为6。 我们使用以下SQL查询来实现。

EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
EXEC sp_configure 'max degree of parallelism', 6;
GO
RECONFIGURE WITH OVERRIDE;
GO

Once we have modified the max degree of parallelism value, SQL Server invalidates all stored procedure plan cache. It behaves similar to a DBCC FREEPROCCACHE command.

修改最大并行度值后,SQL Server将使所有存储过程计划缓存无效。 它的行为类似于DBCC FREEPROCCACHE命令。

Alternatively, we can use the following methods to resolve performance issues and clear the plan cache.

另外,我们可以使用以下方法解决性能问题并清除计划缓存。

  • sp_recompile to recompile a stored procedure. SQL Server also requires generating a new execution plan upon the next execution of the code sp_recompile重新编译存储过程。 SQL Server还需要在下次执行代码时生成新的执行计划
  • In SQL Server 2016 onwards, we can use database scoped configuration option to clear the procedural cache in a specific database. Execute the following query under database context to clear the database-specific procedural cache

    在SQL Server 2016及更高版本中,我们可以使用数据库范围的配置选项来清除特定数据库中的过程缓存。 在数据库上下文中执行以下查询以清除特定于数据库的过程高速缓存

    ALTER SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE
    

DBCC FREEPROCCACHE命令的快速摘要 (Quick Summary of DBCC FREEPROCCACHE command)

Below is the quick summary of what we learned about DBCC FREEPROCCACHE command in this article:

以下是本文中我们从DBCC FREEPROCCACHE命令中学到的内容的快速摘要:

  • It clears out the plan cache in SQL Server for a specific plan hash or all plan as per the parameters used 它根据使用的参数清除SQL Server中针对特定计划哈希或所有计划的计划缓存
  • SQL Server forces to generate a new execution plan of each stored procedure on the next execution SQL Server强制在下次执行时为每个存储过程生成一个新的执行计划
  • It might increase the utilization of system processes such as CPU, memory 它可能会提高系统进程(例如CPU,内存)的利用率
  • It might be good for the development environment, but try to use caution in executing this command in the production environment 这可能对开发环境有好处,但是在生产环境中执行此命令时请谨慎使用
  • You should clear the cache only when it is crucial to do so 仅在至关重要时才应清除缓存
  • We can use an alternative method sp_recompile to generate a new plan for the stored procedure 我们可以使用替代方法sp_recompile为存储过程生成新计划
  • It is a better approach than restarting SQL Server instance to clear the cache however we should not do it frequently 这是比重新启动SQL Server实例以清除缓存更好的方法,但是我们不应该经常这样做

结论 (Conclusion)

In this article, we explored the DBCC FREEPROCCACHE command to clear the procedural cache along with the consequences of it. You should know this command and use it only in case of any urgent requirements.

在本文中,我们探讨了DBCC FREEPROCCACHE命令来清除过程缓存及其后果。 您应该知道此命令,并且仅在有紧急需求时使用。

翻译自: https://www.sqlshack.com/overview-of-sql-server-dbcc-freeproccache-command/

dbcc

dbcc_DBCC FREEPROCCACHE命令介绍和概述相关推荐

  1. 医学数字影像和通讯(DICOM)---DICOM标准3.0 第一部分:介绍和概述

    前言 ACR(美国放射学会)和NAMA(国家电子制造商协会)组成了一个联合委员会来开发一个医学数字成像和通讯的标准----DICOM.这个标准按照NEMA的程序制定. 这个标准通过与其他标准化组织的交 ...

  2. linux df命令各项表示什么意思,linux命令介绍:df使用介绍

    linux中df命令参数功能:检查文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 语法:df [选项] 说明:linux中df命令可显示所有文件系统对 ...

  3. 第五课:系统目录及ls·文件类型及alias命令介绍

    1.上过一次我们学习了单用户和救援模式及服务器秘钥登录等操作,而我们最终的目的还是要操作和使用linux系统,所以我们今天先初步学习linux的基本命令如下: 一·目录介绍        ls命令介绍 ...

  4. puppet子命令介绍

    puppet子命令介绍 # puppet -V                //查看puppet的版本 # puppet help              //查看puppet支持的一些子命令 # ...

  5. linux系统防火墙相关问题及常用命令介绍

    今天介绍关于linux系统防火墙:centos5.centos6.redhat6系统自带的是iptables防火墙,centos7.redhat7自带firewall防火墙,ubuntu系统使用的是u ...

  6. linux vim编辑文本是 m,linux基础命令介绍四:文本编辑 vim

    本文介绍vim(版本7.4)的一般用法 vim是功能强大的文本编辑器,是vi的增强版. vim [options] [file ..] 使用vim编辑一个文件的最常用命令就是: vim file 其中 ...

  7. 常见网络命令介绍及使用

    常见网络命令介绍及使用 ping 介绍 参数说明 使用例子 ipconfig 介绍 参数说明 使用例子 tracert 介绍 参数说明 使用例子 netstat 介绍 参数说明 使用例子 route ...

  8. linux route 刷新_linux基础命令介绍十五:推陈出新

    本文介绍ip.ss.journalctl和firewall-cmd,它们旨在代替linux中原有的一些命令或服务. 1.ip ip [OPTIONS] OBJECT COMMAND ip是iprout ...

  9. 查看MySQL数据库表的命令介绍

    如果需要查看MySQL数据库中都有哪些MySQL数据库表,应该如何实现呢?下面就为您介绍查看MySQL数据库表的命令,供您参考. 进入MySQL Command line client下 查看当前使用 ...

最新文章

  1. 移动互联网下半场 iOS 程序员面试真经,让你进入 BAT 不再是梦
  2. 【超详细】模拟器EVE的安装与使用,附下载链接
  3. openlayer 3 在layer上添加feature
  4. zookeeper的名词复盘-版本-保证分布式数据原子性
  5. 向模态窗体传递参数和获取返回值
  6. python 日志不会按照日期分割_python 按照日期切分大日志文件(重点)和按照指定大小切分日志文件...
  7. 京东极速版如何取消订单 京东极速版怎样取消订单
  8. angular linux 打包不成功_Angular Library 系列之 构建和打包
  9. Linux学习笔记13
  10. 第二章 身份验证——《跟我学Shiro》[张开涛]
  11. 从阿里外包月薪5K到转岗正式员工月薪15K,这100多天的心酸只有自己知道...
  12. EfficientNet理论讲解
  13. ctab提取dna流程图_ctab 提取dna配方
  14. pixi的使用之创建和操作精灵
  15. 新手做独立站需要掌握哪些技能
  16. 研究生语音识别课程作业记录(二) 非特定人孤立词识别
  17. androidentity什么用_Android ORM 框架:GreenDao 使用详解(进阶篇)
  18. Windows下误删EFI分区重建引导简单教程
  19. 建设商城网站需要注意事项_建设商城网站流程_OctShop
  20. IDC评述网:2013年12月全国IDC品牌排行榜

热门文章

  1. 这是一篇很好的文章,学verilog的可以好好看看
  2. LucasExlucas
  3. 和DOM一起的日子:检测与预防DOM跨站脚本攻击
  4. jqGrid时间转换
  5. 【转】聊聊HTTPS和SSL/TLS协议
  6. 六款值得推荐的android(安卓)开源框架简介
  7. IEEE 1588 校时
  8. java中list、set和map 的区别(转)
  9. [EGORefreshTableHeaderView]手动启动下拉更新的方法
  10. 游戏地图制作---Tiled使用教程