![](/img/trans.png)
[英]INSERT more than 1000 rows into SQL Server database from JSON in C# code
[英]MongoDB (C#) Insert operation taking more time compared to SQL Server Insert operation(1000 times more)
我正在從文件結構中插入數據,如圖所示。
文件夾->子文件夾->子文件夾->文件
以前,此數據存儲在SQL Server中。 將所有這些數據插入SQL所需的時間為26秒,而將相同數據插入MongoDB所需的時間為2500秒(負擔不起)。
該文件是xml文件。 文件夾/子文件夾詳細信息存儲在一個表中,文件詳細信息存儲在另一個表中,文件中的某些標簽存儲在另一表中。 我的代碼遞歸檢查並存儲數據。
插入操作同時插入到4個集合中(在SQL插入期間類似於4個表)。
我不想使用一台以上的服務器,因為我將一台服務器用於SQL。 除了MongoDbs _id字段索引(隱式)外,我也沒有明確地進行任何索引編制
請找到以下代碼-
在SQL中:
public int AddRowToFolder(string fRelativePosition,
string flabel, string fPhysicalName, int fParentId, int fViewOrder, EntityState fViewState,
EntityProtection fProtection)
{
//Increment the Folder row count by 1.
folderRowCount++;
try
{
folderDataRow = folderDataSet.Tables[0].NewRow();
//Adding the values to the Folder Dataset.
folderDataRow[Convert.ToInt32(FolderColumns.RelativePosition)] = fRelativePosition;
folderDataRow[Convert.ToInt32(FolderColumns.Label)] = flabel;
folderDataRow[Convert.ToInt32(FolderColumns.PhysicalName)] = fPhysicalName;
folderDataRow[Convert.ToInt32(FolderColumns.ParentId)] = fParentId;
folderDataRow[Convert.ToInt32(FolderColumns.ViewOrder)] = fViewOrder;
folderDataRow[Convert.ToInt32(FolderColumns.ViewState)] = fViewState;
folderDataRow[Convert.ToInt32(FolderColumns.Protection)] = fProtection;
folderDataSet.Tables[0].Rows.Add(folderDataRow);
}
return folderRowCount;
}
在MongoDB中:
public int AddRowToFolder(string fRelativePosition,
string flabel, string fPhysicalName, int fParentId, int fViewOrder, EntityState fViewState,
EntityProtection fProtection)
{
//Increment the Folder row count by 1.
folderRowCount++;
try
{
BsonDocument doc = new BsonDocument();
//Adding the values to the Folder Dataset.
doc.Add(FolderColumns.RelativePosition.ToString() , fRelativePosition);
doc.Add((FolderColumns.Label).ToString(), flabel);
doc.Add((FolderColumns.PhysicalName).ToString(), fPhysicalName);
doc.Add((FolderColumns.ParentId).ToString(), fParentId);
doc.Add((FolderColumns.ViewOrder).ToString(),fViewOrder);
doc.Add((FolderColumns.ViewState).ToString(),fViewState);
doc.Add((FolderColumns.Protection).ToString(), fProtection);
foldersCollection.Insert(doc, WriteConcern.Unacknowledged);
}
return folderRowCount;
}
我正在使用的連接字符串是:
DbConnString = "mongodb://localhost"
與SQL相比,MongoDB的寫操作預計會更快,但是我看不到。 有人可以幫忙嗎?
我發現自己的錯誤非常愚蠢。 我在調試模式下運行我的應用程序。 當我執行exe時,速度更快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.