請問大家如何處理鎖和死鎖的問題(50分)

  • 主题发起人 zhongsir
  • 开始时间
Z

zhongsir

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,我用Windows2000+SQL SERVER2000 我現在的數據庫達到3GB這麼大,公司有200多台電腦。現在出現的問題是速度明顯慢下來,而且如附件所示產生鎖,而且還有死鎖的現象出現,請問大家如何處理?<br>最頭痛的問題一產生死鎖,整個系統就快完蛋了!請高手指點。謝謝!
 
加大服务器配置,改变数据库结构!
 
加大服務器配置,我們現在用的是dell2650的,還可以了。也不算太差。<br>改變數據結構的話,這不是一天兩天可以解決的,而且我看結構上也不是太不合理,如果系統結構要改,那麼推番的東西太多了。<br>大家沒有遇上過此類問題嗎?請大家多發表看法。謝謝了。
 
出现数据库锁或死锁并不一定是数据库大的原来,原来我们碰到过:<br>1.你的数据有3G,我想肯定绝大都是日志文件大,可以压缩一下日志文件,修改数据库日志保存方式为“简单”,你现在可能是“完全”.<br>2.数据库锁并不是所有表都有问题,而出现的原因可能是某一张表问题,当同时执行insert和delete时最容易出现锁,要查出这张表进行一下优化,比如增加索引等,同时你的程序也要做优化,尽量少用select *,一定要指明字段和增加where条件(where的条件最好能建立索引)。
 
TO:zylsoft,謝謝<br>你的第一條,我這應該不會發生,我也經常做sp_updatestats,dump transaction database with no_log,dbcc shrinkdatabase (database),所以日志比較小的。<br>你的第二條,比較有道理,我也正在往這方面查和修改,在我的記憶中用select *的比較少,where裡一般都沒有索引的,我的索引一般都建一個ID字段int遞增型的,不知道這樣合不合理?<br>請大家再多看看,還有沒有什麼原因造成這樣現象?
 
mssql数据库的性能和数据库日志大小有关系 建议定期清除日志<br>事务互锁的问题只有两种解决办法<br>1 是整理所有的事务 把可能导致互锁的逻辑修改掉(把在事务中 select max 然后在生成最大号插入这种方式改掉等等)<br>2 事务排队处理,每个事务没有完成之前不能进行下一个业务<br>个人推荐第一个做法 方法2虽然代码改动少一些 但是系统性能下降很多
 
where 里没索引会很慢,我测试过,where 没索引查询200多万的数据需要很长时间,如加索引就非常快了,就几秒
 
謝謝大家:<br>TO:ball_cao<br>請問如何做你的第二步,事務排隊處理?
 
我的经验告诉我,死锁的产后大多与你所写的事务内的代码有很大的相关.<br>像:ball_cao所说的,事务内把可能导致互锁的逻辑修改.
 
sql本身提供了多种机制来防死锁,但死锁还是会产生,我在编写程序中从来不手工加锁,我做过的系统没有这么大的数据,但我做过32个点,目前有30多万条的记录.目前没有出现过死锁的情况.
 
大家好,我有一個典形的事務處理如下,請看看有什麼問題,是不是這樣就會產生死鎖?<br>代碼我是用VB的。但SQL一樣。<br>On Error GoTo Err_確認出倉_Click:<br> &nbsp; Dim Oqty As Single<br> &nbsp; Dim Mconn As ADODB.Connection<br> &nbsp; Set Mconn = CurrentProject.Connection<br><br> &nbsp; Dim StrOne As ADODB.Command<br> &nbsp; Set StrOne = New ADODB.Command<br> &nbsp; &nbsp; &nbsp; StrOne.ActiveConnection = Mconn<br> &nbsp; &nbsp; &nbsp; StrOne.CommandType = adCmdText<br> &nbsp; &nbsp; &nbsp;<br> &nbsp; Dim StrTwo As ADODB.Command<br> &nbsp; Set StrTwo = New ADODB.Command<br> &nbsp; &nbsp; &nbsp; StrTwo.ActiveConnection = Mconn<br> &nbsp; &nbsp; &nbsp; StrTwo.CommandType = adCmdText<br> &nbsp; Dim StrThree As ADODB.Command<br> &nbsp; Set StrThree = New ADODB.Command<br> &nbsp; &nbsp; &nbsp; StrThree.ActiveConnection = Mconn<br> &nbsp; &nbsp; &nbsp; StrThree.CommandType = adCmdText<br>Mconn.BeginTrans<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;StrOne.CommandText = &quot;INSERT INTO BOM發料記錄表 (計劃單號,發貨單編號,部件型號,發貨數量,日期,出倉標記,備注)&quot; & _<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;VALUES ('&quot; & Me.計劃單號 & &quot;','&quot; & Me.出倉單編號 & &quot;','&quot; & Me.部件型號 & &quot;','&quot; & Me.數量 & &quot;','&quot; & Date & &quot;','&quot; & Me.出倉標記 & &quot;','&quot; & Me.備注 & &quot;');&quot;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;StrTwo.CommandText = &quot;UPDATE 噴油加工計劃表 &quot; & _<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;SET 噴油加工計劃表.領料數量 = [領料數量] + &quot; & Me.數量 & _<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;WHERE (((噴油加工計劃表.計劃單號)='&quot; & Me.計劃單號 & &quot;') and ((噴油加工計劃表.部件型號)='&quot; & Me.部件型號 & &quot;') and ((噴油加工計劃表.顏色)='&quot; & Me.顏色 & &quot;'));&quot;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp;StrThree.CommandText = &quot;UPDATE 部件表 &quot; & _<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;SET 部件表.庫存量=部件表.庫存量 - &quot; & Me.數量 & &quot;, 部件表.噴油加工數 = 部件表.噴油加工數 +&quot; & Me.數量 & &quot; &quot; & _<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;WHERE 部件表.部件型號= '&quot; & Me.部件型號 & &quot;';&quot;<br><br> &nbsp; &nbsp;StrOne.Execute<br> &nbsp; &nbsp;StrTwo.Execute<br> &nbsp; &nbsp;StrThree.Execute<br> &nbsp; &nbsp;Mconn.CommitTrans<br> &nbsp; &nbsp;Set StrOne = Nothing<br> &nbsp; &nbsp;Set StrTwo = Nothing<br> &nbsp; &nbsp;Set StrThree = Nothing<br>MsgBox &quot; &nbsp; &nbsp; &nbsp; &nbsp; OK&quot; &nbsp; &nbsp; &nbsp; <br>End If<br>Exit Sub<br><br>Err_確認出倉_Click:<br> &nbsp; Mconn.RollbackTrans<br> &nbsp; MsgBox &quot; 進倉出錯,請關閉程序再重試! &nbsp;&quot;<br> &nbsp; Set StrOne = Nothing<br> &nbsp; Set StrTwo = Nothing<br> &nbsp; Set StrThree = Nothing
 
沒有其它辦法了嗎?兄弟們!
 
大家再多說點。謝謝了!
 
一般來講避免死鎖:1、縮短每個事務的長度,2.盡可能把事務包含在一個批內,並避免在事務內部於用戶進行交互。
 
您这个典型事务单独执行或多个这样的事务执行都应不会造成死锁<br>但是如果您还有一个事务的操作顺序是<br>部件表<br>噴油加工計劃表<br>bom表<br>则这两个事务同时提交时有可能死锁
 
顶部