Original article: Click to open the link in SQLServer. TempDB is mainly responsible for external use of the following three types of situations: internal use (sorting, hashjoin, worktable, etc.) (Temporary tables, table variables, etc) for internal use of row Version Control (Optimistic Concurrency Control), some complicated queries involve a large number of parallel and sort operations.
Original article: Click to open the link in SQL Server. TempDB is mainly responsible for external use (temporary table, table variables, etc.) Row Version Control (Optimistic Concurrency Control) for internal use, some complicated queries involve a large number of parallel, sort, and other operations
Original article: Click to open the link
In SQL Server, TempDB is mainly responsible for the use of the following three types of situations:
Internal use (sorting, hash join, work table, etc)
External Use (temporary tables, table variables, etc)
Row Version Control (Optimistic Concurrency Control)
For internal use, some complicated queries require a large amount of memory space because they involve a large number of parallel, sorting, and other operations, at the beginning of each query, SQL Server estimates the amount of memory required. During the execution process, if the allocated memory is insufficient, the extra memory needs to be processed by TempDB, this is the so-called Spill to TempDB.
The following statement can be used to observe the number of reads and writes caused by a query to TempDB:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
DECLARE@read BIGINT,
@writeBIGINT
;
SELECT@read= SUM(num_of_bytes_read),
@write = SUM(num_of_bytes_written)
FROM tempdb.sys.database_files ASDBF
JOIN sys.dm_io_virtual_file_stats(2, NULL)ASFS
ONFS.file_id = DBF.file_id
WHERE DBF.type_desc = 'ROWS'
-- Put the statement to be measured here
SELECTtempdb_read_MB = (SUM(num_of_bytes_read) - @read) / 1024. / 1024.,
tempdb_write_MB = (SUM(num_of_bytes_written) - @write) / 1024. / 1024.,
internal_use_MB =
(
SELECTinternal_objects_alloc_page_count / 128.0
FROM sys.dm_db_task_space_usage
WHERE session_id = @@SPID
)
FROM tempdb.sys.database_files ASDBF
JOIN sys.dm_io_virtual_file_stats(2, NULL)ASFS
ONFS.file_id = DBF.file_id
WHERE DBF.type_desc = 'ROWS'
|
The results of TempDB usage caused by bad queries recently seen by a customer are as follows:
You can use this query to learn how many TempDB statements are used.