2015-12-02 43 views
0

問題涉及到SQL Server和C#。C#和SQL Server - 操作或服務器完成之前已超時的時間

我想在一個查詢中執行多個刪除語句。該格式具有以下結構:

DELETE from dbo.table where column = 'value' 

重複超過10萬次

我通過StringBuilder構建命令,並調用它的方式在我的C#代碼:但是它需要

cmd.Connection = con; 
int rows = cmd.ExecuteNonQuery(); 

很多時間執行並以此錯誤結束:

The timeout period elapsed prior to completion of the operation or the server. Executing through Management Studio takes also a lot of time, and let me think that the query isn't performant enough. However, in this case the query is executed successfully.

顯然,使用較少的DELETE語句,由於查詢非常簡單,查詢正常結束。執行多個語句並避免此錯誤的最佳方法是什麼?

+0

需要考慮的另一點:默認情況下,SQL Server使用**行級鎖**,並僅鎖定那些受例如一個'DELETE'語句。但是,如果您在單個事務中傳遞了5'000行,SQL Server將執行**鎖升級**並將鎖移至**整個表**,該表將成爲**獨佔鎖定**,直到事務完成。這意味着:在交易完成之前,沒有人能夠從該表中讀取*。所以,也許你需要將你的DELETE操作批量化爲更小的批次,以避免獨佔表鎖 –

回答

相關問題