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
9.表統計資訊的更新
UPDATE STATISTICS tblname -- 表名
--在執行計劃老是走偏的情況下,執行。比如,我們發現某張表在輔助資料庫上執行1S就可以執行完成,但是在主庫上執行耗時5S,檢視執行計劃,是走的索引不一樣。又不能修改程式,走強制索引(在SQL驗證中走強制索引,也可以降至1S)。
--後來就重新整理了此表的統計資訊。表的資料量為1560W,Size為30G,刷統計資訊耗時67S.