Reference
http://gallery.technet.microsoft.com/ScriptCenter/en-us/
http://technet.microsoft.com/en-us/sqlserver/bb331794.aspx
简介
SQL Server 05提供了动态管理视图Dynamic Management Views和函数 Functions,方便了我们对系统运行情况的监控,故障诊断和性能优化.配合Profiler,dashboard一起使用很不错.
常规服务器动态管理对象包括:
我的脚本(DBA 下的存储过程)
常用的SQL Server监控手段总结
SELECT TOP 100 execution_count,
total_logical_reads / execution_count AS [ Avg Logical Reads ] ,
total_elapsed_time / execution_count AS [ Avg Elapsed Time ] ,
db_name (st.dbid) as [ database name ] ,
object_name (st.dbid) as [ object name ] ,
object_name (st.objectid) as [ object name 1 ] ,
SUBSTRING (st. text , (qs.statement_start_offset / 2 ) + 1 ,
(( CASE statement_end_offset WHEN - 1 THEN DATALENGTH (st. text )
ELSE qs.statement_end_offset END - qs.statement_start_offset)
/ 2 ) + 1 ) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
WHERE execution_count > 100
and db_name (st.dbid) is not null and db_name (st.dbid) not in ( ' distribution ' )
ORDER BY 1 DESC ; --关于statement_start_offset/2的疑问
--注意dm_exec_query_stats仅仅统计与分析在高速缓存中的执行计划,并非所有SQL Server耗时的工作都会有执行计划高速缓存,例如DBCC DBReindex不会被缓存
set statistics io on
go
select top 1 * from sales.customer where customertype <> ' S ' ;
CustomerID TerritoryID AccountNumber CustomerType rowguid ModifiedDate
----------- ----------- ------------- ------------ ------------------------------------ -----------------------
11000 9 AW00011000 I 477586B3-2977-4E54-B1A8-569AB2C7C4D4 2004-10-13 11:15:07.263
(1 行受影响)
表 'Customer'。扫描计数 1,逻辑读取 6 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
--如果需要清理缓存池 DBCC DROPCLEANBUFFER
declare @x int ;
declare @cpu_start int ;
set @x = 1 ;
set @cpu_start = @@cpu_busy ;
while @x < 10000
set @x = @x + 1 ;
print ' ms of cput for loop1: '
+ cast ( ( @@cpu_busy - @cpu_start ) + @@timeticks / 1000 as char );
set @cpu_start = @@cpu_busy ;
while @x < 100000
set @x = @x + 1 ;
print ' ms of cput for loop1: '+ cast ( ( @@cpu_busy - @cpu_start ) + @@timeticks / 1000 as char );--注意这两个参数 @@cpu_busy @@timeticks
SELECT CASE when dbid = 32767
then ' Resource '
else DB_NAME (dbid) end [ DB_NAME ] ,
OBJECT_SCHEMA_NAME(objectid,dbid) AS [ SCHEMA_NAME ] ,
OBJECT_NAME (objectid,dbid) AS [ OBJECT_NAME ] ,
SUM (usecounts) AS [ Use_Count ] ,
SUM (total_elapsed_time) AS [ total_elapsed_time ] ,
SUM (total_elapsed_time) / SUM (usecounts) * 1.0 AS [ avg_elapsed_time ] ,
substring ( convert ( char ( 23 ), DATEADD (ms, sum (total_elapsed_time) / 1000 , 0 ), 121 ), 12 , 23 )AS total_elapsed_time_ms,dbid,
objectid
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle)
JOIN
( SELECT SUM (total_elapsed_time) AS [ total_elapsed_time ] ,
plan_handle
FROM sys.dm_exec_query_stats
GROUP BY plan_handle) qs
ON cp.plan_handle = qs.plan_handle
WHERE objtype = ' Proc '
AND UPPER (
-- remove white space first
REPLACE (
REPLACE (
REPLACE (
REPLACE (
REPLACE (
REPLACE (
REPLACE ( text , ' ' , ' ' ),
' ' , ' ' ),
' ' , ' ' ),
' ' , ' ' ),
' ' , ' ' ),
' ' , ' ' ),
' ' , ' ' )
)
LIKE ' %CREATE PROC% '
GROUP BY dbid, objectid
ORDER BY SUM (total_elapsed_time) / SUM (usecounts) * 1.0 DESC ;
SELECT object_name(i.object_id) as object_name,i.name as IndexName,ps.avg_fragmentation_in_percent,avg_page_space_used_in_percent FROM sys.dm_db_index_physical_stats (db_id(), NULL, NULL, NULL , 'DETAILED') as ps INNER JOIN sys.indexes as i ON i.object_id = ps.object_id AND i.index_id = ps.index_id WHERE ps.avg_fragmentation_in_percent > 50 AND ps.index_id > 0 ORDER BY 1 object_name IndexName avg_fragmentation_in_percent avg_page_space_used_in_percent ----------- --------------------------------- ---------------------------- ------------------------------ Employee PK_Employee_EmployeeID 85.7142857142857 97.8679145045713 Employee AK_Employee_LoginID 66.6666666666667 66.5843093649617 Individual PK_Individual_CustomerID 87.5 61.851371386212 Individual XMLVALUE_Individual_Demographics 100 60.5339263652088
SELECT * FROM sys.dm_db_index_physical_stats (DB_ID(N'AdventureWorks'), OBJECT_ID(N'dbo.DatabaseLog'), NULL, NULL , 'DETAILED'); --index_type_desc: HEAP,NON or CLUSTERED INDEX --alloc_unit_type_desc: IN_ROW_DATA,LOB_DATA --about index: index_depth, index_level --about fragment: avg_fragmentation_in_percent,avg_fragment_size_in_pages