我正在使用SQLBULKCOPY將一些數據表複製到數據庫表中,但是,由於我複製的文件大小有時超過600mb,因此我的內存不足。sqlbulkcopy mem。管理
我希望在將數據提交到數據庫之前得到一些有關管理表大小的建議,這樣我可以釋放一些內存以繼續寫入。
下面是我的一些代碼示例(一些列和行取消了簡單)
SqlBulkCopy sqlbulkCopy = new SqlBulkCopy(ServerConfiguration); //Define the Server Configuration
System.IO.StreamReader rdr = new System.IO.StreamReader(fileName);
Console.WriteLine("Counting number of lines...");
Console.WriteLine("{0}, Contains: {1} Lines", fileName, countLines(fileName));
DataTable dt = new DataTable();
sqlbulkCopy.DestinationTableName = "[dbo].[buy.com]"; //You need to define the target table name where the data will be copied
dt.Columns.Add("PROGRAMNAME");
dt.Columns.Add("PROGRAMURL");
dt.Columns.Add("CATALOGNAME");
string inputLine = "";
DataRow row; //Declare a row, which will be added to the above data table
while ((inputLine = rdr.ReadLine()) != null) //Read while the line is not null
{
i = 0;
string[] arr;
Console.Write("\rWriting Line: {0}", k);
arr = inputLine.Split('\t'); //splitting the line which was read by the stream reader object (tab delimited)
row = dt.NewRow();
row["PROGRAMNAME"] = arr[i++];
row["PROGRAMURL"] = arr[i++];
row["CATALOGNAME"] = arr[i++];
row["LASTUPDATED"] = arr[i++];
row["NAME"] = arr[i++];
dt.Rows.Add(row);
k++;
}
// Set the timeout, 600 secons (10 minutes) given table size--damn that's a lota hooch
sqlbulkCopy.BulkCopyTimeout = 600;
try
{
sqlbulkCopy.WriteToServer(dt);
}
catch (Exception e)
{
Console.WriteLine(e);
}
sqlbulkCopy.Close();//Release the resources
dt.Dispose();
Console.WriteLine("\nDB Table Written: \"{0}\" \n\n", sqlbulkCopy.DestinationTableName.ToString());
}
我繼續有問題越來越SqlBulkCopy的工作,我意識到我需要做更多的工作,上每個記錄在它被輸入到數據庫之前,所以我開發了一個簡單的LinQ to Sql方法來記錄更新記錄,所以我可以編輯其他信息並創建更多的記錄信息,因爲它正在運行,
問題:This方法一直運行得很慢w(即使在Core i3機器上),關於如何加速它的任何想法(線程?) - 在單個處理器內核上,1GB內存會崩潰或需要6-8小時來寫入相同數量的數據一個SQLBulkCopy需要一些時間。它確實可以更好地管理內存。
while ((inputLine = rdr.ReadLine()) != null) //Read while the line is not null
{
Console.Write("\rWriting Line: {0}", k);
string[] arr;
arr = inputLine.Split('\t');
/* items */
if (fileName.Contains(",,"))
{
Item = Table(arr);
table.tables.InsertOnSubmit(Item);
/* Check to see if the item is in the db */
bool exists = table.tables.Where(u => u.ProductID == Item.ProductID).Any();
/* Commit */
if (!exists)
{
try
{
table.SubmitChanges();
}
catch (Exception e)
{
Console.WriteLine(e);
// Make some adjustments.
// ...
// Try again.
table.SubmitChanges();
}
}
}
隨着helper方法:
public static class extensionMethods
{
/// <summary>
/// Method that provides the T-SQL EXISTS call for any IQueryable (thus extending Linq).
/// </summary>
/// <remarks>Returns whether or not the predicate conditions exists at least one time.</remarks>
public static bool Exists<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
{
return source.Where(predicate).Any();
}
}
或許還配置了RDR的可能是一個不錯的主意 – 2010-08-02 03:49:21
感謝 - 我會記住這一點 我想知道,如果我提前處理我的讀者在閱讀完每個文件之前,我完成了閱讀程序當我正在閱讀時,我將放鬆自己的步伐,並且在閱讀時出現內存不足錯誤,而我正在寫作... 任何想法我將如何跳回到我在檔案中的位置? – 2010-08-02 07:20:11
請參閱上面的評論... – 2010-08-02 09:27:12