SQL server to see what statements in the use of temporary table

SQL server query statements that use a temporary table

Recently discovered in the daily work performance test, IO read and write the database side is relatively large, regular 2-8M fluctuations version of the database is SQL server 2008 sp3.

These IO operations mainly from a temporary table, before the test, we have more consumption of resources on the part of the statement tracked.

Some statements using temporary tables were modified, but certainly there are still caught fish. We need to be screened, so that further optimization.

Before we begin, we start to understand what kind of operation will be used to temporary tables:

  • When the user object, such as temporary tables, table variables (# ##, the beginning of the @ variables)
  • cursor
  • Some of the internal printing and sorting operations
  • Line version for snapshot isolation control mechanisms
  • Online index rebuild operations
  • Enabling MARS (Multiple Active Resultsets) mechanism or operation
  • trigger

Some of the above mechanism or operation, will use the temporary table space systems.

We can use the following statement to get some information related to temporary tables:

1. temporary database query how many spare resources:

SELECT   SUM(unallocated_extent_page_count) AS [Temp Free Pages], 
        (SUM(unallocated_extent_page_count)*1.0/128) AS [Temp Free space in MB] 
FROM sys.dm_db_file_space_usage;

 

2. Query TempDB how many resources with version control

SELECT  SUM(version_store_reserved_page_count) AS [TempDB version store pages used], 
        (SUM(version_store_reserved_page_count)*1.0/128) AS [TempDB version store space in MB] 
FROM sys.dm_db_file_space_usage;

 

3. Query TempDB how many resources are being used by internal objects:

SELECT   SUM(internal_object_reserved_page_count) AS [TempDB internal object pages used], 
        (SUM(internal_object_reserved_page_count)*1.0/128) AS [TempDB internal object space in MB] 
FROM sys.dm_db_file_space_usage;

 

4. Query TempDB how many resources are being used by user-level objects:

SELECT SUM(user_object_reserved_page_count) AS [TempDB user object pages used], 
    (SUM(user_object_reserved_page_count)*1.0/128) AS [TempDB user object space in MB] 
FROM sys.dm_db_file_space_usage;

 

In the above statement, we will check out the page divided by 128 to get the value, it is because the size of each page of the SQL server is 8K, then divided by 128 to get the value of M units of data.

We can combine the following several system management table, get the current session TempDB use cases:

  • dm_db_file_space_usage - it Returns the space in tempdb file usage
  • dm_db_session_space_usage - returns each session to allocate and free distribution of the number of pages
  • dm_db_task_space_usage - return to the task page allocation and deallocation activity

With this statement we can see that the overall resource allocation of tempdb.

SELECT    ssu.session_id,
        ssu.internal_objects_alloc_page_count,
        ssu.user_objects_alloc_page_count,
        ssu.internal_objects_dealloc_page_count ,
        ssu.user_objects_dealloc_page_count,
        es.*
FROM    sys.dm_db_session_space_usage  ssu ,sys.dm_exec_sessions as es 
WHERE   ssu.session_id = es.session_id
AND     (ssu.internal_objects_alloc_page_count>0
OR      ssu.user_objects_alloc_page_count>0
OR      ssu.internal_objects_dealloc_page_count>0
OR      ssu.user_objects_dealloc_page_count>0)

 

 

We can through the following statement to obtain the session currently in use Tempdb SQL statement:

SELECT    ssu.session_id,                             
        st.text
FROM    sys.dm_db_session_space_usage as ssu,
        sys.dm_exec_requests as er
CROSS APPLY    sys.dm_exec_sql_text(er.sql_handle) AS st
WHERE   ssu.session_id = er.session_id
AND        ssu.session_id > 0
AND        (ssu.internal_objects_alloc_page_count > 0
OR        ssu.user_objects_alloc_page_count > 0
OR        ssu.internal_objects_dealloc_page_count > 0
OR        ssu.user_objects_dealloc_page_count > 0)

 

We can use the following statement to obtain the current session which is being used tempdb, and other information:

SELECT
  sys.dm_exec_sessions.session_id AS [SESSION ID]
  ,DB_NAME(database_id) AS [DATABASE Name]
  ,HOST_NAME AS [System Name]
  ,program_name AS [Program Name]
  ,login_name AS [USER Name]
  ,status
  ,cpu_time AS [CPU TIME (in milisec)]
  ,total_scheduled_time AS [Total Scheduled TIME (in milisec)]
  ,total_elapsed_time AS    [Elapsed TIME (in milisec)]
  ,(memory_usage * 8)      AS [Memory USAGE (in KB)]
  ,(user_objects_alloc_page_count * 8) AS [SPACE Allocated FOR USER Objects (in KB)]
  ,(user_objects_dealloc_page_count * 8) AS [SPACE Deallocated FOR USER Objects (in KB)]
  ,(internal_objects_alloc_page_count * 8) AS [SPACE Allocated FOR Internal Objects (in KB)]
  ,(internal_objects_dealloc_page_count * 8) AS [SPACE Deallocated FOR Internal Objects (in KB)]
  ,CASE is_user_process
             WHEN 1      THEN 'user session'
             WHEN 0      THEN 'system session'
  END         AS [SESSION Type], row_count AS [ROW COUNT]
FROM 
  sys.dm_db_session_space_usage
INNER join
  sys.dm_exec_sessions
ON  sys.dm_db_session_space_usage.session_id = sys.dm_exec_sessions.session_id
ORDER BY status ASC

 

Finally, the online collection to a foreign cattle were written SQL statement, combined with the use of resources, more comprehensive: Reference: Pinal Dave ( https://blog.sqlauthority.com 

SELECT
st.dbid AS QueryExecutionContextDBID,
DB_NAME(st.dbid) AS QueryExecContextDBNAME,
st.objectid AS ModuleObjectId,
SUBSTRING(st.TEXT,
dmv_er.statement_start_offset / 2  +  1 ,
( CASE  WHEN dmv_er.statement_end_offset =  - 1 
THEN  LEN ( CONVERT ( nvarchar ( MAX ), st. TEXT )) *  2 
ELSE dmv_er.statement_end_offset
 END  - dmv_er.statement_start_offset) / 2 ) AS QUERY_TEXT,
dmv_tsu.session_id ,
dmv_tsu.request_id,
dmv_tsu.exec_context_id,
(dmv_tsu.user_objects_alloc_page_count - dmv_tsu.user_objects_dealloc_page_count) AS OutStanding_user_objects_page_counts,
(dmv_tsu.internal_objects_alloc_page_count - dmv_tsu.internal_objects_dealloc_page_count) AS OutStanding_internal_objects_page_counts,
dmv_er.start_time,
dmv_er.command,
dmv_er.open_transaction_count,
dmv_er.percent_complete,
dmv_er.estimated_completion_time,
dmv_er.cpu_time,
dmv_er.total_elapsed_time,
dmv_er.reads, dmv_er.writes,
dmv_er.logical_reads,
dmv_er.granted_query_memory,
dmv_es. HOST_NAME ,
dmv_es.login_name,
dmv_es.program_name
FROM sys.dm_db_task_space_usage dmv_tsu
INNER JOIN sys.dm_exec_requests dmv_er
ON (dmv_tsu.session_id = dmv_er.session_id AND dmv_tsu.request_id = dmv_er.request_id)
INNER JOIN sys.dm_exec_sessions dmv_es
ON (dmv_tsu.session_id = dmv_es.session_id)
CROSS APPLY sys.dm_exec_sql_text(dmv_er.sql_handle) st
WHERE (dmv_tsu.internal_objects_alloc_page_count + dmv_tsu.user_objects_alloc_page_count) >= 0
ORDER BY (dmv_tsu.user_objects_alloc_page_count - dmv_tsu.user_objects_dealloc_page_count) + (dmv_tsu.internal_objects_alloc_page_count - dmv_tsu.internal_objects_dealloc_page_count) DESC

 

 

I hope the above information can help to you, for the case of high tempdb usage, recommendations can be given as follows:

  1. Try to avoid using triggers, related operations as small as possible when using the trigger.
  2. If sufficient memory resources, you can use CTE substitution variables.
  3. Sorting tables and indexes try to design a reasonable, to avoid a large number of temporary sort.
  4. An appropriate size of enlarged Tempdb file and growth mode to a fixed size.

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/wanghao4023030/p/11761688.html