dbcc_DBCC FREEPROCCACHE命令介绍和概述

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)
AS
     SELECT *
     FROM Sales.Customers AS s
          LEFT OUTER JOIN Sales.CustomerCategories AS sc ON s.CustomerCategoryID = sc.CustomerCategoryID
          LEFT OUTER JOIN [Application].People AS pp ON s.PrimaryContactPersonID = pp.PersonID
     WHERE 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_plansys.dm_exec_sql_textsys.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.

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

check procedural cache and execution plan

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.

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

DBCC command output

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.

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

removed the specific execution plan from the cache

示例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错误日志。

SQL Server Error logs

示例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;

Using DBCC FREEPROCCACHE to clear a specific resource pool

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值