1 常用系统存储过程
1.1 查询表结构
exec sp_columns 'your_table', @column_name = 'table_column'
1.2 查询表索引
exec sp_helpindex 'your_table';
1.3 查询存储过程定义
exec sp_helptext 'sp_columns'
1.4 显示表的磁盘空间使用情况
exec sp_spaceused 'your_table';
2 查看执行计划
2.1 执行计划查询启用和关闭语法
set showplan_all on | off
2.2 案例:在test_db数据库中的user表上查询“学号=123“的学生,并分析哪些索引被系统采用
use test_db
go
set showplan_all on
go
select * from user where id = '123'
go
set showplan_all off
go
```javascript
set showplan_all on | off
3 查看磁盘使用率(statistics io)
3.1 执行计划查询启用和关闭语法
set statistics io on | off
3.2 案例
use book_db
go
set statistics io on
go
select * from book where id = 'ybzt246'
go
set statistics io off
go
4 查询时间耗时较长的语句
select top 20
total_worker_time/1000 as [总消耗cpu 时间(ms)],execution_count [运行次数],
qs.total_worker_time/qs.execution_count/1000 as [平均消耗cpu 时间(ms)],
last_execution_time as [最后一次执行时间],max_worker_time /1000 as [最大执行时间(ms)],
substring(qt.text,qs.statement_start_offset/2+1,
(case when qs.statement_end_offset = -1
then datalength(qt.text)
else qs.statement_end_offset end -qs.statement_start_offset)/2 + 1)
as [使用cpu的语法], qt.text [完整语法],
dbname=db_name(qt.dbid),
object_name(qt.objectid,qt.dbid) objectname
from sys.dm_exec_query_stats qs with(nolock)
cross apply sys.dm_exec_sql_text(qs.sql_handle) as qt
where execution_count>1
order by total_worker_time desc
5 死锁处理
5.1 查询死锁进程id
select
request_session_id spid,
object_name(resource_associated_entity_id) tablename
from sys.dm_tran_locks
where resource_type='object'
5.2 查询死锁进程信息
exec sp_who2 ${spid}
5.3 杀死进程
kill ${spid}
发表评论