1. sqlserver 檢測sql
SELECT top 10
(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'執行次數'
,creation_time N'語句編譯時間'
,last_execution_time 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'執行語句'
,qp.query_plan
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
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;
2. 死鎖
select
request_session_id spid,
OBJECT_NAME(resource_associated_entity_id) tableName
from
sys.dm_tran_locks
where
resource_type='OBJECT'
GRANT SELECT ON sys.dm_tran_locks TO zabbix
3. 連接數
SELECT count(1) as icount FROM
[Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT
[DBID]
FROM
[Master].[dbo].[SYSDATABASES]
WHERE
NAME='JSTMBS'
);
4. 數據文件大小
select convert(float,sum(size)) * (8192.0/1024.0)/1024.0/1024 from JSTMBS.dbo.sysfiles
5. 查看作業 備份JSTMBS 的狀態
select top 1
CASE HIST.run_status
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
END as status
FROM msdb.dbo.sysjobs JOB
INNER JOIN msdb.dbo.sysjobhistory HIST ON HIST.job_id = JOB.job_id
WHERE
JOB.name = '備份JSTMBS'
and HIST.step_id =0
ORDER BY HIST.run_date desc;
6. sqlserver 統計碎片
select B.name,C.name,A.avg_fragmentation_in_percent from sys.dm_db_index_physical_stats(DB_ID() ,object_id('WF_WorkItem') ,NULL,NULL,NULL) A,sysobjects B,sys.indexes C
where A.object_id = B.id and A.object_id = C.object_id and C.index_id = A.index_id
and C.name is not null
and avg_fragmentation_in_percent > 90
7. 查看觸發器
SELECT tb2.name AS tableName,tb1.name AS triggerName FROM Sysobjects tb1 JOIN Sysobjects tb2 ON tb1.parent_obj=tb2.id
WHERE tb1.type='TR';
8. 調試觸發器
思路:創建存儲過程執行update語句,debug
-- 新建觸發器
Create TRIGGER [dbo].[OverloadCaseInfoForupdate] ON [dbo].[GL_OverloadCaseInfo]
for update
AS
DECLARE @CaseId varchar(50), @num int
select @CaseId=CaseId from inserted
select @num=count(CaseId) from GL_OverloadCaseInfo where CaseId=@CaseId and (IsAccept=0 or IsAccept=2)
if @num = 0
begin
ROLLBACK TRANSACTION
end
-- 創建存儲過程
Create proc [dbo].[testtri]
as
begin
update GL_OverloadCaseInfo set PlateColor='7' where CaseId='HZ9482f663ec7fb901643a9dbf163b9d'
end
-- 調試
點擊對應的數據庫,找到存儲過程 testtri ,右鍵 執行存儲過程,點擊上面 “調試”,按F11,會進入觸發器腳本。
9. 創建鏈接服務器
EXEC sp_addlinkedserver 'DB name','','SQLOLEDB','xxx.xx.xx.xxx'
EXEC sp_addlinkedsrvlogin 'DB name','false',NULL,'user','XXXXXX'