程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> [筆記] SQL性能優化 - 常用語句(二)

[筆記] SQL性能優化 - 常用語句(二)

編輯:關於SqlServer

1、查詢CPU開銷大的語句

SELECT top 100
    total_worker_time/execution_count AS avg_cpu_cost, plan_handle,
    execution_count,
    (SELECT SUBSTRING(text, statement_start_offset/2 + 1,
       (CASE WHEN statement_end_offset = -1
          THEN LEN(CONVERT(nvarchar(max), text)) * 2
          ELSE statement_end_offset
       END - statement_start_offset)/2)
    FROM sys.dm_exec_sql_text(sql_handle)
    ) AS query_text,creation_time
 FROM sys.dm_exec_query_stats
 where creation_time > '2014-11-01'
and (total_worker_time/execution_count) > 146341
ORDER BY [avg_cpu_cost] DESC

 

2、查詢阻塞

SELECT a.* FROM sys.[sysprocesses] a
WHERE a.[spid]>50 AND DB_NAME(a.[dbid])='DBName' and a.spid <> @@SPID and a.blocked > 0

 

3、查詢連接數

SELECT COUNT(*) FROM [sys].[dm_exec_sessions] WHERE [session_id]>50

 

4、存儲過程物理讀

SELECT TOP (50) p.[name] AS [SP Name]
    ,qs.total_physical_reads AS [TotalPhysicalReads]
    ,qs.total_physical_reads / qs.execution_count AS [AvgPhysicalReads]
    ,qs.execution_count
    ,qs.total_logical_reads
    ,qs.total_elapsed_time
    ,qs.total_elapsed_time / qs.execution_count AS [avg_elapsed_time]
    ,qs.cached_time
FROM sys.procedures AS p
INNER JOIN sys.dm_exec_procedure_stats AS qs ON p.[object_id] = qs.[object_id]
WHERE qs.database_id = DB_ID()
    AND qs.total_physical_reads > 0
ORDER BY qs.total_physical_reads DESC
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved