xywuyiba8 发表于 2015-7-1 05:15:04

优化-SQL Server 05 的动态管理视图DMV和函数DMF

  SQL Server 05提供了动态管理视图Dynamic Management Views和函数 Functions,方便了我们对系统运行情况的监控,故障诊断和性能优化.配合Profiler,dashboard一起使用很不错.

[*]使用sys.dm_exec_query_stats和sys.dm_exec_sql_text找到CPU占用率高的语句 这里有篇文章不错 http://database.ctocio.com.cn/tips/159/7771659.shtml


SELECT TOP 100 execution_count,
         total_logical_reads /execution_count AS ,
         total_elapsed_time /execution_count AS ,
                db_name(st.dbid) as ,
         object_name(st.dbid) as ,
         object_name(st.objectid) as ,
         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
ORDER BY 1 DESC;  --关于statement_start_offset/2的疑问 http://topic.iyunv.com/u/20100308/14/6b6307d1-efea-459c-b3cd-b50c29d3642e.html
  IO跟踪


setstatistics io on
go
selecttop1*from sales.customer where customertype 'S';

CustomerIDTerritoryID AccountNumber CustomerType rowguid                              ModifiedDateCustomerIDTerritoryID 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 DROPCLEANBUFFERCPU时间


declare@xint;
declare@cpu_startint;
set@x=1;
set@cpu_start=@@cpu_busy;
while@x
页: [1]
查看完整版本: 优化-SQL Server 05 的动态管理视图DMV和函数DMF