RESOURCE_SEMAPHORE Waittype

1, 查看今天的Package执行情况,发现一个Package运行时间过长,为了查看当前DB中正在运行的SQL 语句,使用以下脚本

select    r.session_id,r.blocking_session_id,
        r.request_id,r.start_time,r.status,r.command,
        st.dbid,st.objectid,st.text as SqlStatement,
        SUBSTRING (st.text,
                    r.statement_start_offset/2,
                    (CASE WHEN r.statement_end_offset = -1
                            THEN LEN(CONVERT(NVARCHAR(MAX), st.text)) * 2
                        ELSE r.statement_end_offset END
                        - r.statement_start_offset)/2
                    ) as IndividualQueryStatement,
        r.database_id,r.user_id,r.connection_id,
        r.wait_type,r.wait_time,r.last_wait_type,r.wait_resource,r.open_transaction_count,
        r.percent_complete,r.estimated_completion_time,
        r.cpu_time,r.total_elapsed_time,r.reads,r.writes,r.logical_reads,
        r.transaction_isolation_level,r.lock_timeout,r.deadlock_priority,r.row_count,
        r.granted_query_memory,p.query_plan
from sys.dm_exec_requests r
outer APPLY sys.dm_exec_sql_text(r.sql_handle) as st
outer apply sys.dm_exec_query_plan(r.plan_handle) as p
where r.last_wait_type<>‘MISCELLANEOUS‘
    and r.session_id>50

发现一个SPID运行时间非常长,其last_wait_type = RESOURCE_SEMAPHORE

RESOURCE_SEMAPHORE Wait的含义是:Used to indicate a worker is waiting to be allowed to perform an operation requiring "query memory" such as hashes and sorts .
High wait times indicate too many queries are running concurrently that require query memory. Operations requiring query memory are hashes and sorts. Use DMVs such as dm_exec_query_resource_semaphores and dm_exec_query_memory_grants

引用:Troubleshooting SQL Server RESOURCE_SEMAPHORE Waittype Memory Issues

https://www.mssqltips.com/sqlservertip/2827/troubleshooting-sql-server-resourcesemaphore-waittype-memory-issues/

Resource Semaphore Wait

Before moving on, I would like to shed some light on the Resource Semaphore wait so you can better understand how memory is granted to SQL Server queries.

When SQL Server receives a user query, it first creates a complied plan, then an execution plan is created based on the complied plan. When SQL Server creates a complied plan it calculates two memory grant parameters called "required memory" and "additional memory". Required memory is the minimum memory needed to run a sort and hash join. It is known as required because a query would not start without this memory available. Additional memory is the amount of memory needed to store temporary rows in memory. This is known as additional because a query can be stored on disk if there is not enough memory available.

First, the server calculates how much memory is needed for any given query to execute. This is generally the sum of required memory and additional memory, but if your instance is using parallelism then the needed memory would be (required memory * DOP) + additional memory. The server checks if the needed memory exceeds the per query limit, then the server reduces additional memory until the total fits within the limit. This revised size is called requested memory. There is an internal facility within SQL Server known as RESOURCE SEMAPHORE which is used to grant this requested memory to a query. If query is not able to be granted this requested memory by a Resource Semaphore, then that query will be in a waiting state with a RESOURCE_SEMAPHORE wait type if you query the sysprocesses system table or sys.dm_exec_request DMV.

When a Resource Semaphore receives a new request, it first checks if any query is waiting or not. If it finds one, it puts the new query in the queue because the wait queue is designed on a first-come-first-served basis with a small weighting to favor small queries. Resource Semaphore attempts to grant memory when there is no waiting query or when a query returns reserved memory. If it finds enough memory, then the requested memory is granted and the query can start running and if it does not find enough free memory to grant the requested memory then it puts the current query into the waiting queue with a RESOURCE_SEMAPHORE wait type and your server starts facing memory pressure.

2,使用sys.dm_exec_query_memory_grants  和 sys.dm_exec_query_resource_semaphores 查看memory grant更详细的信息

出现RESOURCE_SEMAPHORE Waittype的原因是不能得到请求的内存,导致查询语句没有执行下去,表明server存在内存压力。在sql server中,sort和hash join是非常消耗资源的两个操作,优化这两个查询可以缓解内存压力。

参考文档:

https://www.mssqltips.com/sqlservertip/2827/troubleshooting-sql-server-resourcesemaphore-waittype-memory-issues/

https://msdn.microsoft.com/en-us/library/ms365393.aspx
https://msdn.microsoft.com/zh-cn/library/ms366321(v=sql.120).aspx

时间: 2024-10-24 12:39:59

