sqlskills-logo-2015-white.png

SQL Server Diagnostic Information Queries Detailed, Day 22

For Day 22 of this series, we start out with Query #50, which is SP Logical Reads. This query retrieves information from the sys.procedures object catalog view and the sys.dm_exec_procedure_stats dynamic management view about the cached stored procedures that have the highest cumulative total logical reads in the current database. Query #50 is shown in Figure 1.

   1: -- Top Cached SPs By Total Logical Reads. Logical reads relate to memory pressure  (Query 50) (SP Logical Reads)

   2: SELECT TOP(25) p.name AS [SP Name], qs.total_logical_reads AS [TotalLogicalReads], 

   3: qs.total_logical_reads/qs.execution_count AS [AvgLogicalReads],qs.execution_count, 

   4: ISNULL(qs.execution_count/DATEDIFF(Minute, qs.cached_time, GETDATE()), 0) AS [Calls/Minute], 

   5: qs.total_elapsed_time, qs.total_elapsed_time/qs.execution_count 

   6: AS [avg_elapsed_time], qs.cached_time

   7: FROM sys.procedures AS p WITH (NOLOCK)

   8: INNER JOIN sys.dm_exec_procedure_stats AS qs WITH (NOLOCK)

   9: ON p.[object_id] = qs.[object_id]

  10: WHERE qs.database_id = DB_ID()

  11: ORDER BY qs.total_logical_reads DESC OPTION (RECOMPILE);

  12:  

  13: -- This helps you find the most expensive cached stored procedures from a memory perspective

  14: -- You should look at this if you see signs of memory pressure

Figure 1: Query #50 SP Logical Reads

This query lets you see which cached stored procedures have the highest number cumulative logical reads in this database. This helps you see which stored procedures are causing the most memory pressure (and indirectly, read I/O pressure) for this database. If you are seeing any signs of memory pressure on your instance of SQL Server (and if this database is using a lot of memory in the SQL Server Buffer pool, as shown in Query #32), then the results of this query can help you figure out which stored procedures are the biggest offenders.

 

Query #51 is SP Physical Reads. This query retrieves information from the sys.procedures object catalog view and the sys.dm_exec_procedure_stats dynamic management view about the cached stored procedures that have the highest cumulative total physical reads in the current database. Query #51 is shown in Figure 2.

   1: -- Top Cached SPs By Total Physical Reads. Physical reads relate to disk read I/O pressure  (Query 51) (SP Physical Reads)

   2: SELECT TOP(25) p.name AS [SP Name],qs.total_physical_reads AS [TotalPhysicalReads], 

   3: qs.total_physical_reads/qs.execution_count AS [AvgPhysicalReads], qs.execution_count, 

   4: qs.total_logical_reads,qs.total_elapsed_time, qs.total_elapsed_time/qs.execution_count 

   5: AS [avg_elapsed_time], qs.cached_time 

   6: FROM sys.procedures AS p WITH (NOLOCK)

   7: INNER JOIN sys.dm_exec_procedure_stats AS qs WITH (NOLOCK)

   8: ON p.[object_id] = qs.[object_id]

   9: WHERE qs.database_id = DB_ID()

  10: AND qs.total_physical_reads > 0

  11: ORDER BY qs.total_physical_reads DESC, qs.total_logical_reads DESC OPTION (RECOMPILE);

  12:  

  13: -- This helps you find the most expensive cached stored procedures from a read I/O perspective

  14: -- You should look at this if you see signs of I/O pressure or of memory pressure

Figure 2: Query #51 SP Physical Reads

This query lets you see which cached stored procedures have the highest number cumulative physical reads in this database. This helps you see which stored procedures are causing the most read I/O pressure (and indirectly, memory pressure) for this database. If you are seeing any signs of high read I/O latency on your instance of SQL Server (and if this database is causing a lot of I/O activity, as shown in Query #31), then the results of this query can help you figure out which stored procedures are the biggest offenders.

Leave a Reply

Your email address will not be published. Required fields are marked *

Other articles

Imagine feeling confident enough to handle whatever your database throws at you.

With training and consulting from SQLskills, you’ll be able to solve big problems, elevate your team’s capacity, and take control of your data career.