码迷,mamicode.com
首页 > 数据库 > 详细

SQL性能优化常用语句(摘录网上)

时间:2014-11-01 23:05:39      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   os   ar   sp   strong   

1.把trace文件导入到表中

SELECT IDENTITY(BIGINT, 1, 1) AS RowNumber,* 
into TableName
FROM fn_trace_gettable(trace.trc, default)

2.查询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

3.查询阻塞

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

4.查询连接数

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

 

SQL性能优化常用语句(摘录网上)

标签:des   style   blog   io   color   os   ar   sp   strong   

原文地址:http://www.cnblogs.com/xqhppt/p/4067804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!