SQLトレース

(19c)


alter session set TRACEFILE_IDENTIFIER = 'trace1';

alter session set timed_statistics = true;


alter session set events '10046 trace name context forever, level 12';
select * from tab1;
alter session set events '10046 trace name context off';

cd /u01/app/oracle/diag/rdbms/orcl/orcl/trace

tkprof orcl_ora_1646_trace1.trc orcl_ora_1646_trace1.txt sys=no

 

※2021/04/16追記
CPU列のために、「TIMED_STATISTICS」パラメータがtrueになっている必要がある。
「STATISTICS_LEVEL」パラメータがTYPICALまたはALLに設定されている場合、「TIMED_STATISTICS」パラメータはデフォルトでTRUEとなる。
したがって、「STATISTICS_LEVEL」パラメータがデフォルトのTYPICALの場合、特に設定は不要と思われるが、念のため、下記の設定を追加する。

alter session set timed_statistics = true;

なお、「STATISTICS_LEVEL」パラメータをALLに設定する必要はないと思われる。(マニュアルには記載がない)

http://nippondanji.blogspot.com/2009/02/mysql.html

set profiling=1;
select 1;
show profile;
SHOW PROFILE SOURCE;
SHOW PROFILE all;
show profile block io;


show profiles;
show profile for query 1;

set @query_id = 1;
select * from information_schema.profiling where query_id = @query_id;

 

SHOW PROFILE SOURCE FOR QUERY 9;
show profile cpu, block io for query 1;

set profiling_history_size=100;

--pg_stat_statements

https://qiita.com/kite_999/items/ae312952dc5a55bd7cf5


yum install postgresql94-contrib


vim postgresql.conf
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.max = 2000
pg_stat_statements.track = top
pg_stat_statements.save = on


select * from pg_available_extensions order by name;
モジュールのインストール
CREATE EXTENSION pg_stat_statements;

SELECT query, calls, total_time, rows, 100.0 * shared_blks_hit /
nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements ORDER BY total_time DESC LIMIT 100;

--リセット
SELECT pg_stat_statements_reset();

https://docs.microsoft.com/ja-jp/sql/relational-databases/sql-trace/create-and-run-traces-using-transact-sql-stored-procedures?view=sql-server-2017

--SQL Server Profilerによるトレース

スロークエリ確認の項参照


--拡張イベント
https://ichiroku11.hatenablog.jp/entry/2017/08/12/200819
https://ichiroku11.hatenablog.jp/entry/2017/08/24/112453


-- イベントセッションを作成
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
)
-- イベントデータをファイルに出力する
add target package0.event_file(
-- 出力先のファイルパスを指定する
set filename = N'E:\work\test_xes.xel'
);


-- セッション(キャプチャ)を開始
alter event session test_xes
on server
state = start;


-- セッション(キャプチャ)を停止
alter event session test_xes
on server
state = stop;


--ファイルに出力されたイベントデータを確認

with xe_e(data)
as(
select cast(event_data as xml) -- データ
-- ファイルを読み込む
from sys.fn_xe_file_target_read_file(N'E:\work\test_xes_*.xel', null, null, null)
)
select
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="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;

 

--拡張イベント一覧
select name,description from sys.dm_xe_objects order by name;

SELECT name, description
FROM sys.dm_xe_objects
WHERE object_type = 'event'
ORDER BY name