1. 程式人生 > 實用技巧 >SQL 元資料徵用 PageLatch_

SQL 元資料徵用 PageLatch_

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

sql tempdb清理_SQL Server TempDB資料庫和閂鎖爭用

2020-07-25 09:18:12127收藏1 文章標籤:資料庫大資料pythonjavamysql 版權

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 aspecial 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 namedSQLQueryStressto 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 theNumber of Iterationsas 100 and will setNumber of Threadsas 25 so that the stored procedure executed 2500 times.

我們將迭代次數設定為100,並將執行緒數設定為25,以便儲存過程執行2500次。

We will click theDatabasebutton and set the database connection and credentials settings.

我們將單擊資料庫按鈕,然後設定資料庫連線和憑據設定。

We will click theGObutton to start executing the query.

我們將單擊GO按鈕開始執行查詢。

While SQLQueryStress is performing the query, we are executingsp_WhoisActiveand analyze the results.

當SQLQueryStress執行查詢時,我們正在執行sp_WhoisActive並分析結果。

As we can see, thePAGELATCH_EXwait 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 latencyon 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;

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

以下查詢可幫助我們檢測此功能的狀態。

  1. SELECT
  2. CASESERVERPROPERTY('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 anyPAGELATCH_EXwait 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_savingsbuilt-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_savingscalculates 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 pagesare 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 namedextent. Information about which extents are allocated is recorded by theGlobal Allocation Map (GAM).Information about which extents are used as mixed is recorded by theShared 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 theNumber of Iterationsas 100 and setNumber of Threadsas 200:

    我們將迭代次數設定為100,並將執行緒數設定為200:

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

    我們將單擊資料庫按鈕並設定資料庫憑據和其他設定:

  • We will click theGObutton 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, thePAGELATCH_UPwait 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 eightdata 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清理