SQL Server 内存使用情况
• 查看设置的最大与最小内存:
exec sp_configure 'max server memory (MB)'
exec sp_configure 'min server memory (MB)'
• SqlServer目标内存、当前内存 、数据库内存页数:
SELECT object_name,counter_name,cntr_value,cntr_value/1024/1024 AS [cntr_value(GB)]
FROM sys.dm_os_performance_counters
WHERE counter_name in('Target Server Memory (KB)','Total Server Memory (KB)','Database pages')
• 查看设置的最大与最小内存:
SELECT * FROM sys.dm_os_sys_info
SELECT * FROM sys.dm_os_sys_memory
SELECT * FROM sys.dm_os_process_memory
• 查看当前是否有等待内存授予的进程:
Select a.*,b.text, c.query_plan
from sys.dm_exec_query_memory_grants a
CROSS APPLY sys.dm_exec_sql_text(sql_handle) b
CROSS APPLY sys.dm_exec_query_plan (plan_handle) c
• SqlServer预留和提交内存情况:
SELECT [type]
,SUM(virtual_memory_reserved_kb) AS [vm reserved]
,SUM(virtual_memory_committed_kb) AS [vm commited]
,SUM(awe_allocated_kb) AS [awe allocated]
,SUM(shared_memory_reserved_kb) AS [sm reserved]
,SUM(shared_memory_committed_kb) AS [sm committed]
,SUM(single_pages_kb) AS [Stolen in Buffer Pool]
,SUM(multi_pages_kb) AS [MemToLeave]
,SUM(single_pages_kb) + SUM(multi_pages_kb) AS [Stolen]
,SUM(virtual_memory_committed_kb) + SUM(multi_pages_kb) AS [Buffer Pool]
FROM sys.dm_os_memory_clerks
GROUP BY [type]
ORDER BY [type] desc
• 当前各数据库buffer pool的分配情况:
SELECT
CASE database_id
WHEN 32767 THEN 'ResourceDb'
ELSE db_name(database_id) END AS Database_name
,count(*) AS cached_pages_count
,count(*)*8/1024 AS cached_space_in_mb
,sum(convert(bigint,free_space_in_bytes))/1024/1024 AS free_space_in_mb
FROM sys.dm_os_buffer_descriptors(nolock)
GROUP BY db_name(database_id) ,database_id
ORDER BY cached_pages_count DESC;
• 当前数据库各表buffer pool的分配情况:
SELECT top(20) name ,index_id ,count(*)AS cached_pages_count
,count(*)*8/1024 AS cached_space_in_mb
FROM sys.dm_os_buffer_descriptors AS bd
INNER JOIN (
SELECT object_name(object_id) AS name ,index_id ,allocation_unit_id
FROM sys.allocation_units AS au INNER JOIN sys.partitions AS p
ON au.container_id = p.hobt_id AND (au.type = 1 OR au.type = 3 )
UNION ALL
SELECT object_name(object_id) AS name,index_id, allocation_unit_id
FROM sys.allocation_units AS au INNER JOIN sys.partitions AS p
ON au.container_id = p.hobt_id AND au.type = 2
) AS obj ON bd.allocation_unit_id = obj.allocation_unit_id
WHERE database_id = db_id() AND obj.name NOT LIKE 'sys%'
GROUP BY name, index_id
ORDER BY cached_pages_count DESC
• 当前各表或索引在缓冲池中的页数量及大小:
SELECT obj.name ,obj.index_id,i.name ,count(*)AS cached_pages_count ,count(*)*8/1024 AS cached_space_in_mb
FROM sys.dm_os_buffer_descriptors AS bd
INNER JOIN (
SELECT object_name(object_id) AS name,object_id,index_id ,allocation_unit_id
FROM sys.allocation_units AS au
INNER JOIN sys.partitions AS p ON au.container_id = p.hobt_id AND (au.type = 1 OR au.type = 3)
UNION ALL
SELECT object_name(object_id) AS name,object_id,index_id, allocation_unit_id
FROM sys.allocation_units AS au
INNER JOIN sys.partitions AS p ON au.container_id = p.partition_id AND au.type = 2
) AS obj ON bd.allocation_unit_id = obj.allocation_unit_id
LEFT JOIN sys.indexes i(nolock) on obj.object_id = i.object_id AND obj.index_id = i.index_id
WHERE database_id = db_id() AND obj.name NOT LIKE 'sys%'
GROUP BY obj.name ,obj.index_id,i.name
ORDER BY cached_pages_count DESC;
• 统计当前内存分配情况 :
select
physical_memory_in_bytes / 1024 / 1024 as physical_memory_mb,
virtual_memory_in_bytes / 1024 / 1024 as virtual_memory_mb,
bpool_committed * 8 / 1024 as bpool_committed_mb,
bpool_commit_target * 8 / 1024 as bpool_target_mb,
bpool_visible * 8 / 1024 as bpool_visible_mb
from sys.dm_os_sys_info
• 当前内存脏页数量及大小 :
SELECT db_name(database_id) AS 'Database'
,count(page_id) AS 'Dirty Pages'
,count(page_id)*8/1024 AS 'Dirty Pages(MB)'
FROM sys.dm_os_buffer_descriptors(nolock)
WHERE is_modified =1
GROUP BY db_name(database_id)
ORDER BY 'Dirty Pages' DESC
• 缓存类型数量大小 :
select cacheobjtype as [Cached Type]
,COUNT(*) [Number of Plans]
,SUM(CONVERT(BIGINT,size_in_bytes))/1024/1024 [Plan Cache Size(MB)]
from sys.dm_exec_cached_plans
group by cacheobjtype
order by [Plan Cache Size(MB)] desc
• 缓存对象数量大小 :
select objtype as [Cached Object Type]
,COUNT(*) as [Number of Plans]
,SUM(CONVERT(BIGINT,size_in_bytes))/1024/1024 [Plan Cache Size(MB)]
,avg(usecounts) [Avg Use Count]
from sys.dm_exec_cached_plans
group by objtype
order by [Plan Cache Size(MB)] desc
• 内存对象 :
SELECT top 10 * FROM sys.dm_os_memory_cache_counters(nolock)
ORDER BY entries_count DESC; SELECT * FROM sys.dm_os_memory_clerks a
WHERE name='TokenAndPermUserStore' SELECT * FROM sys.dm_os_memory_cache_entries(nolock)
WHERE [type]='USERSTORE_TOKENPERM' and name='TokenAndPermUserStore'
• 缓存分析对象 :
SELECT * FROM sys.syscacheobjects
SELECT * FROM sys.dm_exec_cached_plans
• 即时查询优化(运行即生效) :
EXEC sp_configure 'show advanced options',1
RECONFIGURE
EXEC sp_configure 'optimize for ad hoc workloads',1
RECONFIGURE
all code :
--查看设置的最大最小每次
exec sp_configure 'max server memory (MB)'
exec sp_configure 'min server memory (MB)' --SqlServer目标内存、当前内存 、数据库内存页数
SELECT object_name,counter_name,cntr_value,cntr_value/1024/1024 AS [cntr_value(GB)]
FROM sys.dm_os_performance_counters
WHERE counter_name in('Target Server Memory (KB)','Total Server Memory (KB)','Database pages') --系统物理内存使用情况
SELECT * FROM sys.dm_os_sys_info
SELECT * FROM sys.dm_os_sys_memory
SELECT * FROM sys.dm_os_process_memory --查看当前是否有等待内存授予的进程
Select a.*,b.text, c.query_plan
from sys.dm_exec_query_memory_grants a
CROSS APPLY sys.dm_exec_sql_text(sql_handle) b
CROSS APPLY sys.dm_exec_query_plan (plan_handle) c --SqlServer预留和提交内存情况
DBCC MEMORYSTATUS; --旧的方法 SELECT [type]
,SUM(virtual_memory_reserved_kb) AS [vm reserved]
,SUM(virtual_memory_committed_kb) AS [vm commited]
,SUM(awe_allocated_kb) AS [awe allocated]
,SUM(shared_memory_reserved_kb) AS [sm reserved]
,SUM(shared_memory_committed_kb) AS [sm committed]
,SUM(single_pages_kb) AS [Stolen in Buffer Pool]
,SUM(multi_pages_kb) AS [MemToLeave]
,SUM(single_pages_kb) + SUM(multi_pages_kb) AS [Stolen]
,SUM(virtual_memory_committed_kb) + SUM(multi_pages_kb) AS [Buffer Pool]
FROM sys.dm_os_memory_clerks
GROUP BY [type]
ORDER BY [type] desc --当前各数据库buffer pool的分配情况
SELECT
CASE database_id
WHEN 32767 THEN 'ResourceDb'
ELSE db_name(database_id) END AS Database_name
,count(*) AS cached_pages_count
,count(*)*8/1024 AS cached_space_in_mb
,sum(convert(bigint,free_space_in_bytes))/1024/1024 AS free_space_in_mb
FROM sys.dm_os_buffer_descriptors(nolock)
GROUP BY db_name(database_id) ,database_id
ORDER BY cached_pages_count DESC; --当前数据库各表buffer pool的分配情况
SELECT top(20) name ,index_id ,count(*)AS cached_pages_count
,count(*)*8/1024 AS cached_space_in_mb
FROM sys.dm_os_buffer_descriptors AS bd
INNER JOIN (
SELECT object_name(object_id) AS name ,index_id ,allocation_unit_id
FROM sys.allocation_units AS au INNER JOIN sys.partitions AS p
ON au.container_id = p.hobt_id AND (au.type = 1 OR au.type = 3 )
UNION ALL
SELECT object_name(object_id) AS name,index_id, allocation_unit_id
FROM sys.allocation_units AS au INNER JOIN sys.partitions AS p
ON au.container_id = p.hobt_id AND au.type = 2
) AS obj ON bd.allocation_unit_id = obj.allocation_unit_id
WHERE database_id = db_id() AND obj.name NOT LIKE 'sys%'
GROUP BY name, index_id
ORDER BY cached_pages_count DESC -- 当前各表或索引在缓冲池中的页数量及大小
SELECT obj.name ,obj.index_id,i.name ,count(*)AS cached_pages_count ,count(*)*8/1024 AS cached_space_in_mb
FROM sys.dm_os_buffer_descriptors AS bd
INNER JOIN (
SELECT object_name(object_id) AS name,object_id,index_id ,allocation_unit_id
FROM sys.allocation_units AS au
INNER JOIN sys.partitions AS p ON au.container_id = p.hobt_id AND (au.type = 1 OR au.type = 3)
UNION ALL
SELECT object_name(object_id) AS name,object_id,index_id, allocation_unit_id
FROM sys.allocation_units AS au
INNER JOIN sys.partitions AS p ON au.container_id = p.partition_id AND au.type = 2
) AS obj ON bd.allocation_unit_id = obj.allocation_unit_id
LEFT JOIN sys.indexes i(nolock) on obj.object_id = i.object_id AND obj.index_id = i.index_id
WHERE database_id = db_id() AND obj.name NOT LIKE 'sys%'
GROUP BY obj.name ,obj.index_id,i.name
ORDER BY cached_pages_count DESC; -- 统计当前内存分配情况
select
physical_memory_in_bytes / 1024 / 1024 as physical_memory_mb,
virtual_memory_in_bytes / 1024 / 1024 as virtual_memory_mb,
bpool_committed * 8 / 1024 as bpool_committed_mb,
bpool_commit_target * 8 / 1024 as bpool_target_mb,
bpool_visible * 8 / 1024 as bpool_visible_mb
from sys.dm_os_sys_info --当前内存脏页数量及大小
SELECT db_name(database_id) AS 'Database'
,count(page_id) AS 'Dirty Pages'
,count(page_id)*8/1024 AS 'Dirty Pages(MB)'
FROM sys.dm_os_buffer_descriptors(nolock)
WHERE is_modified =1
GROUP BY db_name(database_id)
ORDER BY 'Dirty Pages' DESC --缓存类型数量大小
select cacheobjtype as [Cached Type]
,COUNT(*) [Number of Plans]
,SUM(CONVERT(BIGINT,size_in_bytes))/1024/1024 [Plan Cache Size(MB)]
from sys.dm_exec_cached_plans
group by cacheobjtype
order by [Plan Cache Size(MB)] desc --缓存对象数量大小
select objtype as [Cached Object Type]
,COUNT(*) as [Number of Plans]
,SUM(CONVERT(BIGINT,size_in_bytes))/1024/1024 [Plan Cache Size(MB)]
,avg(usecounts) [Avg Use Count]
from sys.dm_exec_cached_plans
group by objtype
order by [Plan Cache Size(MB)] desc --内存对象
SELECT top 10 * FROM sys.dm_os_memory_cache_counters(nolock)
ORDER BY entries_count DESC; SELECT * FROM sys.dm_os_memory_clerks a
WHERE name='TokenAndPermUserStore' SELECT * FROM sys.dm_os_memory_cache_entries(nolock)
WHERE [type]='USERSTORE_TOKENPERM' and name='TokenAndPermUserStore' --缓存分析对象
SELECT * FROM sys.syscacheobjects
SELECT * FROM sys.dm_exec_cached_plans --即时查询优化(运行即生效)
EXEC sp_configure 'show advanced options',1
RECONFIGURE
EXEC sp_configure 'optimize for ad hoc workloads',1
RECONFIGURE
SQL Server 内存使用情况的更多相关文章
- sql server内存使用情况
查看Sql Server 数据库的内存使用情况 转自:https://www.cnblogs.com/wanghao4023030/p/8299478.html DBCC MemoryStatu ...
- SQL Server内存遭遇操作系统进程压榨案例
场景: 最近一台DB服务器偶尔出现CPU报警,我的邮件报警阈(请读yù)值设置的是15%,开始时没当回事,以为是有什么统计类的查询,后来越来越频繁. 探索: 我决定来查一下,究竟是什么在作怪,我排查的 ...
- SQL SERVER 内存分配及常见内存问题 简介
一.问题: 1.SQL Server 所占用内存数量从启动以后就不断地增加: 首先,作为成熟的产品,内存溢出的机会微乎其微.对此要了解SQL SERVER与windows是如何协调.共享内存.并且SQ ...
- SQL SERVER 内存分配及常见内存问题(1)——简介
原文:SQL SERVER 内存分配及常见内存问题(1)--简介 一.问题: 1.SQL Server 所占用内存数量从启动以后就不断地增加: 首先,作为成熟的产品,内存溢出的机会微乎其微.对此要了解 ...
- (转)SQL Server内存遭遇操作系统进程压榨案例
原文地址:http://www.cnblogs.com/zc_0101/p/3592259.html 场景: 最近一台DB服务器偶尔出现CPU报警,我的邮件报警阈(请读yù)值设置的是15%,开始时没 ...
- Sql Server 内存相关计数器以及内存压力诊断
在数据库服务器中,内存是数据库对外提供服务最重要的资源之一, 不仅仅是Sql Server,包括其他数据库,比如Oracle,MySQL等,都是一类非常喜欢内存的应用. 在Sql Server服务器中 ...
- SQL SERVER 内存学习系列(二)-DMV查看内存信息
内存管理在SQL Server中有一个三级结构.底部是内存节点,这是最低级的分配器,用于SQL Server的内存.第二个层次是由内存Clerk组成,这是用来访问内存节点和缓存存储,缓存存储则用于缓存 ...
- SQL SERVER 内存学习系列(一)
最近帮客户解决发布订阅的问题时,突然遇到这样一个问题发布订阅中报下面的错误,另外执行alter table 操作时也会报错 : 问题很奇怪发布订阅和CLR有什么关系?memtoleave内存是个啥?回 ...
- 人人都是 DBA(IV)SQL Server 内存管理
SQL Server 的内存管理是一个庞大的主题,涉及特别多的概念和技术,例如常见的 Plan Cache.Buffer Pool.Memory Clerks 等.本文仅是管中窥豹,描述常见的内存管理 ...
随机推荐
- 深入理解DIP、IoC、DI以及IoC容器(转载)
<转载的这个up的其他的文章也很nice> 这几个词第一眼看,懵逼,第二眼看,更特么懵逼..... 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序. 其中,OOD有一 ...
- MySQL 5.6 Reference Manual-14.6 InnoDB Table Management
14.6 InnoDB Table Management 14.6.1 Creating InnoDB Tables 14.6.2 Moving or Copying InnoDB Tables to ...
- 【Oracle】使用logmnr挖掘日志
使用Logmnr挖掘日志的详细步骤如下: 1. 查看日志状态及位置 col member for a45 select group#,status from v$log; select group#, ...
- 三维地图中的A*寻路
跟二维地图原理一样,只不过搜索方向多了,二维只搜8个方向,而三维要搜26个方向. 不懂的看我以前写的文章,这里直接贴代码: #include <iostream> #include < ...
- Zabbix系列-REHL6.10离线方式安装Zabbix 4.0 LTS
环境 第零步:关闭系统默认防火墙 setenforce 0 sed -i -r "/^SELINUX=/c SELINUX=disabled" /etc/selinux/confi ...
- php libevent扩展
Libevent 是一个用C语言编写的.轻量级的开源高性能网络库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大: 源代码相当精炼. ...
- 路飞学城Python-Day96
51-数据库表关系之一对多 为什么有多表的概念? 一个Book有单表的时候只能查询Book表中的内容,要想查询更多的出版社的信息,需要再将单表的内容不断更新 导致单表会存储大量的重复的信息,浪费了大量 ...
- linux 结构需要清理 (structure needs cleaning)
下面操作会删除挂载点所有文件,注意备份. df -T 查看出错的挂载点对应的文件系统和文件系统类型 然后umount这个文件系统 umount /dev/sda1 然后文件系统类型不同操作不同 ...
- vue 如何清除定时器
在页面中需要定时刷新局部数据,在数据变化是否频繁的情况下,没有必要使用webSocket,因为数据变化频繁,数据实时变化太快看不清楚.因此页面会定时调用后台接口以达到实时刷新数据的效果. 1.在dat ...
- Callable与Futrue创建线程
接口callable <V> 类型参数 V-call方法的结构类型 public interface Callable<V> 返回结果并且可能抛出的异常的任务.实现者定义一 ...