码迷,mamicode.com
首页 > 数据库 > 详细

SQL 元数据征用 PageLatch_

时间:2020-12-11 12:13:33      阅读:3      评论:0      收藏:0      [点我收藏+]

标签:aspect   figure   contex   inner   混合   case   记录   快照   for   

https://blog.csdn.net/culuo4781/article/details/107627394

 

 

sql tempdb清理_SQL Server TempDB数据库和闩锁争用

sql tempdb清理

In this article, we will learn latch contention issues that we might experience in the SQL Server TempDB database. We will also discuss reasons and the solution method of these latch contention issues. Especially, we will mention the Memory-Optimized TempDB Metadata feature that was introduced with the SQL Server 2019.

在本文中,我们将学习SQL Server TempDB数据库中可能遇到的闩锁争用问题。 我们还将讨论这些闩锁争用问题的原因和解决方法。 特别是,我们将提到SQL Server 2019引入的内存优化的TempDB元数据功能。

Firstly, we will briefly learn the essential characteristics of the TempDB database, and we will also talk about the latch concept of the SQL Server so that we can understand all aspects of the latch contention problems of the TempDB database more clearly.

首先,我们将简要学习TempDB数据库的基本特征,还将讨论SQL Server的闩锁概念,以便我们可以更清楚地了解TempDB数据库的闩锁争用问题的各个方面。

SQL Server中使用的TempDB数据库是什么? (What is the TempDB database used for in SQL Server?)

TempDB database is one of the system databases of the SQL Server, but it has various unique functionalities as distinct from other system databases. Global and local temporary tables are created in this SQL Server TempDB database, and the data of these tables are stored by this database. At the same time, table variables, temporary stored procedures, and cursors are used in this database resource. In addition, TempdDB resources are also used by the following features.

TempDB数据库是SQL Server的系统数据库之一,但是它具有与其他系统数据库不同的各种独特功能。 全局和本地临时表在此SQL Server TempDB数据库中创建,这些表的数据由该数据库存储。 同时,此数据库资源中使用表变量,临时存储过程和游标。 此外,以下功能还使用TempdDB资源。

  • Snapshot Isolation and Read-Committed Snapshot Isolation

     

    快照隔离和读取提交的快照隔离
  • Online index operations

     

    在线索引操作
  • MARS – (Multiple Active Result Sets)

     

    MARS –(多个活动结果集)

When we restart the SQL engine TempdDB database is dropped and re-created. We can not take back up this database and can not change the recovery model from simple to others. When we are taking into account all of these, we can say that the TempDB database settings directly affect query performances.

当我们重新启动SQL引擎时,将删除TempdDB数据库并重新创建它。 我们无法备份此数据库,也无法将恢复模式从简单更改为其他。 当我们考虑所有这些因素时,可以说TempDB数据库设置直接影响查询性能。

SQL Server中的闩锁是什么? (What is the latch in SQL Server?)

A SQL buffer pool is the memory place that is reserved by the operating system for the SQL Server, and it is also called as SQL buffer cache. SQL Server transfers the data pages into the memory from the disk in order to read or manipulate them and sends them back to disk according to a special logic. The main purpose of this mechanism is the desire to deliver faster performance to clients because memory is always faster than the storage systems. In this context, we need a mechanism to guarantee the data pages consistency in the buffer pool. A latch is a synchronization object used to protect data structures held in memory against inconsistency and corruption so that SQL Server ensures the consistency of data pages in the memory. This synchronization operation is managed by the SQL Server internally.

SQL缓冲池是操作系统为SQL Server保留的内存位置,也称为SQL缓冲区高速缓存。 SQL Server将数据页从磁盘传输到内存中,以便读取或操作它们,然后根据特殊逻辑将它们发送回磁盘。 这种机制的主要目的是希望为客户端提供更快的性能,因为内存始终比存储系统快。 在这种情况下,我们需要一种机制来保证缓冲池中数据页的一致性。 闩锁是一个同步对象,用于保护内存中保存的数据结构免于不一致和损坏,以便SQL Server确保内存中数据页的一致性。 此同步操作由SQL Server在内部进行管理。

