[英]Fastest way to copy data from one table to another using linq
我在另一台SQL服務器上有一張表,需要隔夜復制。 目標表的結構非常相似,因此我將使用下面的代碼。 來源-http://forums.asp.net/t/1322979.aspx/1
我還沒有嘗試過,但是在linq中有沒有更好/更快的方法可以做到這一點?
//there exist two table list and listSecond
DataClassesDataContext dataClass = new DataClassesDataContext(); //create the instance of the DataContext
var str = from a in dataClass.lists select a;
foreach (var val in str) // iterator the data from the list and insert them into the listSecond
{
listSecond ls = new listSecond();
ls.ID = val.ID;
ls.pid = val.pid;
ls.url = val.url;
dataClass.listSeconds.InsertOnSubmit(ls);
}
dataClass.SubmitChanges();
Response.Write("success");
使用LINQ插入大量數據不是一個好主意,除非是復雜的架構,在復制之前需要進行大量轉換。 除了將所有記錄都記錄在事務日志中之外,它將為插入的每一行創建一個單獨的查詢。
在這里可以找到一個更快的解決方案-它使用SqlBulkCopy
,這是一種在單個查詢中插入大量數據的方法,而無需進行事務日志記錄來降低其速度。 這將快一個數量級 ,而我是通過兩種方法的親身經驗告訴您的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.