Sqlserver相关性能查询

查找缺失索引:

SELECT
migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure,
'CREATE INDEX [missing_index_' + CONVERT (varchar, mig.index_group_handle) + '_' + CONVERT (varchar, mid.index_handle)
+ '_' + LEFT (PARSENAME(mid.statement, 1), 32) + ']'
+ ' ON ' + mid.statement
+ ' (' + ISNULL (mid.equality_columns,'')
+ CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END
+ ISNULL (mid.inequality_columns, '')
+ ')'
+ ISNULL (' INCLUDE (' + mid.included_columns + ')', '') AS create_index_statement,
migs.*, mid.database_id, mid.[object_id]
FROM sys.dm_db_missing_index_groups mig
INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle
WHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10
ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC

 

死锁等待

select
wt.session_id as waiting_session_id, DB_NAME(tl.resource_database_id) as DatabaseName, wt.wait_duration_ms
,wt.waiting_task_address, tl.request_mode
,(select substring(st.text,(er.statement_start_offset/2)+1,
(case er.statement_end_offset
when -1 then datalength(st.text) else er.statement_end_offset end - er.statement_start_offset/2)+1)
from sys.dm_exec_requests as er cross apply sys.dm_exec_sql_text(er.sql_handle) as st
where er.session_id=tl.request_session_id) as waiting_query_text
,tl.resource_type, tl.resource_associated_entity_id,wt.wait_type, wt.blocking_session_id
,wt.resource_description as blocking_resource_description
, case when wt.blocking_session_id>0 then
(select st2.text from sys.sysprocesses as sp cross apply sys.dm_exec_sql_text(sp.sql_handle) as st2
where sp.spid=wt.blocking_session_id) else null end as blocking_query_text
from sys.dm_os_waiting_tasks as wt join sys.dm_tran_locks as tl
on wt.resource_address = tl.lock_owner_address
where wt.wait_duration_ms > 5000;

 

长时任务:

-- 表锁跟踪
select request_session_id,resource_type, DB_NAME(resource_database_id) as DatabaseName
,OBJECT_NAME(resource_associated_entity_id) as TableName, request_mode, request_type, request_status
from sys.dm_tran_locks as tl join sys.all_objects as ao on tl.resource_associated_entity_id=ao.object_id
where request_type='LOCK' and request_status='GRANT' and request_mode in('X','S')
and ao.type='U' and resource_type='OBJECT' and tl.resource_database_id=db_id()

-- 长时间等待的任务
select top 20 wt.session_id, wt.waiting_task_address, wt.wait_duration_ms, wt.wait_type
, wt.blocking_session_id, wt.resource_description
--, wt.waiting_tasks_count, wt.max_wait_time_ms
from sys.dm_os_waiting_tasks as wt
-- where wt.wait_duration_ms > 5000
order by wait_duration_ms desc

-- Top 10 耗时任务
SELECT TOP 20 creation_time, last_execution_time, total_elapsed_time,
total_elapsed_time/execution_count AS [Avg Elapsed Time us], last_elapsed_time,
execution_count
, ( SELECT SUBSTRING(text, statement_start_offset/2,
( CASE
WHEN statement_end_offset = -1
THEN LEN(CONVERT(nvarchar(max),text)) * 2
ELSE statement_end_offset END
- statement_start_offset
)
)
FROM sys.dm_exec_sql_text(sql_handle)) AS query_text
FROM sys.dm_exec_query_stats
ORDER BY [Avg Elapsed Time us] DESC;

 

 

 

posted @ 2013-04-11 18:02  快活杰  阅读(387)  评论(0编辑  收藏  举报