人人都是 DBA(X)资源信息收集脚本汇编 - Dennis Gao
标签:
人人
dba
资源
| 发表时间:2014-12-10 12:31 | 作者:Dennis Gao
出处:
什么?有个 SQL 执行了 8 秒!
哪里出了问题?臣妾不知道啊,得找 DBA 啊。
DBA 人呢?离职了!!擦!!!
程序员在无处寻求帮助时,就得想办法自救,努力让自己变成 "伪 DBA"。
索引
- 获取数据库的 CPU 使用率
- 过去一段时间里 CPU 利用率的历史情况
- 谁用 CPU 工作的时间最长
- 服务器上安装了多大的 Memory
- SQL Server 进程用了多少 Memory
- 是否申请新的 Memory 无法得到
- SQL Server 的最大最小 Memory 配置
- 通过 Signal Wait 判断是否 CPU 压力过大
- 获取数据库的 Buffer 使用率
- 查看哪张表占用的 Buffer 最多
- 查看 Memory Clerks 使用情况
- 查看 Memory 分配状况
- 查询 SQL Server 内存承担的压力
- 查询 SQL Server 性能计数器
- 查询当前的 Batch Requests 计数
WITH DB_CPU_Stats
AS (
SELECT DatabaseID
,DB_Name(DatabaseID) AS [Database Name]
,SUM(total_worker_time) AS [CPU_Time_Ms]
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY (
SELECT CONVERT(INT, value) AS [DatabaseID]
FROM sys.dm_exec_plan_attributes(qs.plan_handle)
WHERE attribute = N'dbid'
) AS F_DB
GROUP BY DatabaseID
)
SELECT ROW_NUMBER() OVER (
ORDER BY [CPU_Time_Ms] DESC
) AS [CPU Rank]
,[Database Name]
,[CPU_Time_Ms] AS [CPU Time (ms)]
,CAST([CPU_Time_Ms] * 1.0 / SUM([CPU_Time_Ms]) OVER () * 100.0 AS DECIMAL(5, 2)) AS [CPU Percent]
FROM DB_CPU_Stats
WHERE DatabaseID <> 32767 -- ResourceDB
ORDER BY [CPU Rank]
OPTION (RECOMPILE);
AS (
SELECT DatabaseID
,DB_Name(DatabaseID) AS [Database Name]
,SUM(total_worker_time) AS [CPU_Time_Ms]
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY (
SELECT CONVERT(INT, value) AS [DatabaseID]
FROM sys.dm_exec_plan_attributes(qs.plan_handle)
WHERE attribute = N'dbid'
) AS F_DB
GROUP BY DatabaseID
)
SELECT ROW_NUMBER() OVER (
ORDER BY [CPU_Time_Ms] DESC
) AS [CPU Rank]
,[Database Name]
,[CPU_Time_Ms] AS [CPU Time (ms)]
,CAST([CPU_Time_Ms] * 1.0 / SUM([CPU_Time_Ms]) OVER () * 100.0 AS DECIMAL(5, 2)) AS [CPU Percent]
FROM DB_CPU_Stats
WHERE DatabaseID <> 32767 -- ResourceDB
ORDER BY [CPU Rank]
OPTION (RECOMPILE);
DECLARE @ts_now BIGINT = (
SELECT cpu_ticks / (cpu_ticks / ms_ticks)
FROM sys.dm_os_sys_info WITH (NOLOCK)
);
SELECT TOP (256) SQLProcessUtilization AS [SQL Server Process CPU Utilization]
,SystemIdle AS [System Idle Process]
,100 - SystemIdle - SQLProcessUtilization AS [Other Process CPU Utilization]
,DATEADD(ms, - 1 * (@ts_now - [timestamp]), GETDATE()) AS [Event Time]
FROM (
SELECT record.value('(./Record/@id)[1]', 'int') AS record_id
,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS [SystemIdle]
,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS [SQLProcessUtilization]
,[timestamp]
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
OPTION (RECOMPILE);
SELECT cpu_ticks / (cpu_ticks / ms_ticks)
FROM sys.dm_os_sys_info WITH (NOLOCK)
);
SELECT TOP (256) SQLProcessUtilization AS [SQL Server Process CPU Utilization]
,SystemIdle AS [System Idle Process]
,100 - SystemIdle - SQLProcessUtilization AS [Other Process CPU Utilization]
,DATEADD(ms, - 1 * (@ts_now - [timestamp]), GETDATE()) AS [Event Time]
FROM (
SELECT record.value('(./Record/@id)[1]', 'int') AS record_id
,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS [SystemIdle]
,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS [SQLProcessUtilization]
,[timestamp]
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
OPTION (RECOMPILE);
可以查看那个时间点的 CPU 利用率较高。
SELECT TOP (50) DB_NAME(t.[dbid]) AS [Database Name]
,t.[text] AS [Query Text]
,qs.total_worker_time AS [Total Worker Time]
,qs.min_worker_time AS [Min Worker Time]
,qs.total_worker_time / qs.execution_count AS [Avg Worker Time]
,qs.max_worker_time AS [Max Worker Time]
,qs.execution_count AS [Execution Count]
,qs.total_elapsed_time / qs.execution_count AS [Avg Elapsed Time]
,qs.total_logical_reads / qs.execution_count AS [Avg Logical Reads]
,qs.total_physical_reads / qs.execution_count AS [Avg Physical Reads]
,qp.query_plan AS [Query Plan]
,qs.creation_time AS [Creation Time]
FROM sys.dm_exec_query_stats AS qs WITH (NOLOCK)
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS t
CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS qp
ORDER BY qs.total_worker_time DESC
OPTION (RECOMPILE);
,t.[text] AS [Query Text]
,qs.total_worker_time AS [Total Worker Time]
,qs.min_worker_time AS [Min Worker Time]
,qs.total_worker_time / qs.execution_count AS [Avg Worker Time]
,qs.max_worker_time AS [Max Worker Time]
,qs.execution_count AS [Execution Count]
,qs.total_elapsed_time / qs.execution_count AS [Avg Elapsed Time]
,qs.total_logical_reads / qs.execution_count AS [Avg Logical Reads]
,qs.total_physical_reads / qs.execution_count AS [Avg Physical Reads]
,qp.query_plan AS [Query Plan]
,qs.creation_time AS [Creation Time]
FROM sys.dm_exec_query_stats AS qs WITH (NOLOCK)
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS t
CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS qp
ORDER BY qs.total_worker_time DESC
OPTION (RECOMPILE);
或许能找到哪个 SQL 语句占用了最多的 CPU 资源。
SELECT total_physical_memory_kb / 1024 AS [Physical Memory (MB)]
,available_physical_memory_kb / 1024 AS [Available Memory (MB)]
,total_page_file_kb / 1024 AS [Total Page File (MB)]
,available_page_file_kb / 1024 AS [Available Page File (MB)]
,system_cache_kb / 1024 AS [System Cache (MB)]
,system_memory_state_desc AS [System Memory State]
FROM sys.dm_os_sys_memory WITH (NOLOCK)
OPTION (RECOMPILE);
,available_physical_memory_kb / 1024 AS [Available Memory (MB)]
,total_page_file_kb / 1024 AS [Total Page File (MB)]
,available_page_file_kb / 1024 AS [Available Page File (MB)]
,system_cache_kb / 1024 AS [System Cache (MB)]
,system_memory_state_desc AS [System Memory State]
FROM sys.dm_os_sys_memory WITH (NOLOCK)
OPTION (RECOMPILE);
SELECT physical_memory_in_use_kb / 1024 AS [SQL Server Memory Usage (MB)]
,large_page_allocations_kb
,locked_page_allocations_kb
,page_fault_count
,memory_utilization_percentage
,available_commit_limit_kb
,process_physical_memory_low
,process_virtual_memory_low
FROM sys.dm_os_process_memory WITH (NOLOCK)
OPTION (RECOMPILE);
,large_page_allocations_kb
,locked_page_allocations_kb
,page_fault_count
,memory_utilization_percentage
,available_commit_limit_kb
,process_physical_memory_low
,process_virtual_memory_low
FROM sys.dm_os_process_memory WITH (NOLOCK)
OPTION (RECOMPILE);
SELECT @@SERVERNAME AS [Server Name]
,[object_name]
,cntr_value AS [Memory Grants Pending]
FROM sys.dm_os_performance_counters WITH (NOLOCK)
WHERE [object_name] LIKE N'%Memory Manager%' -- Handles named instances
AND counter_name = N'Memory Grants Pending'
OPTION (RECOMPILE);
,[object_name]
,cntr_value AS [Memory Grants Pending]
FROM sys.dm_os_performance_counters WITH (NOLOCK)
WHERE [object_name] LIKE N'%Memory Manager%' -- Handles named instances
AND counter_name = N'Memory Grants Pending'
OPTION (RECOMPILE);
如果 Memory Grants Pending 的值一直大于 0,则明确的说明 Memory 存在压力。
SELECT [name] AS [Name]
,[configuration_id] AS [Number]
,[minimum] AS [Minimum]
,[maximum] AS [Maximum]
,[is_dynamic] AS [Dynamic]
,[is_advanced] AS [Advanced]
,[value] AS [ConfigValue]
,[value_in_use] AS [RunValue]
,[description] AS [Description]
FROM [master].[sys].[configurations]
WHERE NAME IN (
'Min server memory (MB)'
,'Max server memory (MB)'
);
,[configuration_id] AS [Number]
,[minimum] AS [Minimum]
,[maximum] AS [Maximum]
,[is_dynamic] AS [Dynamic]
,[is_advanced] AS [Advanced]
,[value] AS [ConfigValue]
,[value_in_use] AS [RunValue]
,[description] AS [Description]
FROM [master].[sys].[configurations]
WHERE NAME IN (
'Min server memory (MB)'
,'Max server memory (MB)'
);
SELECT *
FROM sys.configurations
WHERE configuration_id IN (
'1543'
,'1544'
)
FROM sys.configurations
WHERE configuration_id IN (
'1543'
,'1544'
)
SELECT CAST(100.0 * SUM(signal_wait_time_ms) / SUM(wait_time_ms) AS NUMERIC(20, 2)) AS [% Signal (CPU) Waits]
,CAST(100.0 * SUM(wait_time_ms - signal_wait_time_ms) / SUM(wait_time_ms) AS NUMERIC(20, 2)) AS [% Resource Waits]
FROM sys.dm_os_wait_stats WITH (NOLOCK)
WHERE wait_type NOT IN (
N'BROKER_EVENTHANDLER'
,N'BROKER_RECEIVE_WAITFOR'
,N'BROKER_TASK_STOP'
,N'BROKER_TO_FLUSH'
,N'BROKER_TRANSMITTER'
,N'CHECKPOINT_QUEUE'
,N'CHKPT'
,N'CLR_AUTO_EVENT'
,N'CLR_MANUAL_EVENT'
,N'CLR_SEMAPHORE'
,N'DBMIRROR_DBM_EVENT'
,N'DBMIRROR_EVENTS_QUEUE'
,N'DBMIRROR_WORKER_QUEUE'
,N'DBMIRRORING_CMD'
,N'DIRTY_PAGE_POLL'
,N'DISPATCHER_QUEUE_SEMAPHORE'
,N'EXECSYNC'
,N'FSAGENT'
,N'FT_IFTS_SCHEDULER_IDLE_WAIT'
,N'FT_IFTSHC_MUTEX'
,N'HADR_CLUSAPI_CALL'
,N'HADR_FILESTREAM_IOMGR_IOCOMPLETION'
,N'HADR_LOGCAPTURE_WAIT'
,N'HADR_NOTIFICATION_DEQUEUE'
,N'HADR_TIMER_TASK'
,N'HADR_WORK_QUEUE'
,N'KSOURCE_WAKEUP'
,N'LAZYWRITER_SLEEP'
,N'LOGMGR_QUEUE'
,N'ONDEMAND_TASK_QUEUE'
,N'PWAIT_ALL_COMPONENTS_INITIALIZED'
,N'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP'
,N'QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP'
,N'REQUEST_FOR_DEADLOCK_SEARCH'
,N'RESOURCE_QUEUE'
,N'SERVER_IDLE_CHECK'
,N'SLEEP_BPOOL_FLUSH'
,N'SLEEP_DBSTARTUP'
,N'SLEEP_DCOMSTARTUP'
,N'SLEEP_MASTERDBREADY'
,N'SLEEP_MASTERMDREADY'
,N'SLEEP_MASTERUPGRADED'
,N'SLEEP_MSDBSTARTUP'
,N'SLEEP_SYSTEMTASK'
,N'SLEEP_TASK'
,N'SLEEP_TEMPDBSTARTUP'
,N'SNI_HTTP_ACCEPT'
,N'SP_SERVER_DIAGNOSTICS_SLEEP'
,N'SQLTRACE_BUFFER_FLUSH'
,N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP'
,N'SQLTRACE_WAIT_ENTRIES'
,N'WAIT_FOR_RESULTS'
,N'WAITFOR'
,N'WAITFOR_TASKSHUTDOWN'
,N'WAIT_XTP_HOST_WAIT'
,N'WAIT_XTP_OFFLINE_CKPT_NEW_LOG'
,N'WAIT_XTP_CKPT_CLOSE'
,N'XE_DISPATCHER_JOIN'
,N'XE_DISPATCHER_WAIT'
,N'XE_TIMER_EVENT'
)
OPTION (RECOMPILE);
,CAST(100.0 * SUM(wait_time_ms - signal_wait_time_ms) / SUM(wait_time_ms) AS NUMERIC(20, 2)) AS [% Resource Waits]
FROM sys.dm_os_wait_stats WITH (NOLOCK)
WHERE wait_type NOT IN (
N'BROKER_EVENTHANDLER'
,N'BROKER_RECEIVE_WAITFOR'
,N'BROKER_TASK_STOP'
,N'BROKER_TO_FLUSH'
,N'BROKER_TRANSMITTER'
,N'CHECKPOINT_QUEUE'
,N'CHKPT'
,N'CLR_AUTO_EVENT'
,N'CLR_MANUAL_EVENT'
,N'CLR_SEMAPHORE'
,N'DBMIRROR_DBM_EVENT'
,N'DBMIRROR_EVENTS_QUEUE'
,N'DBMIRROR_WORKER_QUEUE'
,N'DBMIRRORING_CMD'
,N'DIRTY_PAGE_POLL'
,N'DISPATCHER_QUEUE_SEMAPHORE'
,N'EXECSYNC'
,N'FSAGENT'
,N'FT_IFTS_SCHEDULER_IDLE_WAIT'
,N'FT_IFTSHC_MUTEX'
,N'HADR_CLUSAPI_CALL'
,N'HADR_FILESTREAM_IOMGR_IOCOMPLETION'
,N'HADR_LOGCAPTURE_WAIT'
,N'HADR_NOTIFICATION_DEQUEUE'
,N'HADR_TIMER_TASK'
,N'HADR_WORK_QUEUE'
,N'KSOURCE_WAKEUP'
,N'LAZYWRITER_SLEEP'
,N'LOGMGR_QUEUE'
,N'ONDEMAND_TASK_QUEUE'
,N'PWAIT_ALL_COMPONENTS_INITIALIZED'
,N'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP'
,N'QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP'
,N'REQUEST_FOR_DEADLOCK_SEARCH'
,N'RESOURCE_QUEUE'
,N'SERVER_IDLE_CHECK'
,N'SLEEP_BPOOL_FLUSH'
,N'SLEEP_DBSTARTUP'
,N'SLEEP_DCOMSTARTUP'
,N'SLEEP_MASTERDBREADY'
,N'SLEEP_MASTERMDREADY'
,N'SLEEP_MASTERUPGRADED'
,N'SLEEP_MSDBSTARTUP'
,N'SLEEP_SYSTEMTASK'
,N'SLEEP_TASK'
,N'SLEEP_TEMPDBSTARTUP'
,N'SNI_HTTP_ACCEPT'
,N'SP_SERVER_DIAGNOSTICS_SLEEP'
,N'SQLTRACE_BUFFER_FLUSH'
,N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP'
,N'SQLTRACE_WAIT_ENTRIES'
,N'WAIT_FOR_RESULTS'
,N'WAITFOR'
,N'WAITFOR_TASKSHUTDOWN'
,N'WAIT_XTP_HOST_WAIT'
,N'WAIT_XTP_OFFLINE_CKPT_NEW_LOG'
,N'WAIT_XTP_CKPT_CLOSE'
,N'XE_DISPATCHER_JOIN'
,N'XE_DISPATCHER_WAIT'
,N'XE_TIMER_EVENT'
)
OPTION (RECOMPILE);
通常,如果 Signal Waits 超过 10-15%,则说明 CPU 压力过大。
WITH AggregateBufferPoolUsage
AS (
SELECT DB_NAME(database_id) AS [Database Name]
,CAST(COUNT(*) * 8 / 1024.0 AS DECIMAL(10, 2)) AS [CachedSize]
FROM sys.dm_os_buffer_descriptors WITH (NOLOCK)
WHERE database_id <> 32767 -- ResourceDB
GROUP BY DB_NAME(database_id)
)
SELECT ROW_NUMBER() OVER (
ORDER BY CachedSize DESC
) AS [Buffer Pool Rank]
,[Database Name]
,CachedSize AS [Cached Size (MB)]
,CAST(CachedSize / SUM(CachedSize) OVER () * 100.0 AS DECIMAL(5, 2)) AS [Buffer Pool Percent]
FROM AggregateBufferPoolUsage
ORDER BY [Buffer Pool Rank]
OPTION (RECOMPILE);
AS (
SELECT DB_NAME(database_id) AS [Database Name]
,CAST(COUNT(*) * 8 / 1024.0 AS DECIMAL(10, 2)) AS [CachedSize]
FROM sys.dm_os_buffer_descriptors WITH (NOLOCK)
WHERE database_id <> 32767 -- ResourceDB
GROUP BY DB_NAME(database_id)
)
SELECT ROW_NUMBER() OVER (
ORDER BY CachedSize DESC
) AS [Buffer Pool Rank]
,[Database Name]
,CachedSize AS [Cached Size (MB)]
,CAST(CachedSize / SUM(CachedSize) OVER () * 100.0 AS DECIMAL(5, 2)) AS [Buffer Pool Percent]
FROM AggregateBufferPoolUsage
ORDER BY [Buffer Pool Rank]
OPTION (RECOMPILE);
参考资料:
- SQL Server Buffer Management
- SQL Server, Buffer Manager Object
- Buffer Pool Extension
- Buffer and cache Difference?
SELECT OBJECT_NAME(p.[object_id]) AS [Object Name]
,p.index_id
,CAST(COUNT(*) / 128.0 AS DECIMAL(10, 2)) AS [Buffer size(MB)]
,COUNT(*) AS [BufferCount]
,p.[Rows] AS [Row Count]
,p.data_compression_desc AS [Compression Type]
FROM sys.allocation_units AS a WITH (NOLOCK)
INNER JOIN sys.dm_os_buffer_descriptors AS b WITH (NOLOCK) ON a.allocation_unit_id = b.allocation_unit_id
INNER JOIN sys.partitions AS p WITH (NOLOCK) ON a.container_id = p.hobt_id
WHERE b.database_id = CONVERT(INT, DB_ID())
AND p.[object_id] > 100
GROUP BY p.[object_id]
,p.index_id
,p.data_compression_desc
,p.[Rows]
ORDER BY [BufferCount] DESC
OPTION (RECOMPILE);
,p.index_id
,CAST(COUNT(*) / 128.0 AS DECIMAL(10, 2)) AS [Buffer size(MB)]
,COUNT(*) AS [BufferCount]
,p.[Rows] AS [Row Count]
,p.data_compression_desc AS [Compression Type]
FROM sys.allocation_units AS a WITH (NOLOCK)
INNER JOIN sys.dm_os_buffer_descriptors AS b WITH (NOLOCK) ON a.allocation_unit_id = b.allocation_unit_id
INNER JOIN sys.partitions AS p WITH (NOLOCK) ON a.container_id = p.hobt_id
WHERE b.database_id = CONVERT(INT, DB_ID())
AND p.[object_id] > 100
GROUP BY p.[object_id]
,p.index_id
,p.data_compression_desc
,p.[Rows]
ORDER BY [BufferCount] DESC
OPTION (RECOMPILE);
可以判断哪张表或索引占用的 Buffer 也就是 Memory 最多,可以考虑应用不同的 Compression Type。
参考资料:
- Data Compression
- Row Compression Implementation
- Page Compression Implementation
- Enable Compression on a Table or Index
SQL Server 2012 版本
SELECT TOP (10) mc.[type] AS [Memory Clerk Type]
,CAST((SUM(mc.pages_kb) / 1024.0) AS DECIMAL(15, 2)) AS [Memory Usage (MB)]
FROM sys.dm_os_memory_clerks AS mc WITH (NOLOCK)
GROUP BY mc.[type]
ORDER BY SUM(mc.pages_kb) DESC
OPTION (RECOMPILE);
,CAST((SUM(mc.pages_kb) / 1024.0) AS DECIMAL(15, 2)) AS [Memory Usage (MB)]
FROM sys.dm_os_memory_clerks AS mc WITH (NOLOCK)
GROUP BY mc.[type]
ORDER BY SUM(mc.pages_kb) DESC
OPTION (RECOMPILE);
SQL Server 2008 版本
SELECT TOP (10) [type] AS [Memory Clerk Type]
,SUM(single_pages_kb) / 1024 AS [SPA Memory Usage (MB)]
FROM sys.dm_os_memory_clerks WITH (NOLOCK)
GROUP BY [type]
ORDER BY SUM(single_pages_kb) DESC
OPTION (RECOMPILE);
,SUM(single_pages_kb) / 1024 AS [SPA Memory Usage (MB)]
FROM sys.dm_os_memory_clerks WITH (NOLOCK)
GROUP BY [type]
ORDER BY SUM(single_pages_kb) DESC
OPTION (RECOMPILE);
参考资料:
- sys.dm_os_memory_clerks (Transact-SQL)
- An in-depth look at SQL Server Memory–Part 1
- An in-depth look at SQL Server Memory–Part 2
- An in-depth look at SQL Server Memory–Part 3
可以直接运行:
DBCC MEMORYSTATUS();
查看 Memory 各项指标的细节。
DECLARE @MemStat TABLE (
ValueName SYSNAME
,Val BIGINT
);
INSERT INTO @MemStat
EXEC ('DBCC MEMORYSTATUS() WITH TABLERESULTS');
WITH Measures
AS (
SELECT TOP 2 CurrentValue
,ROW_NUMBER() OVER (
ORDER BY OrderColumn
) AS RowOrder
FROM (
SELECT CASE
WHEN (ms.ValueName = 'Target Committed')
THEN ms.Val
WHEN (ms.ValueName = 'Current Committed')
THEN ms.Val
END AS 'CurrentValue'
,0 AS 'OrderColumn'
FROM @MemStat AS ms
) AS MemStatus
WHERE CurrentValue IS NOT NULL
)
SELECT TargetMem.CurrentValue - CurrentMem.CurrentValue
FROM Measures AS TargetMem
JOIN Measures AS CurrentMem ON TargetMem.RowOrder + 1 = CurrentMem.RowOrder;
ValueName SYSNAME
,Val BIGINT
);
INSERT INTO @MemStat
EXEC ('DBCC MEMORYSTATUS() WITH TABLERESULTS');
WITH Measures
AS (
SELECT TOP 2 CurrentValue
,ROW_NUMBER() OVER (
ORDER BY OrderColumn
) AS RowOrder
FROM (
SELECT CASE
WHEN (ms.ValueName = 'Target Committed')
THEN ms.Val
WHEN (ms.ValueName = 'Current Committed')
THEN ms.Val
END AS 'CurrentValue'
,0 AS 'OrderColumn'
FROM @MemStat AS ms
) AS MemStatus
WHERE CurrentValue IS NOT NULL
)
SELECT TargetMem.CurrentValue - CurrentMem.CurrentValue
FROM Measures AS TargetMem
JOIN Measures AS CurrentMem ON TargetMem.RowOrder + 1 = CurrentMem.RowOrder;
参考资料:
- Using DBCC MEMORYSTATUS to Monitor SQL Server Memory Usage
- Why is that SQL Server Instance under stress?
SELECT record_id
,dateadd(ms, (y.[timestamp] - tme.ms_ticks), GETDATE()) AS [Notification_Time]
,Notification
FROM (
SELECT record.value('(./Record/@id)[1]', 'int') AS record_id
,record.value('(./Record/ResourceMonitor/Notification)[1]', 'varchar(50)') AS Notification
,TIMESTAMP
FROM (
SELECT TIMESTAMP
,CONVERT(XML, record) AS record
FROM sys.dm_os_ring_buffers
WHERE ring_buffer_type = N'RING_BUFFER_RESOURCE_MONITOR'
) AS x
) AS y
CROSS JOIN sys.dm_os_sys_info tme
ORDER BY record_id DESC;
SELECT dateadd(ms, (rbf.[timestamp] - tme.ms_ticks), GETDATE()) AS [Notification_Time]
,cast(record AS XML).value('(//Record/ResourceMonitor/Notification)[1]', 'varchar(30)') AS [Notification_type]
,cast(record AS XML).value('(//Record/MemoryRecord/MemoryUtilization)[1]', 'bigint') AS [MemoryUtilization %]
,cast(record AS XML).value('(//Record/MemoryNode/@id)[1]', 'bigint') AS [Node Id]
,cast(record AS XML).value('(//Record/ResourceMonitor/IndicatorsProcess)[1]', 'int') AS [Process_Indicator]
,cast(record AS XML).value('(//Record/ResourceMonitor/IndicatorsSystem)[1]', 'int') AS [System_Indicator]
,cast(record AS XML).value('(//Record/MemoryNode/ReservedMemory)[1]', 'bigint') AS [SQL_ReservedMemory_KB]
,cast(record AS XML).value('(//Record/MemoryNode/CommittedMemory)[1]', 'bigint') AS [SQL_CommittedMemory_KB]
,cast(record AS XML).value('(//Record/MemoryNode/AWEMemory)[1]', 'bigint') AS [SQL_AWEMemory]
,cast(record AS XML).value('(//Record/MemoryNode/SinglePagesMemory)[1]', 'bigint') AS [SinglePagesMemory]
,cast(record AS XML).value('(//Record/MemoryNode/MultiplePagesMemory)[1]', 'bigint') AS [MultiplePagesMemory]
,cast(record AS XML).value('(//Record/MemoryRecord/TotalPhysicalMemory)[1]', 'bigint') AS [TotalPhysicalMemory_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/AvailablePhysicalMemory)[1]', 'bigint') AS [AvailablePhysicalMemory_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/TotalPageFile)[1]', 'bigint') AS [TotalPageFile_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/AvailablePageFile)[1]', 'bigint') AS [AvailablePageFile_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/TotalVirtualAddressSpace)[1]', 'bigint') AS [TotalVirtualAddressSpace_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/AvailableVirtualAddressSpace)[1]', 'bigint') AS [AvailableVirtualAddressSpace_KB]
,cast(record AS XML).value('(//Record/@id)[1]', 'bigint') AS [Record Id]
,cast(record AS XML).value('(//Record/@type)[1]', 'varchar(30)') AS [Type]
FROM sys.dm_os_ring_buffers rbf
CROSS JOIN sys.dm_os_sys_info tme
WHERE rbf.ring_buffer_type = 'RING_BUFFER_RESOURCE_MONITOR'
ORDER BY rbf.TIMESTAMP ASC;
,dateadd(ms, (y.[timestamp] - tme.ms_ticks), GETDATE()) AS [Notification_Time]
,Notification
FROM (
SELECT record.value('(./Record/@id)[1]', 'int') AS record_id
,record.value('(./Record/ResourceMonitor/Notification)[1]', 'varchar(50)') AS Notification
,TIMESTAMP
FROM (
SELECT TIMESTAMP
,CONVERT(XML, record) AS record
FROM sys.dm_os_ring_buffers
WHERE ring_buffer_type = N'RING_BUFFER_RESOURCE_MONITOR'
) AS x
) AS y
CROSS JOIN sys.dm_os_sys_info tme
ORDER BY record_id DESC;
SELECT dateadd(ms, (rbf.[timestamp] - tme.ms_ticks), GETDATE()) AS [Notification_Time]
,cast(record AS XML).value('(//Record/ResourceMonitor/Notification)[1]', 'varchar(30)') AS [Notification_type]
,cast(record AS XML).value('(//Record/MemoryRecord/MemoryUtilization)[1]', 'bigint') AS [MemoryUtilization %]
,cast(record AS XML).value('(//Record/MemoryNode/@id)[1]', 'bigint') AS [Node Id]
,cast(record AS XML).value('(//Record/ResourceMonitor/IndicatorsProcess)[1]', 'int') AS [Process_Indicator]
,cast(record AS XML).value('(//Record/ResourceMonitor/IndicatorsSystem)[1]', 'int') AS [System_Indicator]
,cast(record AS XML).value('(//Record/MemoryNode/ReservedMemory)[1]', 'bigint') AS [SQL_ReservedMemory_KB]
,cast(record AS XML).value('(//Record/MemoryNode/CommittedMemory)[1]', 'bigint') AS [SQL_CommittedMemory_KB]
,cast(record AS XML).value('(//Record/MemoryNode/AWEMemory)[1]', 'bigint') AS [SQL_AWEMemory]
,cast(record AS XML).value('(//Record/MemoryNode/SinglePagesMemory)[1]', 'bigint') AS [SinglePagesMemory]
,cast(record AS XML).value('(//Record/MemoryNode/MultiplePagesMemory)[1]', 'bigint') AS [MultiplePagesMemory]
,cast(record AS XML).value('(//Record/MemoryRecord/TotalPhysicalMemory)[1]', 'bigint') AS [TotalPhysicalMemory_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/AvailablePhysicalMemory)[1]', 'bigint') AS [AvailablePhysicalMemory_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/TotalPageFile)[1]', 'bigint') AS [TotalPageFile_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/AvailablePageFile)[1]', 'bigint') AS [AvailablePageFile_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/TotalVirtualAddressSpace)[1]', 'bigint') AS [TotalVirtualAddressSpace_KB]
,cast(record AS XML).value('(//Record/MemoryRecord/AvailableVirtualAddressSpace)[1]', 'bigint') AS [AvailableVirtualAddressSpace_KB]
,cast(record AS XML).value('(//Record/@id)[1]', 'bigint') AS [Record Id]
,cast(record AS XML).value('(//Record/@type)[1]', 'varchar(30)') AS [Type]
FROM sys.dm_os_ring_buffers rbf
CROSS JOIN sys.dm_os_sys_info tme
WHERE rbf.ring_buffer_type = 'RING_BUFFER_RESOURCE_MONITOR'
ORDER BY rbf.TIMESTAMP ASC;
-- there are thousands of different counters
SELECT *
FROM sys.dm_os_performance_counters;
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name = 'Page Life expectancy'
AND object_name LIKE '%Buffer Manager%';
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE '%Average Wait Time%'
AND instance_name = 'Database';
SELECT *
FROM sys.dm_os_performance_counters;
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name = 'Page Life expectancy'
AND object_name LIKE '%Buffer Manager%';
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE '%Average Wait Time%'
AND instance_name = 'Database';
DECLARE @BRPS BIGINT
SELECT @BRPS = cntr_value
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE 'Batch Requests/sec%'
WAITFOR DELAY '000:00:10'
SELECT (cntr_value - @BRPS) / 10.0 AS "Batch Requests/sec"
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE 'Batch Requests/sec%'
SELECT @BRPS = cntr_value
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE 'Batch Requests/sec%'
WAITFOR DELAY '000:00:10'
SELECT (cntr_value - @BRPS) / 10.0 AS "Batch Requests/sec"
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE 'Batch Requests/sec%'
《人人都是 DBA》系列文章索引:
序号 | 名称 |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 |
本系列文章《 人人都是 DBA》由 Dennis Gao 发表自 博客园,未经作者本人同意禁止任何形式的转载,任何自动或人为的爬虫转载行为均为耍流氓。
本文链接: 人人都是 DBA(X)资源信息收集脚本汇编,转载请注明。
相关 [人人 dba 资源] 推荐:
臣妾不知道啊,得找 DBA 啊. 程序员在无处寻求帮助时,就得想办法自救,努力让自己变成 "伪 DBA". 获取数据库的 CPU 使用率. 过去一段时间里 CPU 利用率的历史情况. 谁用 CPU 工作的时间最长. 服务器上安装了多大的 Memory. SQL Server 进程用了多少 Memory.
DBA工作总结
- - CSDN博客数据库推荐文章一年以来,本人尊敬领导团结同事、服从安排 遵守纪律,坚持努力学习专业知识,兢兢业业克己奉公努力工作. 总结过去,在知识结构上,能够完成了EBS-DBA的各项工作;在日常工作XX,能够完成EBS-DBA的各项工作任务,适应了DBA工作岗位要求的职责,掌握了EBS-DBA要求的多项技术. 我一年以来的主要工作从以下几个方面说起主要包括日常维护、补丁更新,安装规划,文档整理,最后给出下一步规划.
DBA团队的使命
- 2sin18 - Alibaba DBA TeamDBA团队的使命:提供高可用、高性能、可扩展的数据存储服务. 高可用:可用性是运维的根本,我们不管做什么事情,都要把可用性放在第一位. 高性能:对性能的关注是我们一直坚持、做的最好的一面,仍需要继续做到极致. 可扩展:也就是最适合的,易部署,可线形透明伸缩. 数据存储:不只是关注某个数据库本身,是基于对各种最先进的数据存储技术的精深理解,提供最专业的服务.
DBA Notes 也有 iPhone App 了 ?
- Epile - DBA notes刚才在我的 Google+ 上发布了一条半开玩笑的信息:DBA Notes 也有 iPhone App 了. 其实没那么神奇,借助于这款 iOS App : Bloapp .. 安装完这个 App 之后,到其网站上"创建"你的 App,其实主要是一些视觉风格的定义,用它扫描生成的这个 QR Code :.
一个DBA眼中的HBase
- - IT技术博客大学习标签: HBase. Hadoop,HBase,NO-SQL是当今业界比较火的一些名词. 满互联网都是对它的他们的赞许,其实光芒的背后还有部分缺点. 本文只是我vogts的一些观点和想法. HBase的优点:. 分布式,易扩展,高性价比,运维成本低都是它的优点. HBase可以支持海量数据,单张表的数据量不上T,都不好意思出来打招呼.
常用Oracle DBA 查询
- - CSDN博客推荐文章-- Part1 Oracle常用查询. and t.table_name = 要查询的表 . and au.constraint_type = 'P' and au.table_name = 要查询的表. and au.table_name = 要查询的表 . select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查询的表.
MySQL DBA修炼秘籍
- - OurMySQL本文主要写给那些立志成为MySQL DBA,以及正在学习MySQL的同行们,结合个人及业内其他同行的职业发展经历给大家一些参考,如何成为合格的MySQL DBA. 1、什么是MySQL DBA. 首先,DBA是database administrator(数据库管理员)的简称,在一些招聘网站上,也可能会把职位写成数据库[管理]工程师,MySQL DBA是目前互联网企业中最为炙手可热的岗位需求之一,前(钱)景大好,快到碗里来吧.
如何成为MySQL DBA
- - OurMySQL 互联网高速发展的成功,得益于MySQL数据库的给力支持. MySQL本身发展的速度较快,性能方面提升显著,让传统企业也有想法使用MySQL提供服务. 目前看来MySQL DBA的缺口非常大. 所以欢迎加入到MySQL DBA的团队中来. 有同学一提到MySQL DBA或是DBA都把高难度入门联系到一块.
MySQL DBA面试全揭秘
- - OurMySQL本文起源于有同学留言回复说想了解下MySQL DBA面试时可能涉及到的知识要点,那我们今天就来大概谈谈吧. MySQL DBA职位最近几年特别热门,不少朋友让我帮忙推荐什么的,也有很多公司找不到合适的DBA. 原因很简单,优秀的人才要么被大公司圈起来了,要么被创业公司高薪挖走,如果你既不是大公司,又不能出得起高价钱的土豪公司,想要找到优秀人才的几率堪比买彩票中奖的概率,哈哈.
互联网 DBA 需要做那些事
- - CSDN博客数据库推荐文章很早前就想写篇文章介绍一下互联网DBA需要干的一些事情,但苦于没有时间,忙于平台建设,最近,各个模块都初具规模,故有时间静下心来,介绍一下. 众所周知,互联网DBA与传统行业DBA有很大的不同,那就是管理的机器多,新技术更新快,面对的开发多、网络环境复杂、要求7*24待机;这样就 导致互联网DBA的工作在传统DBA工作之上,增加了更多的复杂性,我们必须考虑如何大批量部署,如何集中化监控、如何解决单点故障而保障7*24,而为 了做到这些,不是靠堆人力,我们必须有一个完整的平台作为支撑,那么数据库平台到底要建成什么样子呢.