Detecting SQL Server Utilization with a query

Posted on

Question :

My current project will send queries to an sql server constantly and It may use 100% of the memory or CPU.

  • How can I check if the server is nearing full utilization in a stored procedure so that I can decide whether to execute the queries or not or save some settings in a table so that the following queries can know the workload is high and decide what to do

  • If not, how can I prevent SQL server to reach full utilization?

More information about the case:
Right now I know our current test server can process 40-50 queries per second (one specific stored procedure). And now we’ll decide how many queries are sent to the server every second. If we set the amount even 1 higher than the expected, in the long run, the queries will eventually fill the virtual memory and out client will have to restart their sql server instance periodically.

Expected Results (For bounty hunters):

@memory_usage float, @cpu_usage float; /* in percentage */

Any ideas are welcomed. Thanks.

Answer :

If you really want to limit SQL Server memory, look at the Maximum server memory option.

Getting memory usage is possible, but it depends on what you really want. Do you want to see memory usage as a percent of the “Maximum server memory” option? If so, look at sys.dm_os_process_memory:

select convert(float, memory_utilization_percentage) / 100 as memory_usage
from sys.dm_os_process_memory

If you want physical memory use vs. total system memory, look in both sys.dm_os_process_memory and sys.dm_os_sys_info:

select * from sys.dm_os_sys_info

declare @physicalMemoryInUseKB bigint
declare @totalSystemMemoryBytes bigint

select @physicalMemoryInUseKB = physical_memory_in_use_kb from sys.dm_os_process_memory
select @totalSystemMemoryBytes = physical_memory_in_bytes from sys.dm_os_sys_info
select convert(float, @physicalMemoryInUseKB) * 1024
       / convert(float, @totalSystemMemoryBytes) as memory_usage

I don’t think you can get the CPU usage unless you have the Resource Governor enabled. If you do, look at sys.dm_os_performance_counters:

select
    case CPUUsageBase
        when 0 then 0
        else convert(float, CPUUsage) / convert(float, CPUUsageBase)
    end as 'cpu_usage'
from
(select cntr_value as [CPUUsage]
from sys.dm_os_performance_counters
where counter_name like 'CPU usage%'
and object_name like '%Workload Group Stats%'
and cntr_type = 537003264) Usage
cross join
(select cntr_value as [CPUUsageBase]
 from sys.dm_os_performance_counters
 where counter_name like 'CPU usage %'
 and object_name like '%Workload Group Stats%'
 and cntr_type = 1073939712) UsageBase

DMV’s provide both cpu and memory usage information. These queries require the VIEW_SERVER_STATE permission to run.

DECLARE @memory_usage FLOAT
    , @cpu_usage FLOAT

SET @memory_usage = ( SELECT    1.0 - ( available_physical_memory_kb / ( total_physical_memory_kb * 1.0 ) ) memory_usage
                        FROM      sys.dm_os_sys_memory
                    )

SET @cpu_usage = ( SELECT TOP ( 1 )
                            [CPU] / 100.0 AS [CPU_usage]
                    FROM     ( SELECT    record.value('(./Record/@id)[1]', 'int') AS record_id
                                        , record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS [CPU]
                                FROM      ( SELECT    [timestamp]
                                                    , CONVERT(XML, record) AS [record]
                                            FROM      sys.dm_os_ring_buffers WITH ( NOLOCK )
                                            WHERE     ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
                                                    AND record LIKE N'%<SystemHealth>%'
                                        ) AS x
                            ) AS y
                    ORDER BY record_id DESC
                    )


SELECT  @memory_usage [memory_usage]
        , @cpu_usage [cpu_usage]

Within a stored procedure? You won’t be able to as once the query which was causing the CPU to go to 100% is finished you can’t monitor for that metric anymore which would be done by the next query within the stored procedure.

If you are using SQL Server 2008 or higher (and Enterprise Edition) you can use the resource governor to ensure that other processes have CPU power and memory available to them.

Leave a Reply

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