How to view the amount of TempDB space used in a query in SQLSERVER

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.