How to see how much tempdb space is used for a query

Source: Internet
Author: User
Tags join

Recently, in the process of helping customers to tune, found that the customer's tempdb there is a very large pressure, after the investigation is found that some statements on the huge use of tempdb caused.

In SQL Server, tempdb is primarily responsible for the following three types of situations:

Internal use (sort, hash join, work table, etc.)

External use (Temporary tables, table variables, etc.)

Row version control (optimistic concurrency control)

For internal use, some of the more complex queries because of a large number of parallel, sorting and other operations require a large amount of memory space, each query at the beginning will be estimated by SQL Server how much memory, in the specific implementation of the process, if the grant of insufficient memory, You will need to add the extra parts by TempDB, which is called spill to tempdb.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/

The following statement allows you to observe how much reading and writing a query has caused to tempdb:

DECLARE @read   BIGINT, 
        @write  BIGINT
;        
SELECT  @read = SUM (num_of_bytes_read), 
        @write = SUM (num_of_bytes_written) 
from    tempdb.sys.database _files as DBF
JOIN    sys.dm_io_virtual_file_stats (2, NULL) as FS on
        fs.file_id = dbf.file_id
where< C14/>dbf.type_desc = ' ROWS '
    
--here put the statement you want to measure
    
SELECT  tempdb_read_mb = (SUM (num_of_bytes_read)-@read)/ 1024./1024., 
        TEMPDB_WRITE_MB = (SUM (num_of_bytes_written)-@write)/1024./1024.,
        INTERNAL_USE_MB = 
            (
            SELECT  internal_objects_alloc_page_count/128.0
            from    sys.dm_db_task_space_usage
            WHERE   session_id = @ @SPID
            )    from Tempdb.sys.database_files as DBF
JOIN    sys.dm_io_virtual_file_stats (2, NULL) as FS
        on fs.file_id = dbf.file_id
WHERE   dbf.type_desc = ' ROWS '

The results of the tempdb usage resulting from a bad query recently seen at a customer are as follows:

Use this query to help you understand how much tempdb is used by a statement.

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.