sql server中如何查看执行效率不高的语句
sql server中,如果想知道有哪些语句是执行效率不高的,应该如何查看呢?下面就将为您介绍sql server中如何查看执行效率不高的语句,供您参考。
在测量功能时,先以下命令清除sql server的缓存
dbcc freeProcCache
在点击某个按钮,执行完后,再执行下面语句,就可以知道系统运行什么Sql和多少次了,其主要慢语句是那些了;
方法一:
SELECT creation_time N
'语句编译时间'
,last_execution_time N
'上次执行时间'
,total_physical_reads N
'物理读取总次数'
,total_logical_reads/execution_count N
'每次逻辑读次数'
,total_logical_reads N
'逻辑读取总次数'
,total_logical_writes N
'逻辑写入总次数'
,execution_count N
'执行次数'
,total_worker_time/1000 N
'所用的CPU总时间ms'
,total_elapsed_time/1000 N
'总花费时间ms'
,(total_elapsed_time / execution_count)/1000 N
'平均时间ms'
,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) N
'执行语句'
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
where
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) not like
'%fetch%'
ORDER BY total_elapsed_time / execution_count DESC;
方法二:
SELECT TOP 10 TEXT AS 'SQL Statement' ,last_execution_time AS 'Last Execution Time' ,(total_logical_reads + total_physical_reads + total_logical_writes) / execution_count AS [Average IO] ,(total_worker_time / execution_count) / 1000000.0 AS [Average CPU Time (sec)] ,(total_elapsed_time / execution_count) / 1000000.0 AS [Average Elapsed Time (sec)] ,execution_count AS "Execution Count" ,qp.query_plan AS "Query Plan" FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp ORDER BY total_elapsed_time / execution_count DESC参考链接1:https://www.cnblogs.com/weiqt/articles/3873001.html
参考链接2:https://q.cnblogs.com/q/95427/