sql server查询优化语句

学习来源:SQL2008技术内幕:T-SQL查询
SELECT
(total_elapsed_time / execution_count)/1000 N'平均时间ms'
,total_elapsed_time/1000 N'总花费时间ms'
,total_worker_time/1000 N'所用的CPU总时间ms'
,total_physical_reads N'物理读取总次数'
,total_logical_reads/execution_count N'每次逻辑读次数'
,total_logical_reads N'逻辑读取总次数'
,total_logical_writes N'逻辑写入总次数'
,execution_count N'执行次数'
,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'执行语句'
,creation_time N'语句编译时间'
,last_execution_time N'上次执行时间'
FROM
sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as 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%'
and creation_time between '2020-08-04 00:00:00.000' and '2020-08-06 00:00:00.000'
ORDER BY 
-- last_execution_time DESC
 total_elapsed_time / execution_count DESC;

 --系统等待时间类型
 select 
wait_type,
waiting_tasks_count,--表示该类型的等待数量
wait_time_ms--以毫秒为单位的该类等待的总等待时间(该时间包含signal_wait_time_ms)
max_wait_time_ms,
signal_wait_time_ms--这个是在线程收到信号到通知运行的时间差
from sys.dm_os_wait_stats
order by wait_type

with waits as (
  select wait_type,
  wait_time_ms/1000 wait_time_s,
  100*wait_time_ms/sum(wait_time_ms) over() as pct,
  row_number() over(order by wait_time_ms desc ) as rn ,
  100*signal_wait_time_ms/wait_time_ms as signal_pct
  from sys.dm_os_wait_stats 
  where wait_time_ms>0 
  and wait_type not like '%SLEEP%'
    and wait_type not like '%IDLE%'
    and wait_type not like '%QUEUE%'
    and wait_type not in ( 'CLR_AUTO_EVENT','REQUEST_FOR_DEADLOCK_SEARCH','SQLTRACE_BUFFER_FLUSH')
  )
  select 
  w1.wait_type,--等待类型
  cast(w1.wait_time_s as numeric(12,2)) as wait_time_s,--
  cast(w1.pct as numeric(5,2)) as qct,
  cast(sum(w1.pct) as numeric(5,2)) as running_qct,
  cast(w1.signal_pct as numeric(5,2)) as signal_pct
  from waits as w1 
  join waits as w2 on w2.rn<=w1.rn
  group by w1.rn ,w1.wait_type,w1.wait_time_s,w1.pct,w1.signal_pct
  having sum(w2.pct)-w1.pct<80 or w1.rn<=5
  order by w1.rn


  --系统计数类型

  select 
  object_name,
  counter_name,
  instance_name,
  cntr_value,
  cntr_type
  from sys.dm_os_performance_counters

 
--查询数据库id和文件id
go
with dbio as(
select 
db_name(ivfs.database_id) as db,
mf.type_desc,
sum(ivfs.num_of_bytes_read+ivfs.num_of_bytes_written) as io_bytes,
sum(ivfs.io_stall) as io_stall_ms
from sys.dm_io_virtual_file_stats(null,null) as ivfs
join sys.master_files as mf
on ivfs.database_id =mf.database_id and mf.file_id=ivfs.file_id
grouP by  db_name(ivfs.database_id) ,mf.type_desc
)
select 
db,--数据库名称
type_desc,--文件类型
cast(1.0*io_bytes/(1024*1024) AS numeric(12,2)) as io_mb,--以字节为单位的总io读写
cast(io_stall_ms/1000 AS numeric(12,2)) as io_stall_s,--以秒为单位的总io等待时间
cast(100.0*io_stall_ms/sum(io_stall_ms) over() AS numeric(10,2)) as io_stall_s,--每个io的等待时间占整个系统的等待时间的百分比
ROW_NUMBER() over(order by io_stall_ms desc ) as rn--按照io等待时间排序的排序号
from dbio order by io_stall_ms desc

--查看数据库文件和日志文件信息
select *from sys.database_files
select *from sys.partitions
select *from sys.allocation_units
select *from sys.internal_tables
dbcc sqlperf(logspace)

--打开以下统计数据可以查看关于io的统计信息:扫描次数,逻辑读取次数,物理读取次数等信息
set statistics io on;
select *from Books
set statistics io off;
--打开一下统计信息可以查看关于执行时间的统计信息:
set statistics time on;
select *from Books
set statistics time off;

你可能感兴趣的:(sql,server,sqlserver)