TempDB数据库元数据争用 (TempDB database Metadata Contention)

TempDB metadata contention occurs when many sessions try to access the SQL Server TempDB’s system tables at the same time during the creation of the temp tables. This heavy workload causes latency on these system tables due to this reason, and the query performance will be decreased.

当在创建临时表的过程中许多会话尝试同时访问SQL Server TempDB的系统表时,就会发生TempDB元数据争用。 由于这种原因,繁重的工作负载导致这些系统表上的延迟,并且查询性能将降低。

Now, we will create a fake workload on the TempDB to simulate this problem. We will use an oldie but goodie tool named SQLQueryStress to generate a fake workload on the TempDB database.

现在,我们将在TempDB上创建伪造的工作负载以模拟此问题。 我们将使用名为SQLQueryStress的老式工具,在TempDB数据库上生成伪造的工作负载。

At first, we will create the following procedure. This stored procedure will create a temp table and will insert random 20 rows from the sys.all_columns table.

首先,我们将创建以下过程。 此存储过程将创建一个临时表,并将从sys.all_columns表中随机插入20行。

  1.  
    CREATE PROC ProcTest
  2.  
    AS
  3.  
    BEGIN
  4.  
    CREATE TABLE #t1
  5.  
    (
  6.  
      c1 INT,
  7.  
      c2 INT);
  8.  
    INSERT INTO #t1
  9.  
           SELECT TOP 20 column_id,
  10.  
                         system_type_id
  11.  
           FROM sys.all_columns WITH(NOLOCK);
  12.  
    END

We will launch the SQLQueryStress and paste the following query into the query panel. This query executes the ProcTest stored procedure 100 times in a WHILE loop.

我们将启动SQLQueryStress并将以下查询粘贴到查询面板中。 该查询在WHILE循环中执行ProcTest存储过程100次。

  1.  
    DECLARE @i INT;
  2.  
    SET @i = 1;
  3.  
    WHILE @i <= 100
  4.  
        BEGIN
  5.  
            EXEC ProcTest;
  6.  
            SET @i = @i + 1;
  7.  
        END

We will set the Number of Iterations as 100 and will set Number of Threads as 25 so that the stored procedure executed 2500 times.

我们将迭代次数设置为100,并将线程数设置为25,以便存储过程执行2500次。

 

技术图片

We will click the Database button and set the database connection and credentials settings.

我们将单击数据库按钮,然后设置数据库连接和凭据设置。

 

技术图片

We will click the GO button to start executing the query.

我们将单击GO按钮开始执行查询。

 

技术图片

While SQLQueryStress is performing the query, we are executing sp_WhoisActive and analyze the results.

当SQLQueryStress执行查询时,我们正在执行sp_WhoisActive并分析结果。

 

技术图片

 

 

As we can see, the PAGELATCH_EX wait type can be seen in the wait_info column for the TempDB database. Specific to the TempDB database, we can overcome this wait using a new feature of the SQL Server 2019. In the next section, we will learn this feature.

如我们所见,可以在TempDB数据库的wait_info列中看到PAGELATCH_EX等待类型。 特定于TempDB数据库,我们可以使用SQL Server 2019的新功能来克服这种等待。在下一节中,我们将学习此功能。

内存优化的TempDB元数据 (Memory-Optimized TempDB Metadata)

When we enable the Memory-Optimized TempDB Metadata feature, it converts some of the SQL Server TempDB system tables to non-durable memory-optimized tables, so it minimizes the latency on TempDB’s system tables. Memory-optimized tables offer low latency, high throughput, and accelerated response time, so this feature takes advantage of these performance enhancements.

当我们启用内存优化的TempDB元数据功能时,它会将某些SQL Server TempDB系统表转换为非持久的内存优化表,从而最大程度地减少了TempDB系统表上的延迟。 内存优化表提供了低延迟,高吞吐量和加速的响应时间,因此此功能利用了这些性能增强功能。

We can enable this feature through the following query:

我们可以通过以下查询启用此功能:

ALTER SERVER CONFIGURATION SET MEMORY_OPTIMIZED TEMPDB_METADATA = ON;

Or we can use the following query to enable this option.

