Skip to content

Instantly share code, notes, and snippets.

@ichiroku11
Last active August 19, 2017 03:48
Show Gist options
  • Select an option

  • Save ichiroku11/308682aa651b28ab7007c3d9890cd18f to your computer and use it in GitHub Desktop.

Select an option

Save ichiroku11/308682aa651b28ab7007c3d9890cd18f to your computer and use it in GitHub Desktop.
拡張イベントを試す
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
namespace ConsoleApp {
class Program {
private static readonly string _connectionString
= new SqlConnectionStringBuilder {
DataSource = ".",
IntegratedSecurity = true,
}.ToString();
static void Main(string[] args) {
using (var connection = new SqlConnection(_connectionString)) {
// 3秒以上かかってsql_batch_completedイベントを発生
var result1 = connection.Query<int>(
"waitfor delay '00:00:03'; select 100;").First();
Console.WriteLine(result1);
// 3秒以上かかってrpc_completedイベントを発生
var result2 = connection.Query<int>(
"waitfor delay '00:00:03'; select @p;",
new { p = 99 }).First();
Console.WriteLine(result2);
}
}
}
}
-- イベントセッションが存在していたら削除
if exists(select * from sys.server_event_sessions where name = N'test_xes')
drop event session test_xes
on server;
-- イベントセッションを作成
create event session test_xes
on server
-- sql_batch_completedイベントをキャプチャする
add event sqlserver.sql_batch_completed(
-- duration(バッチが完了するまでの時間)が3秒以上のイベントにフィルタする
where duration >= 3000000
),
-- rpc_completedイベントをキャプチャする
add event sqlserver.rpc_completed(
where duration >= 3000000
)
-- イベントデータをring_buffer(メモリ)に出力する
add target package0.ring_buffer,
-- イベントデータをファイルに出力する
add target package0.event_file(
-- 出力先のファイルパスを指定する
set filename = N'C:\Temp\test_xes.xel'
);
-- イベントデータを確認
select
xe_s.name,
xe_st.target_data, -- データ(xml)
*
from sys.dm_xe_sessions as xe_s
inner join sys.dm_xe_session_targets as xe_st
on xe_s.address = xe_st.event_session_address
where xe_s.name = N'test_xes';
-- イベントセッション(キャプチャ)を開始
alter event session test_xes
on server
state = start;
-- イベントセッション(キャプチャ)を停止
alter event session test_xes
on server
state = stop;
-- イベントデータを確認(XMLからデータを取り出す)
with xe_e(data)
as(
select
cast(event_data as xml) -- データ
-- ファイルを読み込む
from sys.fn_xe_file_target_read_file(N'C:\Temp\test_xes_*.xel', null, null, null)
)
select
data,
data.value('(event/@package)[1]', 'nvarchar(10)') as package,
data.value('(event/@name)[1]', 'nvarchar(20)') as event,
data.value('(event/@timestamp)[1]', 'datetime2') as timestamp,
data.value('(event/data[@name="cpu_time"]/value)[1]', 'bigint') as cpu_time,
data.value('(event/data[@name="duration"]/value)[1]', 'bigint') as duration,
data.value('(event/data[@name="batch_text"]/value)[1]', 'nvarchar(max)') as batch_text,
data.value('(event/data[@name="statement"]/value)[1]', 'nvarchar(max)') as statement
from xe_e;
-- イベントデータを確認(XMLからデータを取り出す)
with xe_e(data)
as(
select
cast(xe_st.target_data as xml) -- データ(xml)
from sys.dm_xe_sessions as xe_s
inner join sys.dm_xe_session_targets as xe_st
on xe_s.address = xe_st.event_session_address
where xe_s.name = N'test_xes'
)
select
node.query('.'),
node.value('@package', 'nvarchar(10)') as package,
node.value('@name', 'nvarchar(20)') as event,
node.value('@timestamp', 'datetime2') as timestamp,
node.value('(data[@name="cpu_time"]/value)[1]', 'bigint') as cpu_time,
node.value('(data[@name="duration"]/value)[1]', 'bigint') as duration,
node.value('(data[@name="batch_text"]/value)[1]', 'nvarchar(max)') as batch_text,
node.value('(data[@name="statement"]/value)[1]', 'nvarchar(max)') as statement
from xe_e
cross apply [data].nodes('/RingBufferTarget/event') as event(node);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment