Monday, 2 September 2019

Use DynamicPerf- Beginner Guide-Find Expensive Query by time period


Use DynamicsPerf

SELECT TOP 100 SERVER_NAME,
               DATABASE_NAME,
               QUERY_HASH,
               QUERY_PLAN_HASH,
               SUM(CAST(TIME_THIS_PERIOD / 1000.000 AS DECIMAL(20, 3)))                               AS TOTAL_TIME_MS,
               SUM(EXECUTIONS_THIS_PERIOD)                                                            AS TOTAL_EXECUTIONS,
               CASE SUM(EXECUTIONS_THIS_PERIOD)
                 WHEN 0 THEN 0
                 ELSE ( SUM(CAST(TIME_THIS_PERIOD / 1000.000 AS DECIMAL(20, 3))) / SUM(EXECUTIONS_THIS_PERIOD) )
               END                                                                                    AS AVG_TIME_MS,
               (SELECT TOP 1 QUERY_PLAN_PARSED
                FROM   QUERY_PLANS_PARSED_VW V2
                WHERE  V2.SERVER_NAME = CTE.SERVER_NAME
                       AND V2.DATABASE_NAME = CTE.DATABASE_NAME
                       AND V2.QUERY_HASH = CTE.QUERY_HASH
                       AND V2.QUERY_PLAN_HASH = CTE.QUERY_PLAN_HASH)                                  AS QUERY_PLAN_PARSED,
               (SELECT SQL_TEXT
                FROM   QUERY_TEXT V2
                WHERE  V2.SERVER_NAME = CTE.SERVER_NAME
                       AND V2.DATABASE_NAME = CTE.DATABASE_NAME
                       AND V2.QUERY_HASH = CTE.QUERY_HASH)                                            AS SQL_TEXT,
               (SELECT QUERY_PLAN
                FROM   QUERY_PLANS V2
                WHERE  V2.SERVER_NAME = CTE.SERVER_NAME
                       AND V2.DATABASE_NAME = CTE.DATABASE_NAME
                       AND V2.QUERY_PLAN_HASH = CTE.QUERY_PLAN_HASH)                                  AS QUERY_PLAN,
               SUM(CAST(WORKER_TIME_THIS_PERIOD / 1000.000 AS DECIMAL(20, 3)))                        AS WORK_TIME,
               SUM(CAST(( TIME_THIS_PERIOD - WORKER_TIME_THIS_PERIOD ) / 1000.000 AS DECIMAL(14, 3))) AS WAIT_TIME
FROM   QUERY_STATS_CTE_VW CTE
WHERE  1 = 1
--AND STATS_TIME BETWEEN '2018-10-29 07:15:00.000' AND '2018-10-29 07:26:00.000' --REH 1 minute past last collection
--AND STATS_TIME IN (SELECT MAX(STATS_TIME) FROM QUERY_STATS)  --Look at last data collection
--  Find all queries using a specific table/index,
--AND QUERY_PLAN_HASH IN
--             (SELECT DISTINCT  QUERY_PLAN_HASH FROM QUERY_PLANS_PARSED QPP WHERE CTE.QUERY_PLAN_HASH = QPP.QUERY_PLAN_HASH
--                             AND TABLE_NAME = 'INVENTDIM' AND INDEX_NAME = 'I_698DIMIDIDX')
--AND DATABASE_NAME = 'XXXXXXXXXXXXXX'
--AND QUERY_HASH = 0x020C200A1715926C
--AND SQL_TEXT like '%TABLE_NAME%'
GROUP  BY SERVER_NAME,
          DATABASE_NAME,
          QUERY_HASH,
          QUERY_PLAN_HASH
ORDER  BY TOTAL_TIME_MS DESC

Ebooks Site for future reference

Thursday, 16 May 2019

Find running query in sql


SELECT s.session_id
    ,r.STATUS
    ,r.blocking_session_id 'blocked by'
    ,r.wait_type
    ,wait_resource
    ,r.wait_time / (1000.0) 'Wait Time (in Sec)'
    ,r.cpu_time
    ,r.logical_reads
    ,r.reads
    ,r.writes
    ,r.total_elapsed_time / (1000.0) 'Elapsed Time (in Sec)'
    ,Substring(st.TEXT, (r.statement_start_offset / 2) + 1, (
            (
                CASE r.statement_end_offset
                    WHEN - 1
                        THEN Datalength(st.TEXT)
                    ELSE r.statement_end_offset
                    END - r.statement_start_offset
                ) / 2
            ) + 1) AS statement_text
    ,Coalesce(Quotename(Db_name(st.dbid)) + N'.' + Quotename(Object_schema_name(st.objectid, st.dbid)) + N'.' +
     Quotename(Object_name(st.objectid, st.dbid)), '') AS command_text
    ,r.command
    ,s.login_name
    ,s.host_name
    ,s.program_name
    ,s.host_process_id
    ,s.last_request_end_time
    ,s.login_time
    ,r.open_transaction_count
FROM sys.dm_exec_sessions AS s
INNER JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
WHERE r.session_id != @@SPID
ORDER BY r.cpu_time DESC
    ,r.STATUS
    ,r.blocking_session_id
    ,s.session_id