或者我们可以使用以下查询启用此选项。

  1.  
    EXEC sys.sp_configure N‘ show advanced options‘, 1;
  2.  
    RECONFIGURE;
  3.  
    EXEC sys.sp_configure N‘tempdb metadata memory-optimized‘, 1;
  4.  
    RECONFIGURE;
  5.  
     

The following query helps us to detect the status of this feature.

以下查询可帮助我们检测此功能的状态。

  1.  
    SELECT
  2.  
    CASE  SERVERPROPERTY(‘IsTempdbMetadataMemoryOptimized‘)
  3.  
    WHEN 1 THEN ‘Enable‘
  4.  
    WHEN 0 THEN ‘Disable‘
  5.  
    END
  6.  
    AS ‘Memory-Optimized TempDB Metadata Status‘

 

技术图片

After enabling the memory-optimized TempDB metadata feature, the SQL Server engine must be restarted. After restarting the SQL Server, we can see the list of the tables that are converted to the memory-optimized tables through the following query:

启用内存优化的TempDB元数据功能后,必须重新启动SQL Server引擎。 重新启动SQL Server之后,我们可以看到通过以下查询转换为内存优化表的表的列表:

  1.  
    SELECT mem_table.[object_id], obj.name
  2.  
      FROM tempdb.sys.all_objects AS obj
  3.  
      INNER JOIN tempdb.sys.memory_optimized_tables_internal_attributes AS mem_table
  4.  
      ON obj.[object_id] = mem_table.[object_id]

 

技术图片

When we rerun the SQLQueryStress for the same query with the same parameters. The sp_WhoIsActive output will be changed, and we don’t see any PAGELATCH_EX wait type.

当我们为具有相同参数的相同查询重新运行SQLQueryStress时。 sp_WhoIsActive输出将被更改,并且我们看不到任何PAGELATCH_EX等待类型。

 

技术图片

 

 

Memory-Optimized TempDB Metadata feature has some limitations, and we should consider these limitations before deciding to use it:

内存优化的TempDB元数据功能有一些限制,在决定使用它之前,我们应该考虑以下限制:

  • The column store indexes can not be created for the temp tables when Memory-Optimized TempDB Metadata is enabled

    启用内存优化的TempDB元数据时,无法为临时表创建列存储索引

     

     

    1.  
      CREATE TABLE #temp1 (val1 INT, val2 NVARCHAR(50));
    2.  
      CREATE COLUMNSTORE INDEX indexcol1 ON #temp1(val2);

     

     

     

    技术图片

     

     

At first, we created a local temporary table that name is #temp1, and when we tried to create a columnstore index to it, we could not succeed because the memory-optimized metadata feature is enabled.

最初,我们创建了一个名为#temp1的本地临时表,当我们尝试为其创建列存储索引时,由于启用了内存优化的元数据功能,我们无法成功。

  • sp_estimate_data_compression_savings built-in procedure does not run for the tables that include columnstore indexes when Memory-Optimized TempDB Metadata is enabled

    启用内存优化的TempDB元数据后,对于包含列存储索引的表, sp_estimate_data_compression_savings内置过程不会运行

     

     

    1.  
      CREATE TABLE t1 (val1 INT, val2 NVARCHAR(50));
    2.  
      CREATE COLUMNSTORE INDEX indexcol1 ON t1(val2);
    3.  
      GO
    4.  
      EXEC sp_estimate_data_compression_savings ‘dbo‘, ‘t1‘, NULL, NULL, ‘ROW‘ ;  

     

     

     

    技术图片

     

     

Stored Procedure, sp_estimate_data_compression_savings calculates the estimated compression gains for the tables before to compress operation. However, when we enabled the Memory-Optimized TempDB Metadata option, this procedure does not work for the t1 table because it includes a columnstore index.

在存储过程中, sp_estimate_data_compression_savings在压缩操作之前计算表的估计压缩增益。 但是,当我们启用“内存优化的TempDB元数据”选项时,此过程不适用于t1表,因为它包含列存储索引。

TempDB数据库分配页面争用 (TempDB Database Allocation Page Contention )

