1. 查詢某存儲過程的訪問情況 SELECT TOP 1000 db_name(d.database_id) as DBName, s.name as 存儲名字, s.type_desc as 存儲類型, d.cached_time as SP添加到緩存的時間, d.last_execution_t ...
1. 查詢某存儲過程的訪問情況
SELECT TOP 1000 db_name(d.database_id) as DBName, s.name as 存儲名字, s.type_desc as 存儲類型,
d.cached_time as SP添加到緩存的時間, d.last_execution_time AS 上次執行SP的時間,
d.last_elapsed_time as [上次執行SP所用的時間(微秒)], d.total_elapsed_time [完成此SP的執行所用的總時間(微秒)],
d.total_elapsed_time/d.execution_count AS [平均執行時間(微秒)], d.execution_count as 自上次編譯以來所執行的次數
FROM sys.procedures s INNER JOIN sys.dm_exec_procedure_stats d
ON s.object_id = d.object_id where s.name='存儲過程的名稱' ----請在此處替換要統計的SP
ORDER BY d.total_elapsed_time/d.execution_count DESC
2. 查詢包含關鍵字(字元串)的存儲過程 SP
Select distinct o.* from sysobjects o, syscomments s
where o.xtype='P' and s.id=o.id and text like '%關鍵字%' order by o.name
3. 查詢包含關鍵字(字元串)的Job
Select * from msdb.dbo.sysjobs
where job_id in(Select job_id from msdb.dbo.sysjobsteps where command like '%XXXXXX%' )
4. 查詢哪些SQL語句占用CPU的頻率高
SELECT TOP 10 [cpu_time],
[session_id],
[request_id],
[start_time] AS '開始時間',
[status] AS '狀態',
[command] AS '命令',
dest.[text] AS 'sql語句',
DB_NAME([database_id]) AS '資料庫名',
[blocking_session_id] AS '正在阻塞其他會話的會話ID',
[wait_type] AS '等待資源類型',
[wait_time] AS '等待時間',
[wait_resource] AS '等待的資源',
[reads] AS '物理讀次數',
[writes] AS '寫次數',
[logical_reads] AS '邏輯讀次數',
[row_count] AS '返回結果行數'
FROM sys.[dm_exec_requests] AS der
CROSS APPLY
sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id]>50 AND DB_NAME(der.[database_id])='資料庫名字' ----請在此處輸入要統計的資料庫名字
ORDER BY [cpu_time] DESC
5. DB的單用戶模式更新成多用戶模式
alter database 資料庫名字 set multi_user;
6. 查詢某表的列的詳情
select * from syscolumns where id=object_id('要統計的表') ---請替換
7. 查詢當前SQL Server日誌信息
Exec xp_readerrorlog 0
其實 xp_readerrorlog 一共有7個參數
(1) 存檔編號;
(2) 日誌類型(1為SQL Server日誌,2為SQL Agent日誌);
(3) 查詢包含的字元串;
(4) 查詢包含的字元串;
(5) LogDate開始時間;
(6) 結果排序,按LogDate排序(可以為降序"Desc" Or 升序"Asc");
(7) 結果排序,按LogDate排序(可以為降序"Desc" Or 升序"Asc") 。
8. 查詢Job 明細
SELECT b.[name] [JobName]
,b.enabled [Enabled]
,a.step_id [StepID]
,b.description [JobDescription]
,a.step_name [StepName]
,a.command [Script]
FROM msdb.dbo.sysjobsteps a
INNER JOIN msdb.dbo.sysjobs b
ON a.job_id=b.job_id