標籤:des blog http io os ar for strong sp
分析下自己寫的SQL Server同步工具的效能和缺陷
1. C#同步SQL Server資料庫Schema
2. C#同步SQL Server資料庫中的資料--資料庫同步工具[同步新資料]
通過測試我寫的同步程式,得出結論:1.程式第一次調用SQLBulkCopy會耗時較長2.同步程式放在目標機器在耗時方面相對少些
測試資料:
declare @varI varchar(200)
set @varI=0
while(@varI<100000)
begin
set @[email protected]+1;
insert into [pink].[dbo].[Customers] ([CustomerName],[IsNewData])values (‘test‘+@varI, 1);
end
統計資料:(單位:秒)
source--> destination
sync program host on source host:
19.1431558
1.8603441
1.4992773
1.7913309
sync program host on destination host:
20.1563122
2.3704811
3.2282182
1.1696436
1.5312439
sync program host on non-source and non-destination host:
13.072742
1.8705590
1.2103618
另外測了下100萬條資料的一組資料:
source主機插入資料耗時2分鐘55秒
destination主機刪除資料耗時6秒
sync program host on source host:
22.1482469
sync program host on destination host:
18.5495432
通過測試發現了程式的缺陷:
當第二次同步時,查詢條件到10萬條已存在的記錄時,就已經超過限制:
報錯:internal error: Server stack limit has been reached. Please look for potentially deep nesting in your query, and try to simplify it.
原因:This issue occurs because SQL Server limits the number of identifiers and constants that can be contained in a single expression of a query. The limit is 65,535.
問題代碼:
string whereClause = " where ";
while (readerSource.Read())
{
isSourceContainsData = true;
whereClause += " " + primaryKeyName + "!=‘" + readerSource[primaryKeyName].ToString() + "‘ and ";
}
whereClause = whereClause.Remove(whereClause.Length - " and ".Length, " and ".Length);
readerSource.Close();
解決方案:
1。把查詢條件分成多個查詢條件
2。批量同步,比如100條100條的同步
分析下自己寫的SQL Server同步工具的效能和缺陷