Data pages are the fundamental unit of the SQL Server that stores data, and the size of the data pages are 8 KB. The eight physically contiguous data pages are named extent. Information about which extents are allocated is recorded by the Global Allocation Map (GAM). Information about which extents are used as mixed is recorded by the Shared Global Allocation Map (SGAM). Page Free Space (PFS) records how much free space is available on which page in the extents.

数据页是SQL Server存储数据的基本单位,数据页的大小为8 KB。 这八个物理上连续的数据页被命名程度 。 有关分配范围的信息由全局分配图(GAM)记录。 共享全局分配图(SGAM)记录有关混合使用哪个范围的信息。 页面可用空间(PFS)记录扩展区中哪个页面上的可用空间。

A session should update the SQL Server TempDB allocation pages when creating and dropping temporary tables. As this number of concurrent connections begins to increase, accessing these pages allocation will become more difficult because, at a time, only one thread is able to change these pages, so other threads have to wait for this page to be released allocated resource. Now we will simulate this scenario.

创建和删除临时表时,会话应更新SQL Server TempDB分配页。 随着并发连接数量的增加,访问这些页面的分配将变得更加困难,因为一次只能有一个线程更改这些页面,因此其他线程必须等待该页面被释放分配的资源。 现在我们将模拟这种情况。

  • We will launch the SQLQueryStress and paste the following query into the query panel:

    我们将启动SQLQueryStress并将以下查询粘贴到查询面板中:

    1.  
      SELECT TOP 2500 *
    2.  
      INTO #t1
    3.  
      FROM sys.all_objects WITH(NOLOCK);
  • We will set the Number of Iterations as 100 and set Number of Threads as 200:

    我们将迭代次数设置为100,并将线程数设置为200:

     

    技术图片

     

  • We will click the Database button and set the database credentials and other settings:

    我们将单击数据库按钮并设置数据库凭据和其他设置:

     

    技术图片

     

  • We will click the GO button to start executing the query:

    我们将单击GO按钮开始执行查询:

     

    技术图片

     

While SQLQueryStress is performing the queries, we are executing sp_WhoisActive and analyze the result of the wait_info column.

当SQLQueryStress执行查询时,我们正在执行sp_WhoisActive并分析wait_info列的结果。

EXEC sp_WhoIsActive

 

技术图片

 

 

As we can see, the PAGELATCH_UP wait type can be seen in the wait_info column. If we add more data files to the TempDb database, this problem will be minimized, and Microsoft recommends a formula for how many files we need.

如我们所见,可以在wait_info列中看到PAGELATCH_UP等待类型。 如果我们将更多的数据文件添加到TempDb数据库,此问题将被最小化,Microsoft建议使用公式来确定需要多少文件。

“If the number of logical processors is less than or equal to eight (8), use the same number of data files as logical processors. If the number of logical processors is greater than eight (8), use eight data files. If contention continues, increase the number of data files by multiples of four (4) up to the number of logical processors until the contention is reduced to acceptable levels.”

“如果逻辑处理器的数量小于或等于八(8),请使用与逻辑处理器相同数量的数据文件。 如果逻辑处理器的数量大于八(8),请使用八个数据文件。 如果争用仍在继续,则将数据文件的数量增加四(4)的倍数,直至逻辑处理器的数量,直到争用减少到可接受的水平为止。”

According to this formula, we can increase the file number of the TempDB database to minimize this problem.

根据此公式,我们可以增加TempDB数据库的文件数量以最大程度地减少此问题。

结论 (Conclusion)

SQL Server TempDB database settings affect the performance of the queries, so we have to configure it attentively. In this article, we discussed the latch contention issues that we might face in the TempDB database and also walked through the corresponding solution methods.

SQL Server TempDB数据库设置会影响查询的性能,因此我们必须认真配置它。 在本文中,我们讨论了TempDB数据库中可能会遇到的闩锁争用问题,并逐步介绍了相应的解决方法。

翻译自: https://www.sqlshack.com/sql-server-tempdb-database-and-latch-contention/

sql tempdb清理

SQL 元数据征用 PageLatch_

标签:aspect   figure   contex   inner   混合   case   记录   快照   for   

原文地址:https://www.cnblogs.com/ifreesoft/p/14096728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!