Skip to content

Instantly share code, notes, and snippets.

@ghotz
Created March 4, 2015 12:48
Show Gist options
  • Select an option

  • Save ghotz/db38bb62dc04d459a1a1 to your computer and use it in GitHub Desktop.

Select an option

Save ghotz/db38bb62dc04d459a1a1 to your computer and use it in GitHub Desktop.
Get a rough idea of the workload distribution per database
WITH cte AS
(
SELECT DB_NAME(t.[dbid]) AS [Database Name],
qs.total_worker_time AS [Total Worker Time],
qs.min_worker_time AS [Min Worker Time],
qs.total_worker_time/qs.execution_count AS [Avg Worker Time],
qs.execution_count AS [Execution Count]--,
FROM sys.dm_exec_query_stats AS qs WITH (NOLOCK)
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS t
)
SELECT [Database Name]
, SUM([Total Worker Time]) AS [Total Worker Time]
, 1. * SUM([Total Worker Time]) / (SELECT SUM(cte.[Total Worker Time]) FROM cte) * 100 AS [Worker Time Percentage]
, SUM([Execution Count]) AS [Total Execution Count]
, 1. * SUM([Execution Count]) / (SELECT SUM(cte.[Execution Count]) FROM cte) * 100 AS [Execution Count Percentage]
FROM cte
GROUP BY [Database Name]
ORDER BY 2 DESC OPTION (RECOMPILE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment