获取SQL Server数据库增长和收缩事件的详细信息

It is essential for the DBA to need to ensure the SQL Server database performance. Performance tuning is an open-ended task, and you need to ensure the monitoring of various database parameters.

对于DBA而言,确保SQL Server数据库性能至关重要。 性能调优是一项开放式任务,您需要确保监视各种数据库参数。

SQL Server中的默认跟踪概述 (Overview of Default trace in SQL Server)

Starting from SQL Server 2005, SQL Server captures a few critical events in the default traces. The default trace is enabled on each SQL Server instance. It is a lightweight trace consisting of five trace files in the installation directory of SQL Server.

从SQL Server 2005开始,SQL Server会在默认跟踪中捕获一些关键事件。 在每个SQL Server实例上启用默认跟踪。 它是一个轻量级跟踪,由SQL Server安装目录中的五个跟踪文件组成。

We can check the default trace configuration using the following query.

我们可以使用以下查询检查默认跟踪配置。

SELECT* FROM sys.configurations WHERE configuration_id = 1568

Default trace configuration for SQL Server database

If the default trace is not running, we can use sp_configure commands to enable it using the following query.

如果默认跟踪未运行,则可以使用sp_configure命令通过以下查询启用它。

sp_configure 'show advanced options', 1;
GO
RECONFIGURE; 
GO
sp_configure 'default trace enabled', 1;
GO
RECONFIGURE;
GO

In the default trace, SQL Server calculates following auto grow and shrink events for the SQL Server database.

在默认跟踪中,SQL Server为SQL Server数据库计算以下自动增长和收缩事件。

  • Data file auto grow

    数据文件自动增长
  • Data file auto shrink

    数据文件自动缩小
  • Log file auto grow

    日志文件自动增长
  • Log file auto shrink

    日志文件自动缩小

Once we create a SQL Server database, we define auto growth for each data and log file.

创建SQL Server数据库后,我们将为每个数据和日志文件定义自动增长。

SQL Server expands the size of a database (data and log file) based on the auto growth setting of an individual file to avoid space issues in the existing transactions. Once an auto-growth event occurs, SQL Server holds up transaction processing for the time being. We should monitor and manage the auto growth setting for a database. If the auto-growth event takes a little longer time, it might influence database performance and availability.

SQL Server根据单个文件的自动增长设置扩展数据库(数据和日志文件)的大小,以避免现有事务中出现空间问题。 一旦发生自动增长事件,SQL Server暂时将暂缓事务处理。 我们应该监视和管理数据库的自动增长设置。 如果自动增长事件花费更长的时间,则可能会影响数据库的性能和可用性。

In case of any auto-growth event, SQL Server looks for additional space in the disk where the data or log file exists. If we have a very frequent auto-growth event, it causes physical fragmentation as well. The physical fragmented database takes a long time to complete the transactions. We should take action to minimize the auto-growth events for highly transactional databases.

如果发生任何自动增长事件,SQL Server会在磁盘中寻找数据或日志文件的其他空间。 如果我们有一个非常频繁的自动增长事件,它也会导致物理碎片。 物理碎片数据库需要很长时间才能完成事务。 我们应该采取措施使高事务性数据库的自动增长事件最小化。

By default, Auto Growth is set to grow 10% percent of existing file size in the SQL Server database. It might be suitable for small databases, however, if you look at the perspective of a large database file, it is not the right configuration.

默认情况下,“自动增长”设置为在SQL Server数据库中增长现有文件大小的10%。 它可能适用于小型数据库,但是,如果从大型数据库文件的角度来看,它不是正确的配置。

Database Properties

Let’s look at the following examples. In example 1, SQL Server needs to expand the data file by 100 GB while in example 2, SQL Server expands data file by 10 GB. It will take a long time to expand the data file by 100 GB, and it might put additional load on the database as well.

让我们看下面的例子。 在示例1中,SQL Server需要将数据文件扩展100 GB,而在示例2中,SQL Server将数据文件扩展10 GB。 将数据文件扩展到100 GB将花费很长时间,并且可能还会给数据库增加额外的负载。

Example 1

Example 2

Database File Size – 1 TB

Database File Size – 100 GB