RESOURCE_SEMAPHORE Waittype的相关文章

根据WaitType诊断故障

在查询执行时,等待次数和等待时间在一定程度上指示查询的瓶颈,甚至非常有助于对系统进行诊断.偶尔一次的异常等待,不足以表明系统存在瓶颈,但是,SQL Server实例经常出现特定的等待类型,并且等待时间趋于增加,这就说明,系统存在压力,或内存,或IO等,根据WaitType对系统进行监控和诊断,还能对查询进行性能调优,例如,Lock等待表明查询存在数据竞争,PageIOLatch等待表明IO响应缓慢,PageLatch等待表明文件的布局需要改进等. 一,资源信号(RESOURCE SEMAPHOR

高老大 ‘SQL Server 优化器特性导致的内存授予相关BUG’ 学习笔记

今天高老大出了好文章.在这里 自己本来对这一块比较混乱,正好借这个机会学习一下. 就用高老大的脚本.需要的直接去他那里找吧,这里就省了. 加查询优化标记前后对比 可以看到GrantedMemory是504928KB,大约是213096/1024=208.101562MB(这里的那个值好像每次都有差别,但是差距不太大.不影响效果) 加上跟踪标记后 4560/1024大概只有4MB吧. 差别很大吧~ 其实这里这些值还可能通过查询计划的XML里看,里面有更详细的信息. 如 <QueryPlan Deg

在SQL Server 2012中,对存在ColumnStore Index的Table进行查询,出现WaitType:HTMEMO 和 HTBUILD

DW中有两个表,LittleTable和BigTable,BigTable上创建了Nonclustered ColumnStore Index,对着两个表进行查询,如下面的Scirpt.在查询时,出现WaitType:HTMEMO 和 HTBUILD,查询速度极其慢,十几个小时都不能出结果. select a few columns,,, from dbo.LittleTable lt with(nolock) inner join dbo.BitTable bt with(nolock) on

SQLServer RESOURCE_SEMAPHORE 等待状态

原文:SQLServer RESOURCE_SEMAPHORE 等待状态 概述: 当一个SQLServer实例运行得很慢的时候,应该做一些检查,如检查等待状态.最好的方法是一开始就建立一个性能基线,以便做性能对比.当发现与性能基线对比后,存在内存压力的话,就要找出是什么原因导致的.可以检查事务的等待状态,其中Resource_semaphore等待可能出现最多.下面是如何去处理这个问题: 当检查事务的所有等待类型后,可能会发现Resource_semaphore这个等待类型出现非常多,这会增加一

WaitType:ASYNC_NETWORK_IO

官方文档的定义,是指SQL Server 产生的结果集需要经过Network传递到Client,Network不能很快将结果集传输到Client,导致结果集仍然驻留在SQL Server的Session中,可能的原因是结果集非常大,或者Network带宽小,传输慢. ASYNC_NETWORK_IO:Occurs on network writes when the task is blocked behind the network. Verify that the client is pro

RESOURCE_SEMAPHORE类型等待

通过查询动态管理视图sys.dm_exec_query_resource_semaphores得到正在发生的等待的数目 SELECT waiter_count, timeout_error_count, forced_grant_count FROM sys.dm_exec_query_resource_semaphores waiter_count表示正在等待内存授予以继续执行的任务的数目.timeout_error_count表示从服务器最近一次启动以来发生等待内存授予超时的数目.force

系统Session的WaitType

在一个时间点,从Server上查询到的系统Session的Wait type 截图 1,CHECKPOINT_QUEUE Occurs while the checkpoint task is waiting for the next checkpoint request. Description:This wait type is when the background checkpoint process is idle waiting to be signaled that a new c

WaitType:ASYNC_IO_COMPLETION

项目组有一个数据库备份的Job运行异常,该Job将备份数据存储到remote server上,平时5个小时就能完成的备份操作,现在运行19个小时还没有完成,backup命令的Wait type是 ASYNC_IO_COMPLETION: 根据MSDN 官方文档的定义:Occurs when a task is waiting for asynchronous I/O operations to finish. 该等待类型表示:Task在等待异步IO操作的完成.进程申请了一个IO操作,但是系统(D

WaitType:ASYNC

项目组有一个数据库备份的Job运行异常,该Job将备份数据存储到remote server上,平时5个小时就能完成的备份操作,现在运行19个小时还没有完成,backup命令的Wait type是 ASYNC_IO_COMPLETION: 根据MSDN 官方文档的定义:Occurs when a task is waiting for asynchronous I/O operations to finish. 该等待类型表示:Task在等待异步IO操作的完成.进程申请了一个IO操作,但是系统(D