ExtendEvent-判断DB是否有在使用
Posted JinweiChang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExtendEvent-判断DB是否有在使用相关的知识,希望对你有一定的参考价值。
判断一个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‘AAjinwei‘)) ADD TARGET package0.event_file(SET filename=N‘D:XEventDB_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:XEventDB_Usage_0*.xel‘, null, null, null)) ed cross apply ed.event_data.nodes(‘event‘) as q(n)
以上是关于ExtendEvent-判断DB是否有在使用的主要内容,如果未能解决你的问题,请参考以下文章