Trace tempdb growth by xe


--Drop the event if it already exists IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name = 'tempdb_file_size_changed') DROP EVENT SESSION [tempdb_file_size_changed] ON SERVER; GO -- Create event CREATE EVENT SESSION [tempdb_file_size_changed] ON SERVER ADD EVENT sqlserver.database_file_size_change(SET collect_database_name=(1) ACTION (sqlserver.client_app_name,sqlserver.client_hostname, sqlserver.database_id,sqlserver.session_id, sqlserver.session_nt_username,sqlserver.username,sqlserver.sql_text) WHERE ([database_id]=(2))) ADD TARGET package0.event_file (SET filename=N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log\tempdb_file_size_changed.xel') WITH (MAX_DISPATCH_LATENCY=1 SECONDS) GO ================= SET NOCOUNT ON; GO USE [master]; GO SELECT database_name AS DatabaseName , file_type AS FileType , SUM (size_change_kb) AS Total_Size_Change_KB , SUM (duration) AS Total_Duration , client_app_name AS Client_Application , session_id AS SessionID , sql_text AS sql_text FROM ( SELECT n.value ('(data[@name="size_change_kb"]/value)[1]', 'int') AS size_change_kb , n.value ('(data[@name="database_name"]/value)[1]', 'nvarchar(50)') AS database_name , n.value ('(data[@name="duration"]/value)[1]', 'int') AS duration , n.value ('(data[@name="file_type"]/text)[1]','nvarchar(50)') AS file_type , n.value ('(action[@name="client_app_name"]/value)[1]','nvarchar(50)') AS client_app_name , n.value ('(action[@name="session_id"]/value)[1]','nvarchar(50)') AS session_id , n.value ('(action[@name="sql_text"]/value)[1]','nvarchar(500)') AS sql_text FROM ( SELECT CAST(event_data AS XML) AS event_data FROM sys.fn_xe_file_target_read_file( N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log\tempdb_file_size_changed*.xel', NULL, NULL, NULL) ) AS tab CROSS APPLY event_data.nodes('event') AS q(n)) xyz GROUP BY client_app_name, database_name, file_type, session_id, sql_text ORDER BY client_app_name start xe--------------- ================= SELECT n.value ('(data[@name="size_change_kb"]/value)[1]', 'int') AS size_change_kb , n.value ('(data[@name="database_name"]/value)[1]', 'nvarchar(50)') AS database_name , n.value ('(data[@name="duration"]/value)[1]', 'int') AS duration , n.value ('(data[@name="file_type"]/text)[1]','nvarchar(50)') AS file_type , n.value ('(action[@name="client_app_name"]/value)[1]','nvarchar(50)') AS client_app_name , n.value ('(action[@name="session_id"]/value)[1]','nvarchar(50)') AS session_id , n.value ('(action[@name="sql_text"]/value)[1]','nvarchar(500)') AS sql_text FROM ( SELECT CAST(event_data AS XML) AS event_data FROM sys.fn_xe_file_target_read_file( N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log\tempdb_file_size_changed*.xel', NULL, NULL, NULL) ) AS tab CROSS APPLY event_data.nodes('event') AS q(n) ====================

--> SQL Server tempdb file size and autogrow information <--
SET NOCOUNT ON;
GO  
USE [master];
GO
-- Check for trace flag 1117/1118 or any other trace flag
DBCC TRACESTATUS (1117) WITH NO_INFOMSGS; -- Grows all data files at once, else it goes in turns.
DBCC TRACESTATUS (1118) WITH NO_INFOMSGS; -- Switches allocations in tempDB from 1pg at a time (for first 8 pages) to one extent
DBCC TRACESTATUS () WITH NO_INFOMSGS;

SELECT
   old.physical_name AS 'FileName',
   old.name AS 'LogicalName',
   old.size * 8.0 / 1024.0 AS 'OriginalSizeInMB',
   new.size * 8.0 / 1024.0 AS 'CurrentSizeInMB',
   'GrowthType' = 
       CASE
           WHEN new.growth = 0 THEN 'File is fixed size and will not grow.'
           WHEN new.growth > 0 AND new.is_percent_growth = 0 
               THEN 'Growth increment is in units of 8-KB pages, rounded to the nearest 64 KB'
           ELSE 'Growth increment is expressed as a whole number percentage.'
       END,
   'GrowthIncrement' =
       CASE
           WHEN new.is_percent_growth = 0 THEN CAST((new.growth * 8 / 1024) AS NVARCHAR(30)) + 'MB' 
           ELSE CAST (new.growth AS NVARCHAR(30)) + '%'
           END, 
   'MaximumFileSize' =
       CASE new.max_size -- file size, in 8-KB pages
           WHEN 0 THEN 'No growth is allowed.'
           WHEN -1 THEN 'File will grow until the disk is full.'
           -- !Databases that are upgraded with an unlimited log file size will report -1 for the maximum size of the log file!
           WHEN 268435456 THEN 'Log file will grow to a maximum size of 2 TB.' 
           ELSE CAST (new.max_size * 8.0 / 1024.0 AS NVARCHAR(30)) + 'MB'
       END
FROM
   MASTER.sys.master_files old INNER JOIN tempdb.sys.database_files new ON
   old.FILE_ID = new.FILE_ID
WHERE
   database_id = DB_ID('tempdb')
   AND old.size <> new.size

  

  

你可能感兴趣的:(Trace tempdb growth by xe)