good-arrow’s blog

https://good-arrow.net/

【SQL Server】SQL実行ログ調査

こちらを参考にさせて頂きました。ありがとうございます。
SQL Server で実行された SQL を SQL で取得する方法 - Project Group



コストの高いSQLの調査

SELECT
    st.text,
    last_execution_time,
    execution_count,
    min_worker_time,
    max_worker_time,
    total_worker_time
FROM
    sys.dm_exec_query_stats qs
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
WHERE
    last_execution_time >= '2022/12/12 12:00:00'
ORDER BY
    total_worker_time DESC,last_execution_time



参考URL

sys.dm_exec_sql_text (Transact-SQL) - SQL Server | Microsoft Learn

sys.dm_exec_query_stats (Transact-SQL) - SQL Server | Microsoft Learn



SQL Server Profiler のログ抽出(.trcファイル時)

ストアドプロシージャのパラメーターも含めたログが欲しいとき。

select
    *
from
    fn_trace_gettable(N'C:\Profiler_log\無題 - 1.trc',default)
where
-- 不要なログ出力をフィルター
    TextData not like 'exec sp_cursorfetch %'
and TextData not like 'exec sp_cursorclose %'
and TextData not like 'exec sp_cursoroption %'
and TextData not like 'exec sp_reset_connection %'
and TextData not like 'exec sp_cursor %'
and TextData not like 'set implicit_transactions on %'
--
and TextData not like '%exec sp_cursoropen %'
and TextData not like '%ロールオーバー ファイル%'
and TextData not like '%無題%'
and TextData not like 'IF%'
and TextData not like ' IF%'
and TextData not like 'SET NOCOUNT ON'