sp_who active –看看哪個引起的死鎖, blk里面即阻塞的spid;
dbcc inputbuffer(@blk) — 可以查看是那個sql語句造成的死鎖;
sp_lock –看看鎖住了那個資源,objid即被鎖住的資源id;
select object_name(objid) 可得到受影響的表名;
綜合以上功能,我們可以寫一個自動查找死鎖的存儲過程,如下:
查找死鎖.sql
SET ANSI_NULLS ONsqlserver性能優(yōu)化
go
exec sp_configure ”awe enabled”,”1″–內(nèi)存可以支持64g
exec sp_configure ”lightweight pooling”,”0″–不使用nt纖程
exec sp_configure ”priority boost”,”1″–增加sqlserver優(yōu)先級
exec sp_configure ”network packet size (b)”,”8192″–增加sqlserver網(wǎng)絡包的大小
reconfigure with override
–優(yōu)化數(shù)據(jù)庫設(shè)置
declare @currentdatabase sysname
select @currentdatabase = db_name((select dbid from master.dbo.sysprocesses wherespid = @@spid))
exec sp_dboption @currentdatabase, ‘select into/bulkcopy’, ‘true’ –對大容量數(shù)據(jù)操作不記錄日志
exec sp_dboption @currentdatabase, ‘trunc. log on chkpt.’, ‘true’ –自動截斷日志
exec sp_dboption @currentdatabase, ‘a(chǎn)uto create statistics’, ‘true’–自動創(chuàng)建統(tǒng)計
exec sp_dboption @currentdatabase, ‘a(chǎn)uto update statistics’, ‘true’–自動更新統(tǒng)
go
如果你不做日常維護,請立即開始。
經(jīng)常用 DBCC showcontig(表名) 查看表的碎片, 主要查看掃描密度 ,如果主要的大表掃描密度 < 40%, 意味著你有嚴重的碎片可以用DBCC indexDefrag (數(shù)據(jù)庫名,表名)整理碎片或者用DBCC DBREINDEX(表名) 重建 index.