分析下自己寫的SQL Server同步工具的效能和缺陷

來源:互聯網
上載者:User

標籤: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同步工具的效能和缺陷

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.