SQLServer数据库优化常用语句

-- 平均物理读次数最多的SQL语句:
select top 50 *, (s.total_physical_reads / s.execution_count) as avephysicalreads from sys.dm_exec_query_stats s
cross apply sys.dm_exec_sql_text(s.sql_handle)
order by avephysicalreads desc

 
-- 平均逻辑读次数最多的SQL语句:
select top 50 *, (s.total_logical_reads / s.execution_count) as avglogicalreads from sys.dm_exec_query_stats s
cross apply sys.dm_exec_sql_text(s.sql_handle)
order by avglogicalreads desc


-- 平均逻辑写次数最多的SQL语句:
select top 50 *, (s.total_logical_writes / s.execution_count) as avglogicalwrites from sys.dm_exec_query_stats s
cross apply sys.dm_exec_sql_text(s.sql_handle)
order by avglogicalwrites desc


-- 返回执行的线程所遇到的所有等待的相关信息
select * from sys.dm_os_wait_stats


-- 返回正在等待某些资源的任务的等待队列的有关信息
select * from sys.dm_os_waiting_tasks
--dbcc inputbuffer(103)查看哪条SQL造成的等待

 

--查看当前占用 cpu 资源最高的会话和其中执行的语句(及时CPU)
select spid,cmd,cpu,physical_io,memusage,
(select top 1 [text] from ::fn_get_sql(sql_handle)) sql_text
from master..sysprocesses order by cpu desc,physical_io desc   

 

--查看缓存中重用次数少,占用内存大的查询语句(当前缓存中未释放的)--全局
SELECT TOP 100 usecounts, objtype, p.size_in_bytes,[sql].[text] 
FROM sys.dm_exec_cached_plans p OUTER APPLY sys.dm_exec_sql_text (p.plan_handle) sql 
ORDER BY usecounts,p.size_in_bytes  desc

你可能感兴趣的:(sqlserver)