[MS SQL] SP수행 시간 및 작업빈도 확인

SQL 2008에서 제공하는
sys.dm_exec_procedure_stats,
sys.dm_exec_sql_text,
sys.dm_exec_query_plan
 통해 시스템에서 가장 많이 사용하는 것과 읽기/쓰기를 많이 하는 SP 추적하여
성능 모니터링에 이용할  있다.

주석으로 되어 있는 부분을 적절히 풀어서 필요에 맞게 수정하여 사용 가능.

ALTER PROCEDURE SP_CHECK_FRQ
    @EXE_COUNT INT=100,
    @WORK_TIME float=50,
    @WRITE_COUNT INT=1
AS
BEGIN
-- SP_CHECK_FRQ 10,50,1
    SELECT *
    FROM (select
            DB_NAME(qt.dbid) as 'DB',
            OBJECT_NAME(qt.objectid, qt.dbid) as 'SP',
            qs.execution_count as '실행횟수',
            isnull(qs.total_worker_time/qs.execution_count, 0)/1000.0 as '평균작업시간(ms)',        
            qs.total_worker_time/1000.0 as '전체작업시간(ms)',
            qs.max_worker_time/1000.0 as '최대작업시간(ms)',
            --ISNULL(qs.execution_count/DATEDIFF(second, qs.cached_time, getdate()), 0) as 'Calls/Second',
            --DATEDIFF(Minute, qs.cached_time, getdate()) as 'Age in Cache(min)',
            --qs.cached_time,
            --qs.last_execution_time,
            --ISNULL(qs.total_elapsed_time/qs.execution_count, 0) /1000.0 as 'avg_Elapsed_Time(ms)',
            --qs.total_elapsed_time/1000.0/1000.0 as 'total_Elapsed_Time(sec)',
            --max_elapsed_time /1000.0/1000.0 as 'max_Elapsed_Time(sec)',

            isnull(qs.total_logical_reads/qs.execution_count,0) as '평균읽기수',
            --qs.total_logical_reads as total_logical_reads,
            --qs.max_logical_reads as max_logical_reads,
            --isnull(qs.total_physical_reads/qs.execution_count, 0) as 'avg_physical_reads',
            --qs.total_physical_reads as total_physical_reads,
            --qs.max_physical_reads as max_physical_reads,
            isnull(qs.total_logical_writes/qs.execution_count, 0) as '평균쓰기수'
            --qs.total_logical_writes as total_logical_writes,
            --qs.max_logical_writes as max_logical_writes,
            --qp.query_plan
        from sys.dm_exec_procedure_stats as qs
            cross apply sys.dm_exec_sql_text (qs.sql_handle) as qt
            cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
        where db_name(qt.dbid) is not null ) x    
    where [실행횟수]>=@EXE_COUNT
    and [평균작업시간(ms)]>=@WORK_TIME
    and [평균쓰기수]>=@WRITE_COUNT
    --order by [평균작업시간(ms)] DESC, [실행횟수] DESC
    order by [실행횟수DESC, [평균작업시간(ms)] DESC
END

댓글

이 블로그의 인기 게시물

dtsrun 실행하기

Slug가 뭘까?