Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Display the 50 most used stored procedures in SQL Server

From Wiki

Jump to: navigation, search

Sometimes you get asked what are the most executed stored procedures in your database, before SQL Server 2005 this was a big pain in the neck. SQL Server 2005 makes this much easier to find out with the introduction of dynamic management views. The two dynamic management views we will use are sys.dm_exec_query_stats and sys.dm_exec_sql_text.

To get the top 50 most executed statements in stored procedures you would use the following query


  1. SELECT top 50 * FROM(SELECT COALESCE(OBJECT_NAME(s2.objectid),'Ad-Hoc') AS ProcName,execution_count,s2.objectid,
  2.     (SELECT TOP 1 SUBSTRING(s2.TEXT,statement_start_offset / 2+1 ,
  3.       ( (CASE WHEN statement_end_offset = -1
  4.          THEN (LEN(CONVERT(NVARCHAR(MAX),s2.TEXT)) * 2)
  5.          ELSE statement_end_offset END)  - statement_start_offset) / 2+1)) AS sql_statement,
  6.        last_execution_time
  7. FROM sys.dm_exec_query_stats AS s1
  8. CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 ) x
  9. WHERE sql_statement NOT like 'SELECT * FROM(SELECT coalesce(object_name(s2.objectid)%'
  10. and OBJECTPROPERTYEX(x.objectid,'IsProcedure') = 1
  11. and exists (Select 1 from sys.procedures s
  12. where s.is_ms_shipped = 0
  13. and s.name = x.ProcName )
  14. ORDER BY execution_count DESC

Below is some sample output on my system.

proc            count           objectid        statement               last_execution_time
----------------------------------------------------------------------------------------------
usp_AddLast	42757230	517576882	UPDATE dbo.LastSome	2010-04-14 14:15:33.433
usp_AddLast	42757230	517576882	IF EXISTS(   SELECT 	2010-04-14 14:15:33.433
usp_Update	20290	        725577623	update t  set pclose	2010-04-14 14:15:33.433
usp_Update	20288	        725577623	update t  set pclose	2010-04-14 14:15:33.453
usp_GetLast3	3188	        501576825	SELECT distinct l.Sy	2010-04-14 14:14:33.350
usp_Historical	168	        1029578706	select * from Histor	2010-04-14 14:02:08.190
usp_AddLast	3	        517576882	INSERT dbo.Sometable	2010-04-07 08:42:57.040
usp_GetLast2	3	        965578478	SELECT l.Symbol,   q	2010-04-06 16:14:49.523
usp_Historical2	2	        1045578763	select SomeNumber,G	2010-04-14 12:10:48.860
usp_GetLast	1	        901578250	SELECT distinct l.Sy	2010-02-12 09:11:59.840

As you can see the first two rows are for the same stored procedure, what if you only want to know the procedure names? You can use the following query for that, I grouped them by name and then used the max count of the statement itself as the execution count, you could also use SUM instead of MAX. If you have a lot of if else conditions then max might not give you the whole picture.

  1. SELECT top 50 * FROM
  2.     (SELECT OBJECT_NAME(s2.objectid) AS ProcName,
  3.         MAX(execution_count) AS execution_count,s2.objectid,
  4.         MAX(last_execution_time) AS last_execution_time
  5. FROM sys.dm_exec_query_stats AS s1
  6. CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2
  7. GROUP BY OBJECT_NAME(s2.objectid),s2.objectid) x
  8. WHERE OBJECTPROPERTYEX(x.objectid,'IsProcedure') = 1
  9. AND EXISTS (SELECT 1 FROM sys.procedures s
  10.             WHERE s.is_ms_shipped = 0
  11.             AND s.name = x.ProcName )
  12. ORDER BY execution_count DESC

Here is the output from this query.

proc            count           objectid        last_execution_time
----------------------------------------------------------------------
usp_AddLast	42667941	517576882	2010-04-14 14:08:48.287
usp_Update	20263		725577623	2010-04-14 14:08:48.307
usp_GetLast3	3180		501576825	2010-04-14 14:07:10.513
usp_Historical	168		1029578706	2010-04-14 14:02:08.190
usp_GetLast2	3		965578478	2010-04-06 16:14:49.523
usp_Historical2	2		1045578763	2010-04-14 12:10:48.860
usp_GetLast	1		901578250	2010-02-12 09:11:59.840

There are some differences between this output and the output from above, this is because I ran the queries about 7 minutes apart so that is why the counts don't match

Contributed by: --SQLDenis 18:52, 14 April 2010 (GMT)

Part of SQL Server Admin Hacks

698 Rating: 4.0/5 (7 votes cast)