Auto Growth – By 10%

Auto Growth – By 10%

Auto Growth File size increment: 100 GB

Auto Growth File size increment: 10 GB

例子1

例子2

数据库文件大小– 1 TB

数据库文件大小– 100 GB

汽车增长–增长10%

汽车增长–增长10%

自动增长文件大小增加:100 GB

自动增长文件大小增加:10 GB

We should take the following approach to avoid auto-growth events.

我们应该采取以下方法来避免自动增长事件。

  • Proactively grow data and log file size for the SQL Server database. If database files have sufficient free space, it will not cause any auto growth

    主动增加SQL Server数据库的数据和日志文件大小。 如果数据库文件具有足够的可用空间,则不会导致任何自动增长
  • Monitor auto growth events and set appropriate size for the auto growth in fixed MB instead of percentage (%) growth

    监视自动增长事件,并为自动增长设置适当的大小,以固定MB为单位,而不是百分比(%)增长

Suppose we set the data file auto growth to 512 MB instead of a 10% setting. In the following table, we can see that auto growth does not require any dependency on the data file size if we use fixed MB growth.

假设我们将数据文件自动增长设置为512 MB,而不是10%的设置。 在下表中,我们可以看到,如果使用固定的MB增长,则自动增长不需要对数据文件大小的任何依赖。

Example 1

Example 2

Database File Size – 1 TB

Database File Size – 100 GB

Auto Growth – By 512 MB

Auto Growth – By 512 MB

Auto Growth File size increment: 512 MB

Auto Growth File size increment: 512 MB

例子1

例子2

数据库文件大小– 1 TB

数据库文件大小– 100 GB

自动增长– 512 MB

自动增长– 512 MB

自动增长文件大小增加:512 MB

自动增长文件大小增加:512 MB

SQL Server数据库缩小 (SQL Server database Shrink)

We might come across a situation where the database files have sufficient free space, but the disk does not meet the free space threshold (assume we maintain a 20% free disk space threshold). Many times, DBA executes DBCC SHRINKDATABASE command to shrink database and return free space to the disk. We should not shrink a database unless it is very much required to do so.

我们可能会遇到这样一种情况,即数据库文件具有足够的可用空间,但是磁盘未达到可用空间阈值(假设我们维持20%的可用磁盘空间阈值)。 很多时候,DBA执行DBCC SHRINKDATABASE命令来收缩数据库并将可用空间返回到磁盘。 除非非常需要收缩数据库,否则我们不应该对其进行收缩。

Some of the issues you might face due to shrinking databases are as follows:

由于数据库缩小,您可能会遇到的一些问题如下:

  • Shrink operation is slow and takes a long time to complete, especially for large database files

    收缩操作很慢并且需要很长时间才能完成,特别是对于大型数据库文件
  • It might cause blocking during its execution time

    它可能在执行期间导致阻塞
  • It brings index fragmentation, and you need to do index maintenance tasks

    它带来了索引碎片,您需要执行索引维护任务
  • Due to index fragmentation, database performance will be slow

    由于索引碎片,数据库性能会变慢

检查SQL Server数据库的自动增长和收缩事件的不同方法 (Different ways to check Auto Growth and Shrink events for SQL Server database)

At this point, you should be aware that it is vital for a DBA to monitor the auto growth and database shrink events. It is especially vital for the high transactional database. In this section, we will identify different ways to get details of these events.

此时,您应该意识到,DBA监视自动增长和数据库收缩事件至关重要。 这对于高事务性数据库尤其重要。 在本节中,我们将确定获取这些事件详细信息的不同方法。

SSMS磁盘使用情况报告 (SSMS Disk Usage Report)

Connect to a SQL instance and right-click on a database for which we want to get details of Auto Growth and Shrink Events.

连接到SQL实例,然后右键单击我们要获取其“自动增长和收缩事件”详细信息的数据库。

Go to Reports -> Standard Reports and Disk Usage

转到报告->标准报告和磁盘使用情况

Standard reports for SQL Server Database

It opens the disk usage report of the specified database. In this disk usage report, we get the details of the data file and log file space usage.

它会打开指定数据库的磁盘使用情况报告。 在此磁盘使用情况报告中,我们获得了数据文件和日志文件空间使用情况的详细信息。

In the following screenshot, we get a message – No entry found for autogrow/auto shrink event for SQLShackDemo database in the trace log.

在以下屏幕截图中,我们收到一条消息– 在跟踪日志中找不到SQLShackDemo数据库的自动增长/自动收缩事件条目

It shows no auto growth and auto shrink event occurred in the trace log for our SQL Server databases. You should note that as per the message it indicates that only auto events are captured, but default trace captures events that occurred due to executing shrink command.

它显示我们SQL Server数据库的跟踪日志中没有发生自动增长和自动收缩事件。 您应该注意,根据消息,它指示仅捕获自动事件,但是默认跟踪捕获由于执行收缩命令而发生的事件。

Disk usage report in SSMS

Let’s shrink the transaction log file of SQLShackDemo database using the following DBCC SHRINKFILE command.

让我们使用以下DBCC SHRINKFILE命令缩小SQLShackDemo数据库的事务日志文件。

USE [SQLShackDemo]
GO
DBCC SHRINKFILE (N'SQLShackDemo_log' , 0, TRUNCATEONLY)
GO

Rerun the disk usage report, and it shows the event for this. You may notice that it shows the event name Log File Auto Growth.

重新运行磁盘使用情况报告,并为此显示事件。 您可能会注意到它显示了事件名称“日志文件自动增长”。

Disk usage report in SSMS

Let’s execute a workload on SQLShackDemo database, and it should cause data and log file growth.

让我们在SQLShackDemo数据库上执行工作负载,它会导致数据和日志文件增长。

CREATE TABLE Temp
(ID   INT, 
 Name CHAR(8000)
);
GO
SET NOCOUNT ON;
GO
DECLARE @i INT;
SET @i = 1;
WHILE @i < 10000
    BEGIN
        INSERT INTO Temp
        VALUES
        (1, 
         'Rajendra'
        );
        SET @i = @i + 1;
    END;

Once query execution is completed, rerun the disk usage report. In the following screenshot, you can see data and log file auto-growth events.

查询执行完成后,请重新运行磁盘使用情况报告。 在以下屏幕截图中,您可以看到数据和日志文件自动增长事件。

You need to notice and monitor the frequency of auto-growth along with the duration (ms) for the SQL Server database. It gives you the necessary data to correlated performance issues with auto events and time is taken in completing the request. If the duration is more even for a small increment, you should also consult your storage team for disk-related performance issues.

您需要注意并监视SQL Server数据库自动增长的频率以及持续时间(ms)。 它为您提供了必要的数据,以将性能问题与自动事件相关联,并花费了完成请求的时间。 如果持续时间即使只是很小的增量,也应咨询与磁盘相关的性能问题的存储团队。

Data and log file events

用于检查SQL Server数据库中自动增长事件的脚本: (Scripts to check auto growth events in SQL Server database:)

Let’s first execute the script and get the details.

让我们首先执行脚本并获取详细信息。

DECLARE @current_tracefilename VARCHAR(500);
DECLARE @0_tracefilename VARCHAR(500);
DECLARE @indx INT;
SELECT @current_tracefilename = path
FROM sys.traces
WHERE is_default = 1;
SET @current_tracefilename = REVERSE(@current_tracefilename);
SELECT @indx = PATINDEX('%\%', @current_tracefilename);
SET @current_tracefilename = REVERSE(@current_tracefilename);
SET @0_tracefilename = LEFT(@current_tracefilename, LEN(@current_tracefilename) - @indx) + '\log.trc';
SELECT DatabaseName, 
       te.name, 
       Filename, 
       CONVERT(DECIMAL(10, 3), Duration / 1000000e0) AS TimeTakenSeconds, 
       StartTime, 
       EndTime, 
       (IntegerData * 8.0 / 1024) AS 'ChangeInSize MB', 
       ApplicationName, 
       HostName, 
       LoginName
FROM ::fn_trace_gettable(@0_tracefilename, DEFAULT) t
     INNER JOIN sys.trace_events AS te ON t.EventClass = te.trace_event_id
WHERE(trace_event_id >= 92
      AND trace_event_id <= 95)
ORDER BY t.StartTime;

