标签:color arc ali mem name 扩展 pack targe retention
判断一个DB是否在被使用,需要观察一段时间,创建一个扩展事件会话,收集该DB是否有执行SQL即可。
创建会话
CREATE EVENT SESSION [DB_Usage] ON SERVER ADD EVENT sqlserver.sql_statement_completed(SET collect_statement=(1) ACTION(sqlserver.client_app_name,sqlserver.client_hostname, sqlserver.database_name,sqlserver.nt_username,sqlserver.sql_text,sqlserver.username) WHERE ([sqlserver].[equal_i_sql_unicode_string]([sqlserver].[database_name],N‘SDS_NONEDI_20190627‘) OR [sqlserver].[equal_i_sql_unicode_string]([sqlserver].[database_name],N‘Web‘) AND [sqlserver].[nt_user]<>N‘AA\jinwei‘)) ADD TARGET package0.event_file(SET filename=N‘D:\XEvent\DB_Usage.xel‘) WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS, MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE, TRACK_CAUSALITY=OFF,STARTUP_STATE=ON) GO
解析数据
select SWITCHOFFSET(n.value(‘@timestamp‘,‘Datetime‘),‘+08:00‘) as EventTime, n.value(‘(data[@name="duration"]/value)[1]‘, ‘int‘) as duration, n.value(‘(data[@name="statement"]/value)[1]‘, ‘nvarchar(max)‘) as statement, n.value(‘(action[@name="database_name"]/value)[1]‘, ‘nvarchar(128)‘) as database_name, n.value(‘(action[@name="client_app_name"]/value)[1]‘, ‘nvarchar(128)‘) as client_app_name, n.value(‘(action[@name="username"]/value)[1]‘, ‘nvarchar(128)‘) as username, n.value(‘(action[@name="nt_username"]/value)[1]‘, ‘nvarchar(128)‘) as nt_username, n.value(‘(action[@name="client_hostname"]/value)[1]‘, ‘nvarchar(128)‘) as client_hostname from (select cast(event_data as XML) as event_data from sys.fn_xe_file_target_read_file(‘D:\XEvent\DB_Usage_0*.xel‘, null, null, null)) ed cross apply ed.event_data.nodes(‘event‘) as q(n)
标签:color arc ali mem name 扩展 pack targe retention
原文地址:https://www.cnblogs.com/JinweiChang/p/11854737.html