We get the following output, and it shows useful information for all file grow events. We also get the application name, hostname and login name that caused this file size growth.

我们得到以下输出,它显示了所有文件增长事件的有用信息。 我们还获得了导致该文件大小增加的应用程序名称,主机名和登录名。

SQL query output to get auto growth events details for the SQL Server Database

Let’s understand the query quickly.

让我们快速了解查询。

  • sys.trace_events catalog view to get SQL Server events and filter those events for our SQL Server database. In the following query, we can see the trace event id 92 to 95 with their description. The event id and description do not change with the new versions of SQL Server sys.trace_events目录视图获取SQL Server事件并为SQL Server数据库过滤这些事件。 在以下查询中,我们可以看到跟踪事件ID 92到95及其描述。 新版本SQL Server不会更改事件ID和描述

    SELECT *
    FROM sys.trace_events
    WHERE trace_event_id >= 92
          AND trace_event_id <= 95;
    

    sys.trace_events for auto growth

  • sys.traces catalog view to get details of current running traces on the system. If the sys.traces目录视图来获取系统上当前正在运行的跟踪的详细信息。 如果任何正在运行的跟踪的is_default property value for any running trace is 1, it shows for the default trace. We also get the trace file location using this catalog view is_default属性值为1,则显示默认跟踪。 我们还使用此目录视图获取跟踪文件的位置
  • fn_trace_gettable table-valued function to read the content of a trace file and return it in a tabular format. In this query, it reads the default trace file and gives us the required information fn_trace_gettable表值函数读取跟踪文件的内容,并以表格格式返回它。 在此查询中,它将读取默认跟踪文件并向我们提供所需的信息

Find-DbaDbGrowthEvent DBATools命令 (Find-DbaDbGrowthEvent DBATools Command)

We can also use DBATools PowerShell command to get details of auto-growth events in the SQL Server database. It reads the default trace and provides us with the information for the auto- growth the event.

我们还可以使用DBATools PowerShell命令来获取SQL Server数据库中自动增长事件的详细信息。 它读取默认跟踪,并为我们提供事件自动增长的信息。

The DBATools command to check auto growth events is Find-DbaDbGrowthEvent.

用于检查自动增长事件的DBATools命令是Find-DbaDbGrowthEvent。

Let’s check the syntax, synopsis and description of Find-DbaDbGrowthEvent command.

让我们检查一下Find-DbaDbGrowthEvent命令的语法,大纲和描述。

> get-help Find-DbaDbGrowthEvent 

help page of Find-DbaDbGrowthEvent DBATools command

Find-DbaDbGrowthEvent command checks for the AutoGrow events in the Default Trace for all databases. It includes the following events.

Find-DbaDbGrowthEvent命令检查所有数据库的默认跟踪中的自动增长事件。 它包括以下事件。

  • 92 – Data File Auto Grow

    92 –数据文件自动增长
  • 93 – Log File Auto Grow

    93 –日志文件自动增长
  • 94 – Data File Auto Shrink

    94 –数据文件自动收缩
  • 95 – Log File Auto Shrink

    95 –日志文件自动收缩

Let’s execute the command for my SQL instance and get the output in a grid format.

让我们为我SQL实例执行命令,并以网格格式获取输出。

> Find-DbaDbGrowthEvent -SqlInstance Kashish\SQL2019CTP | Out-GridView

It opens a new interactive output window, and we can see all auto growth events in this.

它打开一个新的交互式输出窗口,我们可以在其中看到所有自动增长事件。

Find-DbaDbGrowthEvent DBATools command

结论 (Conclusion)

In this article, we explored the overview of auto growth and shrink activity in the SQL Server database. We also learned a different way to extract the growth events details from the default trace of SQL Server. If you have any comments or questions, feel free to leave them in the comments below.

在本文中,我们探讨了SQL Server数据库中自动增长和收缩活动的概述。 我们还学习了从SQL Server的默认跟踪中提取增长事件详细信息的另一种方法。 如果您有任何意见或疑问,请随时将其留在下面的评论中。

翻译自: https://www.sqlshack.com/get-details-of-sql-server-database-growth-and-shrink-events/

  • 0
    点赞
  • 2
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值